[英]Spring Controller @RequestMapping does not support for non English special characters
I am currently working on an old spring app which exposes REST API for mobile devices. 我目前正在使用旧的Spring应用程序,该应用程序公开了用于移动设备的REST API。 I need to create a new REST service for a Json request with multiple language support.
我需要为具有多种语言支持的Json请求创建一个新的REST服务。 But when I testing this it only support English.
但是,当我对此进行测试时,它仅支持英语。 When I try with an other language it gives me ?????
当我尝试其他语言时,它会给我?????? characters for relevant data.
相关数据的字符。
following is my configuration class 以下是我的配置类
@Configuration
@EnableScheduling
@ComponentScan(basePackages = "lk.test.com.controller")
public class ControllerConfig extends WebMvcConfigurationSupport {
private static final Logger log = LoggerFactory.getLogger(ControllerConfig.class);
@Override
public void configureMessageConverters(final List<HttpMessageConverter<?>> converters) {
converters.add(getMappingJackson2HttpMessageConverter());
addDefaultHttpMessageConverters(converters);
}
@Bean
public MappingJackson2HttpMessageConverter getMappingJackson2HttpMessageConverter() {
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setObjectMapper(getObjectMapper());
List<MediaType> mediaTypes = new ArrayList<MediaType>();
mediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
converter.setSupportedMediaTypes(mediaTypes);
return converter;
}
@Bean
public ObjectMapper getObjectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
objectMapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
return objectMapper;
}
}
Following is a sample controller method 以下是示例控制器方法
@RequestMapping(value = "send/push", method = RequestMethod.POST,consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
@ResponseBody
public Response sendPush(final @RequestBody CustomerExternalRequest request, HttpServletRequest servletRequest) {
System.out.println(request.getPushMessage());
Response response = new Response();
return response;
}
and Relevant Request Object 和相关请求对象
@Getter
@Setter
@ToString
public class CustomerExternalRequest extends Request{
private String phoneNo;
//push send
private Boolean isOnlyToCurrenltyLoggedDevice;
private String pushMessage;
private String pushTitle;
}
my Json Request 我的Json请求
{"request":{"phoneNo":"+94776587745","isOnlyToCurrenltyLoggedDevice":true,"pushMessage":"测试","pushTitle":"测试"}}
And this is my POSTMAN request 这是我的邮递员要求
System.out.println(request);
prints ?? 打印?? in the console
在控制台中
Can any one know how to solve it? 有人知道如何解决吗? Thanks.
谢谢。
Finally found the reason and the solution. 终于找到了原因和解决方法。
When I tried with a spring boot sample app it worked. 当我尝试使用Spring Boot示例应用程序时,它可以工作。 Because it is has an embedded tomcat server.
因为它具有嵌入式的tomcat服务器。 In my case I used eclipse jetty plugin.
就我而言,我使用了Eclipse Jetty插件。 I then found a place to change encoding type and changed it to UTF-8 and now it works fine.
然后,我找到了一个更改编码类型的地方,并将其更改为UTF-8,现在可以正常使用了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.