Request does not work when there are headers
In my controller I have a method below which works well
@RequestMapping(value="/searchresults",method = RequestMethod.GET)
public SearchResponse searchResults(
@PathVariable("domain") String domain,
@RequestParam(value="rowCount" , defaultValue="0", required=false) Integer rowCount,
HttpServletRequest req){}
but the same doesn't work when adding headers,
@RequestMapping(value="/searchresults", method = RequestMethod.GET,headers = "application/json;charset=UTF-8")
public SearchResponse searchResults(
@PathVariable("domain") String domain,
@RequestParam(value="rowCount" , defaultValue="0", required=false) Integer rowCount,
HttpServletRequest req){}
Exception: View: null org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException: No matching handler method found for servlet t request: path '/search/searchresults.json', method 'GET',
I tried the following:
@RequestMapping(value="/searchresults", method = RequestMethod.GET,headers = {"content-type=application/json,charset=UTF-8"})
but it throws, java.lang.IllegalArgumentException: "charset = UTF-8" does not contain '/'
How to resolve it
source to share
You forgot to add header names: |
application/json
- Content-Type , and UTF-8
- Charset .
Check out the complete list of HTTP headers .
The correct display will be as follows:
@RequestMapping(value="/searchresults", method = RequestMethod.GET,
headers = {"content-type=application/json,charset=UTF-8"})
However, it's worth knowing that the ContentType should only be specified for POST and PUT requests .
source to share
Change headers
toproduces
@RequestMapping(value="/searchresults", method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
public SearchResponse searchResults(
@PathVariable("domain") String domain,
@RequestParam(value="rowCount" , defaultValue="0", required=false) Integer rowCount,
HttpServletRequest req){}
Ideally you should use @RestController
if using Spring 4
source to share
A workaround for anyone using WebLogic and possibly other app servers does the same, here's what worked for me in weblogic.xml
<wls:charset-params>
<wls:input-charset>
<wls:resource-path>/restful</wls:resource-path>
<wls:java-charset-name>UTF-8</wls:java-charset-name>
</wls:input-charset>
</wls:charset-params>
My Request Mapping annotations look like this:
@RequestMapping(method = RequestMethod.POST, value = "/echo", produces = "text/plain;charset=UTF-8", headers = "Accept=*/*")
addition
headers = {"content-type=application/json,charset=UTF-8"}
didn't help and I'm puzzled as to why, but I got away somehow. NTN
source to share
I found this helpful (stuck with Spring 3.0.x):
@ResponseBody
public ResponseEntity<String> getResponse() {
String body = ...
MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>();
headers.set("Content-Type", "application/json;charset=UTF-8");
return new ResponseEntity<String>(body, headers, HttpStatus.OK);
}
source to share