在我的控制器中,我有一个正常运行的方法
@RequestMapping(value="/searchresults",method = RequestMethod.GET)
public SearchResponse searchResults(
@PathVariable("domain") String domain,
@RequestParam(value="rowCount" , defaultValue="0", required=false) Integer rowCount,
HttpServletRequest req){}
但是添加标题时同样的事情不起作用,
@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){}
例外: Representation:null org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException:没有为servle找到匹配的处理程序方法 请求:路径' /search/searchresults.json' ;,方法' GET',
我尝试了如下,
@RequestMapping(value="/searchresults", method = RequestMethod.GET,headers = {"content-type=application/json,charset=UTF-8"})
但它会抛出, java.lang.IllegalArgumentException:" charset = UTF-8"不包含' /'
如何解决它
答案 0 :(得分:1)
您忘记添加标题名称:|
application/json
是内容类型,而UTF-8
是 Charset 。
查看complete list of HTTP headers。
正确的映射将是:
@RequestMapping(value="/searchresults", method = RequestMethod.GET,
headers = {"content-type=application/json,charset=UTF-8"})
那就是说,值得知道ContentType should be specified only for POST and PUT requests。
答案 1 :(得分:0)
您还需要指定标题名称,即content-type
。改变这个:
headers ="application/json;charset=UTF-8"
到
headers = {"content-type=application/json,charset=UTF-8"}
答案 2 :(得分:0)
将headers
更改为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){}
理想情况下,如果使用Spring 4
,则应使用@RestController
答案 3 :(得分:0)
使用;而不是,
@RequestMapping(value="/searchresults", method = RequestMethod.GET,headers = {"content-type=application/json;charset=UTF-8"})
答案 4 :(得分:0)
对于那些使用WebLogic的人来说有一个解决方法,而其他应用服务器也可以做类似的事情,这里的内容对我有用 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>
我的请求映射注释如下所示:
@RequestMapping(method = RequestMethod.POST, value = "/echo", produces = "text/plain;charset=UTF-8", headers = "Accept=*/*")
添加
headers = {"content-type=application/json,charset=UTF-8"}
没有帮助,我很困惑为什么,但我以某种方式离开了。 HTH