Java Spring3.0返回的json字符串显示为问号(ResponseEntity)
我有一个web应用程序用于实践。 并且在json返回方面有问题 编码为UTF-8的unicode字符串通常存储在orcl数据库中。 在服务器阶段,这个unicode字符串是正常状态 但在前端(angularjs),这个unicode字符串显示为问号。 前端的所有unicode字符串都显示为问号 我现在已经尝试了很多方法,并且查找了很多关于这个问题的地方。 我不知道我必须为此做些什么 我使用的是Spring3.0.5、Angular.js1.2、oracle数据库、Tomcat7 这是控制器方法Java Spring3.0返回的json字符串显示为问号(ResponseEntity),java,angularjs,json,spring,unicode,Java,Angularjs,Json,Spring,Unicode,我有一个web应用程序用于实践。 并且在json返回方面有问题 编码为UTF-8的unicode字符串通常存储在orcl数据库中。 在服务器阶段,这个unicode字符串是正常状态 但在前端(angularjs),这个unicode字符串显示为问号。 前端的所有unicode字符串都显示为问号 我现在已经尝试了很多方法,并且查找了很多关于这个问题的地方。 我不知道我必须为此做些什么 我使用的是Spring3.0.5、Angular.js1.2、oracle数据库、Tomcat7 这是控制器方法
@RequestMapping(value = {"/categories"}, method = RequestMethod.GET, headers = "Accept=application/json")
@ResponseBody
public ResponseEntity<String> getCategories() throws ParseException, UnsupportedEncodingException {
List<Category> categories= postService.getCategories();
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "application/json; charset=utf-8");
if (categories == null) {
return new ResponseEntity<String>(headers, HttpStatus.NOT_FOUND);
}
return new ResponseEntity<String>(JSONResponseUtil.getJSONString(categories), headers, HttpStatus.OK);
}
和错误数据
{
"postId": "POST14042622131875",
"realPath": "1398517998159_8.jpeg",
"title": "????",
"textContent": "",
"nickName": "Demian",
"userId": "Demian",
"categoryName": "???",
"categoryId": "ES"
}
您应该设置响应标题,例如
@RequestMapping("/test22")
@ResponseBody
public ResponseEntity<String> test22(){
MultiValueMap<String, String> map=new LinkedMultiValueMap<>();
map.put("Content-Type", Lists.newArrayList("text/html;charset=UTF-8"));
return new ResponseEntity<String>("you body",map , HttpStatus.OK);
}
您应该设置响应标题,例如
@RequestMapping("/test22")
@ResponseBody
public ResponseEntity<String> test22(){
MultiValueMap<String, String> map=new LinkedMultiValueMap<>();
map.put("Content-Type", Lists.newArrayList("text/html;charset=UTF-8"));
return new ResponseEntity<String>("you body",map , HttpStatus.OK);
}
您可能需要声明编码筛选器:
<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
编码滤波器
org.springframework.web.filter.CharacterEncodingFilter
编码
UTF-8
强制编码
真的
编码滤波器
/*
您可能需要声明编码筛选器:
<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
编码滤波器
org.springframework.web.filter.CharacterEncodingFilter
编码
UTF-8
强制编码
真的
编码滤波器
/*
- 首先要确认源代码是否正确,即
postService.getCategories()代码>实际上返回了正确的格式化数据
- 如果排除了第一种可能性,那么我的假设是您只需要在代码中添加
charset=UTF-8
@RequestMapping(method=RequestMethod.GET,products={“application/json;charset=UTF-8”})
- 如果以上2个不起作用,那么我建议使用GSON
- 首先要确认源代码是否正确,即
postService.getCategories()代码>实际上返回了正确的格式化数据
- 如果排除了第一种可能性,那么我的假设是您只需要在代码中添加
charset=UTF-8
@RequestMapping(method=RequestMethod.GET,products={“application/json;charset=UTF-8”})
- 如果以上2个不起作用,那么我建议使用GSON
- 我的问题与你的问题相似。
我使用了
string.getBytes(“UTF-8”)
请看我的问题与你的类似。 我使用了
string.getBytes(“UTF-8”)
请参见您是否可以共享其应为和当前为的示例数据?您是否可以共享用于显示此数据的angularjs代码?我添加了示例数据和angularjs代码。您是否可以共享其应为和当前为的示例数据?您是否可以共享用于显示此数据的angularjs代码?我添加了示例数据StringHttpMessageConverter WriteIner1类中的angularjs code.debug和StringHttpMessageConverter WriteIner1类中的angularjs code.debug。getCategories()返回正确的数据。2.您的建议需要Spring3.2+版本,但我使用的是Spring3.0.53。当我将POJO转换为JSON字符串时,我正在使用gson。getCategories()返回正确的数据。2.您的建议需要Spring3.2+版本,但我使用的是Spring3.0.53。当我将POJO转换为JSON字符串时,我正在使用gson。
@RequestMapping("/test22")
@ResponseBody
public ResponseEntity<String> test22(){
MultiValueMap<String, String> map=new LinkedMultiValueMap<>();
map.put("Content-Type", Lists.newArrayList("text/html;charset=UTF-8"));
return new ResponseEntity<String>("you body",map , HttpStatus.OK);
}
@Override
protected void writeInternal(String str, HttpOutputMessage outputMessage) throws IOException {
if (this.writeAcceptCharset) {
outputMessage.getHeaders().setAcceptCharset(getAcceptedCharsets());
}
Charset charset = getContentTypeCharset(outputMessage.getHeaders().getContentType());
StreamUtils.copy(str, charset, outputMessage.getBody());
}
<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>