简体   繁体   English

SpringBoot 响应字符集错误

[英]SpringBoot response charset errors

i got some problems with my SpringBoot REST Controller.我的 SpringBoot REST 控制器出现了一些问题。 This simply does a http GET call to our database and should return a simple String / json.这只是对我们的数据库进行 http GET 调用,并且应该返回一个简单的字符串/json。 when i call the URL simply in my browser or via my angular 3 app, the response has some charset errors and i don't know, how to fix them.当我只是在浏览器中或通过我的 angular 3 应用程序调用 URL 时,响应有一些字符集错误,我不知道如何修复它们。 I suggest, it is a UTF-8 problem.我建议,这是一个 UTF-8 问题。

First to show you the output: this is how it comes from the Controller: MeinekestraÃe and it should be Meinekestraße首先向您展示输出:这是来自控制器的方式: MeinekestraÃe ,它应该是Meinekestraße

here is a part of my SpringBoot Controller:这是我的 SpringBoot 控制器的一部分:

@Controller
public class RecieverController {

@Value("${server}")
private String server;

@Value("${user.token}")
private String token;   

@RequestMapping(value="/reciever", method=RequestMethod.GET, produces = "text/plain;charset=UTF-8")
@ResponseBody
@CrossOrigin(origins = "http://localhost:4200", maxAge = 3600)
public String getRecieverData(
        @RequestHeader(value="Accept") String accept,
        @RequestHeader(value="Host") String host) {

    final String url = server + "/rest/client/profile";

    RestTemplate restTemplate = new RestTemplate();
    HttpHeaders headers = new HttpHeaders();

    headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
    headers.set("Auth-Token", token); // user_token

    HttpEntity<String> entity = new HttpEntity<String>("parameters", headers);

    ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, entity, String.class);

    return response.getBody();
}}

I tried the following things, but nothing changed in the output.我尝试了以下操作,但输出中没有任何变化。

spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true

or this或者这个

RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters()
    .add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));

any other ideas what could be the problem?任何其他想法可能是什么问题? the database isnt the issue.数据库不是问题。 Everything is stored correctly there.一切都正确存储在那里。

Edit: this is the screenshot of the header from the output编辑:这是输出中标题的屏幕截图在此处输入图片说明 and a part from the json output:以及 json 输出的一部分: 在此处输入图片说明

The Problem could be solved by adding both可以通过添加两者来解决问题

spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true

and this而这

RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters()
    .add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));

Thanks @dienerd for helping me via chat感谢@dienerd 通过聊天帮助我

For the ones looking for a way to force encoding request/response in @RestController in a Spring Boot project.对于那些在 Spring Boot 项目中寻找在 @RestController 中强制编码请求/响应的方法的人。

spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true

is deprecated, in your application.yaml or application.properties use the following:已弃用,在您的 application.yaml 或 application.properties 中使用以下内容:

server.servlet.encoding.charset=UTF-8
server.servlet.encoding.force=true

This did the trick for me.这对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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