How to get HttpServletRequest object in FlowController?
I am trying to retrieve request object from getNativeRequest() method,But it is not giving HttpServletRequest object.
(HttpServletRequest)RequestContextHolder.getRequestContext().getExternalContext().getNativeRequest()
I followed above approach, It's returning SRTServletRequest object.
Correct me if I'm wrong, but SRTServletRequest implements HttpServletRequest, so you should just be able to cast it.
Related
I am in process of writing a junit for one of the controller methods with method signature as below:
#RequestMapping(value="/getTokenizedURL.json",method=RequestMethod.POST)
#ResponseBody
public ResponseData getTokenizedURL(#RequestBody final RequestData requestData, final HttpServletRequest request) throws CustomException
I need to call this method using MockMvc and I am able to call using below:
mockMvc.perform(post("/user/getTokenizedURL.json")
.contentType(MediaType.APPLICATION_JSON)
.content(json))
.andDo(print())
.andExpect(status().isOk());
But the problem is I am unable to setup HttpServletRequest Parameter while calling the original method using mock mvc. Without setting HttpServletRequest argument, my test is giving issues since it is something required and used with in original method.
Please let me know how should I setup the same. Thanks!
The idea is that you shouldn't need to.
MockMvcRequestBuilders#post(..) returns a MockHttpServletRequestBuilder which lets you construct the request with any values you want. These will be mirrored in the HttpServletRequest that gets passed to your handler method.
For example, you used
.contentType(MediaType.APPLICATION_JSON)
this will set the content-type header of the request. In your handler method, if you did
request.getHeader("content-type");
you'd get back a corresponding String for the MediaType.APPLICATION_JSON.
The MockHttpServletRequestBuilder has "setters" for every part of the request.
In Spring Framework,i am using responseBody annotation and serialization,i learned that responseBody is for HttpMessageConverts,it will return the output to view Resolver and serialization will convert the data in to byte stream and transfer it using version Id and header,here i have a question that, whats the difference between this two ?
Serialization is a computer science concept that describes how a data structure can be broken down and stored. Deserialization is the reverse, taking a stored format and converting it back into a data structure.
#ResponseBody is an annotation that Spring MVC uses on #RequestMapping methods. It tells the DispatcherServlet to take the return value of your handler method and, using an HttpMessageConverter, serialize it and write it directly to the HTTP response OutputStream.
See the javadoc of HttpMessageConverter for a list of implementation classes. You can write byte[], String, InputStream, Resource objects directly to the stream. There also exist HttpMessageConverter classes for converting any object returned by the handler method to JSON or XML.
I have read the Spring documentation and I was not able to find relevant information regarding the expected behavior of a Spring MVC controller method returning null (with a return type of String).
Can someone please provide a reply or direct me to relevant documentation? Or to put it another way, what would be the benefit of returning null from a Spring MVC controller method?
In Spring 2, when you returned null from a controller you were saying to the Spring dispatcher that you don't want it to search for a view.
You did this if you were handling the response yourself by writing the response content directly and then flushing the output stream (you were managing a file download for example).
If you didn't return null, Spring would have forwarded to a view who would try to write to the response also, messing up your already written data or resulting in an exception if the response was already commited.
Returning null was a way of saying back off to Spring's view resolver.
A lot of things changed in Spring 3 and now the same can be obtained by having an #RequestMapping annotated method that returns void.
If you have a return type of String but you return null I think that it uses the default RequestToViewNameTranslator for translating an incoming HttpServletRequest into a logical view name when the view name wasn't explicitly supplied.
Return type String in spring mvc generally returns your view resolver, it can be your JSP, html or any other view page.
http://www.mkyong.com/spring3/spring-3-mvc-hello-world-example/
Suppose you want to return a normal String like "hi", you can use #ResponseBody annotation to do this.
Can anyone describe when and why you might want to use Spring MVC ModelAndView instead of just returning a String for the view name and using a ModelMap.
I know one method is older than the other, but I don't know why one method might be better than the other?
Thanks
I think other than one style being newer than the other there is no one better way.
The String view name + ModelMap is probably more familiar to developers coming from Struts though.
One more advantage of using ModelAndView is that it tell's the Spring container explicitly that you want to send a View and not a string in case if you are using #ResponseBody in your controller say for AJAX calls, this way if there is an error in your jsp, with ModelAndView you will get an exception whereas in the String version the String will be returned as it is to the view page without throwing any exception......
I have a small question regarding Spring's MVC data binding capabilities.
I do have the following controller class:
#Controller
#RequestMapping("/foo")
public class FooController() {
// … some init stuff //
#RequestMapping(value = "/{id}/edit.{format}", method = RequestMethod.POST)
public ModelAndView editFoo(#RequestBody FooItem foo, #PathVariable("format") String format) {
// some code here to edit the FooItem //
}
}
I want to be able to post form data as well as XML against this method. For that to work I added two message converters to my applicationContext.xml: The default formHttpMessageConverter and an XStream marshaller.
This works fine, but I have a problem, that if I use #RequestBody and post form data against the URL, the server responds with a 415 Error. If I remove this annotation, form data works well and Spring creates the object for me, but if I post XML against it, I get an empty object.
Is there any way around this or do I need to have 2 methods to be able to handle both of the incoming formats?
Thanks in advance!
I think you need two methods.
FormHttpMessageConverter doesn't have the same databinding capabilities as #ModelAttribute provides, it can't bind request to the specified target class, only to MultiValueMap (see javadoc).