2014-09-10 76 views
3

在我的控制,我有以下這些方法運作良好Requestmapping不工作時,有頭

@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){} 

例外: 表示形式:null org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException:找不到匹配的處理程序方法 t request:path'/search/searchresults.json',方法'GET',

我試過如下,

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

但它拋出, java.lang.IllegalArgumentException異常: 「字符集= UTF-8」 不包含 '/'

如何解決它

回答

0

您還需要指定標頭名稱,即content-type。更改此:

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

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

我試圖頭= { 「內容類型=應用/ JSON,字符集= UTF-8」},但它拋出的java .lang.IllegalArgumentException:「charset = UTF-8」不包含'/' – tjkmr 2014-09-11 05:37:46

0

變化headersproduces

@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

0

使用使用@RestController;的,

@RequestMapping(value="/searchresults", method = RequestMethod.GET,headers = {"content-type=application/json;charset=UTF-8"}) 
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

0

我發現這個有用的(被卡住彈簧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); 
}