You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring REST API返回JSON时俄文字符编码异常问题

解决Spring REST端点返回JSON时俄文字符编码问题

看起来你遇到了Spring REST接口返回JSON时俄文字符乱码(显示为????)的问题,我来帮你分析下原因并给出几种靠谱的解决方案:

问题根源分析

你的代码里直接手动拼接了JSON字符串并返回ResponseEntity<String>,默认情况下Spring的StringHttpMessageConverter会使用ISO-8859-1编码处理字符串响应,而俄文字符不在这个编码的覆盖范围内,所以会被替换成问号。另外手动拼接JSON不仅容易出错,也不利于后续的维护和编码问题处理。


方案1:使用对象序列化(最推荐)

不要手动拼接JSON,而是定义对应的Java实体类,让Spring通过Jackson自动序列化对象为JSON,这样编码问题会被自动处理,代码也更简洁易维护。

首先定义实体类:

// RelatedParty.java
public class RelatedParty {
    private String role;
    private String name;

    // 构造器、getter、setter
    public RelatedParty(String role, String name) {
        this.role = role;
        this.name = name;
    }

    // 省略getter和setter方法
}

// Order.java
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Order {
    private String id;
    private String href;
    private String state;
    private String orderDate;
    private Map<String, Object> orderSpecification;
    private List<RelatedParty> relatedParty;

    // 构造器、getter、setter
    public Order(String id, String href, String state, String orderDate, List<RelatedParty> relatedParty) {
        this.id = id;
        this.href = href;
        this.state = state;
        this.orderDate = orderDate;
        this.orderSpecification = new HashMap<>();
        this.relatedParty = relatedParty;
    }

    // 省略getter和setter方法
}

然后修改控制器方法:

import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Collections;
import java.util.Map;

@RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Transactional
public ResponseEntity<Order> getOrder(@PathVariable String id, @RequestParam Map<String, String> params) {
    RelatedParty party = new RelatedParty("customer", "АГРО-СТАР");
    Order order = new Order(
        "composite_order2",
        "/orderManagement/2.0/serviceOrder/composite_order2",
        "Acknowledged",
        "2018-05-17T18:27:29+03:00",
        Collections.singletonList(party)
    );
    return new ResponseEntity<>(order, HttpStatus.OK);
}

这里用MediaType.APPLICATION_JSON_UTF8_VALUE明确指定UTF-8编码,Spring会自动用Jackson序列化对象,俄文字符就能正常显示了。


方案2:直接返回字符串时指定编码

如果你因为某些原因必须手动拼接JSON字符串,可以在produces属性里明确指定UTF-8编码,同时确保代码文件本身是UTF-8编码(IDE要设置文件编码为UTF-8):

@RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
@Transactional
public ResponseEntity<String> getOrder(@PathVariable String id, @RequestParam Map<String, String> params) {
    String result = "{\"id\":\"composite_order2\",\"href\":\"/orderManagement/2.0/serviceOrder/composite_order2\",\"state\":\"Acknowledged\",\"orderDate\":\"2018-05-17T18:27:29+03:00\",\"orderSpecification\":{},\"relatedParty\":[{\"role\":\"customer\",\"name\":\"АГРО-СТАР\"}]}";
    return new ResponseEntity<>(result, HttpStatus.OK);
}

方案3:全局配置消息转换器编码

如果想让所有REST接口都默认使用UTF-8编码,可以在Spring配置中自定义消息转换器:

import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.nio.charset.StandardCharsets;
import java.util.List;

@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void configureMessageConverters(List<org.springframework.http.converter.HttpMessageConverter<?>> converters) {
        // 配置字符串消息转换器为UTF-8
        StringHttpMessageConverter stringConverter = new StringHttpMessageConverter(StandardCharsets.UTF_8);
        converters.add(stringConverter);
        
        // 配置Jackson JSON转换器为UTF-8
        MappingJackson2HttpMessageConverter jacksonConverter = new MappingJackson2HttpMessageConverter();
        jacksonConverter.setDefaultCharset(StandardCharsets.UTF_8);
        converters.add(jacksonConverter);
    }
}

这样所有的字符串和JSON响应都会使用UTF-8编码,彻底解决编码问题。

内容的提问来源于stack exchange,提问作者Ankur Adhyapak

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:27:29