简体   繁体   中英

Requestmapping not working when there is headers

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

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 .

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"}

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

Use ; instead of ,

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

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

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM