我正在开发一个Spring RESTful应用程序,它有MVC架构。 3层如下:dao,service和controller。控制器层(包)负责向JSON数据提供相应的URL。 Controller包中的以下方法有效,但在某些输入值中断,
@RequestMapping(value = "/category/{category}", method = RequestMethod.GET)
public @ResponseBody ResponseEntity<String> getGivenCategory(@PathVariable String category){
HttpHeaders ResultHeader = new HttpHeaders();
ResultHeader.set("Access-Control-Allow-Origin", "*");
ResultHeader.set("Content-Type", "application/json");
String result = productsService.getGivenCategory(category);
if(null == result){
return new ResponseEntity<String>( "{}", ResultHeader, HttpStatus.EXPECTATION_FAILED);
}
return new ResponseEntity<String>(result, ResultHeader, HttpStatus.OK);
}
该方法适用于常用字符串,例如“MASONRY COATINGS”或“SOS FAUCETS”。因此,浏览器中的调用将是,
http://localhost:8080/searchAPI/category/MASONRY%20COATINGS
但是,对于某些字符串,URL会分解,例如“PLYWOOD - SANDED / FINISHED”。我假设使用某些正则表达式可以解决问题,但是,我的知识在这种情况下非常有限。有什么建议我怎么能破坏代码?
答案 0 :(得分:0)
您需要将CharacterEncodingFilter过滤器添加到您的web.xml
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>