I am currently working on an old spring app which exposes REST API for mobile devices. I need to create a new REST service for a Json request with multiple language support. 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
{"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. Because it is has an embedded tomcat server. In my case I used eclipse jetty plugin. I then found a place to change encoding type and changed it to UTF-8 and now it works fine.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.