3

In My controller i have a method below which is working 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 thing is not working 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 : Representation: null org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException: No matching handler method found for servle t request: path '/search/searchresults.json', method 'GET',

I tried as follows,

@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

tjkmr
  • 71
  • 2
  • 7

6 Answers6

2

You forgot to add the headers names :|

application/json is the Content-Type, while UTF-8 is the Charset.

Take a look at the complete list of HTTP headers.

The correct mapping will then be :

@RequestMapping(value="/searchresults", method = RequestMethod.GET, 
                       headers = {"content-type=application/json,charset=UTF-8"})

That said, it's worth knowing that the ContentType should be specified only for POST and PUT requests.

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
0

You need to specify the header name also, which is content-type. Change this:

headers ="application/json;charset=UTF-8"

to

headers = {"content-type=application/json,charset=UTF-8"}
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
  • I tried headers = {"content-type=application/json,charset=UTF-8"} but it throws java.lang.IllegalArgumentException: "charset=UTF-8" does not contain '/' – tjkmr Sep 11 '14 at 05:37
0

Change headers to produces

@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 you are using Spring 4

shazin
  • 21,379
  • 3
  • 54
  • 71
0

Use ; instead of ,

@RequestMapping(value="/searchresults", method = RequestMethod.GET,headers = {"content-type=application/json;charset=UTF-8"})
Filipe Roberto
  • 339
  • 4
  • 16
0

There is a workaround for those who use WebLogic, while maybe other app servers an do the similar, here is 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 annotation looks like this:

@RequestMapping(method = RequestMethod.POST, value = "/echo", produces = "text/plain;charset=UTF-8", headers = "Accept=*/*")

adding

headers = {"content-type=application/json,charset=UTF-8"}

didn't help and I am puzzled why, but I got away somehow. HTH

Peter
  • 566
  • 4
  • 14
0

I found this useful (being 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);
}
mks-d
  • 166
  • 3
  • 14