Return literal JSON strings in spring mvc @ResponseBody

This works with Jackson 2 (at least): @Controller public class YourController { @RequestMapping(..) public @ResponseBody Json get() { return new Json(“{ \”attr\” : \”value\” }”); } } class Json { private final String value; public Json(String value) { this.value = value; } @JsonValue @JsonRawValue public String value() { return value; } } Not particularly pretty … Read more

spring mvc get all request mappings

I am replicating one of my previous answers here: If you are using Spring 3.1 this handlerMapping component is an instance of RequestMappingHandlerMapping, which you can query to find the handlerMappedMethods and the associated controllers, along these lines(if you are on an older version of Spring, you should be able to use a similar approach): … Read more

@ModelAttribute annotation, when to use it?

You don’t need @ModelAttribute (parameter) just to use a Bean as a parameter For example, these handler methods work fine with these requests: @RequestMapping(“/a”) void pathA(SomeBean someBean) { assertEquals(“neil”, someBean.getName()); } GET /a?name=neil @RequestMapping(value=”/a”, method=RequestMethod.POST) void pathAPost(SomeBean someBean) { assertEquals(“neil”, someBean.getName()); } POST /a name=neil Use @ModelAttribute (method) to load default data into your model … Read more

How to pass post array parameter in Spring MVC

You can use this: @RequestMapping(value=”/schedule”, method = RequestMethod.POST) public void action(@RequestParam(value = “param[]”) String[] paramValues){…} it will retrieve all values (inside array paramValues) of parameter param (note the attribute value of RequestParam: it ends with [])

mockMvc – Test Error Message

You can use the method status.reason(). For example: @Test public void loginWithBadCredentials() { this.mockMvc.perform( post(“/rest/login”) .contentType(MediaType.APPLICATION_JSON) .content(“{\”username\”: \”baduser\”, \”password\”: \”invalidPassword\”}”) ) .andDo(MockMvcResultHandlers.print()) .andExpect(status().isUnauthorized()) .andExpect(status().reason(containsString(“Bad credentials”))) .andExpect(unauthenticated()); } MockHttpServletResponse: Status = 401 Error message = Authentication Failed: Bad credentials Content type = null Body = Forwarded URL = null Redirected URL = null Cookies = []

ContentCachingResponseWrapper Produces Empty Response

After couple of hours of struggling, I’ve finally found the solution. In short, ContentCachingResponseWrapper.copyBodyToResponse() should be called in the end of the filter method. ContentCachingResponseWrapper caches the response body by reading it from response output stream. So, the stream becomes empty. To write response back to the output stream ContentCachingResponseWrapper.copyBodyToResponse() should be used.

AngularJS with Spring-mvc [closed]

Two cases: Your architecture is full client-side: In this case the integration is very natural. Spring MVC exposes your service as a REST (JSON/XML) and your client application with AngularJS consumes your JSON. Here the .war application (Spring MVC) must be deployed in a servlet container (e.g. Tomcat) and your client application can be deployed … Read more

Isolated Controller Test can’t instantiate Pageable

Original answer: The problem with pageable can be solved by providing a custom argument handler. If this is set you will run in a ViewResolver Exception (loop). To avoid this you have to set a ViewResolver (an anonymous JSON ViewResolver class for example). mockMvc = MockMvcBuilders.standaloneSetup(controller) .setCustomArgumentResolvers(new PageableHandlerMethodArgumentResolver()) .setViewResolvers(new ViewResolver() { @Override public View resolveViewName(String … Read more