java Jackson 对象映射器在响应中包含转义序列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28134391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hymanson object mapper includes escape sequence in responses
提问by Rithik_Star
When I use object mapper, it inluces \r\n in the responses.Help me how to resolve it. I am having train POJO and it has String name and String Value. I set name as "Sydney" and Value as "SYD".It reruns
当我使用对象映射器时,它会在响应中包含 \r\n。帮助我如何解决它。我有火车 POJO,它有字符串名称和字符串值。我将名称设置为“悉尼”,将值设置为“SYD”。它重新运行
{\ \ \"name \" : \"Sydney\",\ \ \"Value \" : \"SYD\",\ \ \"isEnable\" : false,\ \ \"isCurrent\" : false\ \ }"
raw value in browser
浏览器中的原始值
"{\r\n \"name\" : \"Sydney\",\r\n \"name\" : \"SYD\",\r\n \"isEnable\" : false,\r\n \"isCurrent\" : false\r\n}"
below is my code Train
下面是我的代码火车
public class Train {
public Train() {
}
private String name;
private String value;
private String Code;
private String countryName;
private String state;
private String stateName;
private boolean isEnable;
private boolean isCurrent;
//*getters and setters/*/
}
Controller calss
控制器类
public ResponseEntity<String> getDetails( )
throws IOException {
ResponseEntity<String> responseEntity = null;
try(StringWriter writer = new StringWriter()) {
ObjectMapper mapper = new ObjectMapper();
Train train = new Train();
// set name and value to the train object//
if(train != null)
{
mapper.setSerializationInclusion(Inclusion.NON_NULL);
mapper.setSerializationInclusion(Inclusion.NON_EMPTY);
mapper.writerWithDefaultPrettyPrinter().writeValue(writer,
train);
responseEntity = new ResponseEntity<>(writer.toString(),
HttpStatus.OK);
}
}
catch()
{}
return responseEntity;
}
Configuration:
配置:
@Override
public void configureMessageConverters(
List<HttpMessageConverter<?>> converters) {
converters.add(extendedJsonConvertor());
super.configureMessageConverters(converters);
}
@Bean
public MappingHymanson2HttpMessageConverter extendedJsonConvertor() {
MappingHymanson2HttpMessageConverter mappingHymanson2HttpMessageConverter = new MappingHymanson2HttpMessageConverter();
mappingHymanson2HttpMessageConverter
.setObjectMapper(getNullAndEmptyFilteredObjectMapper());
return mappingHymanson2HttpMessageConverter;
}
@Bean
public ObjectMapper getNullAndEmptyFilteredObjectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
objectMapper.configure(
DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
return objectMapper;
}
When I debug the above code I came to know mapper include those \r\n in the response.Help me how to remove those slashes.
当我调试上面的代码时,我开始知道映射器在响应中包含了那些 \r\n。帮助我如何删除这些斜线。
回答by Haim Raman
The problem is the line below.
问题是下面的行。
mapper.writerWithDefaultPrettyPrinter().writeValue(writer,train);
Try replacing it with
尝试将其替换为
mapper.writeValue(writer,train);
Why do you create a new object mapper when you are configuring, MappingHymanson2HttpMessageConverter?
You can autowire the object mapper or return the actual object and let spring convert it to json
为什么在配置的时候新建一个对象映射器,MappingHymanson2HttpMessageConverter?
您可以自动装配对象映射器或返回实际对象并让 spring 将其转换为 json
回答by Martin Frey
That's a "simple" double encoding issue i believe. You set a string in the response entity which is again writen as a json response.
我相信这是一个“简单”的双重编码问题。您在响应实体中设置一个字符串,该字符串再次被写入为 json 响应。
If you want to rely on the spring view rendering (mappingHymanson2httpmessageconverter) you have to create a response entity for "Train". (Or return a train instance directly from your controller method)
如果您想依赖 spring 视图渲染(mappingHymanson2httpmessageconverter),您必须为“火车”创建一个响应实体。(或直接从您的控制器方法返回列车实例)
Or you use the way you implemented it and you have to ensure that rendering a string for a json response will not use the Hymanson message converter, but is left untouched by spring.
或者你使用你实现它的方式,你必须确保为 json 响应呈现字符串不会使用 Hymanson 消息转换器,但不会被 spring 影响。