我想将自定义MappingJackson2HttpMessageConverter添加到Spring Boot。它成功设置为转换器,但没有用它来转换......
我看到这个错误仅适用于4.3和4.3的弹簧。它在春季成功设定4.0.3
如何更正此转换器???
这是我的代码
public class ResponseViewEntity<T> extends
ResponseEntity<ContainerViewEntity<T>> {
private Class<? extends View.Base> view;
public ResponseViewEntity(HttpStatus statusCode) {
super(statusCode);
}
public ResponseViewEntity(T body, HttpStatus statusCode) {
super(new ContainerViewEntity<T>(body, View.Base.class), statusCode);
}
public ResponseViewEntity(T body, Class<? extends View.Base> view, HttpStatus statusCode) {
super(new ContainerViewEntity<T>(body, view), statusCode);
}
转换器:
public class JsonViewMessageConverter extends
MappingJackson2HttpMessageConverter {
private ObjectMapper objectMapper = new HibernateAwareObjectMapper();
protected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage)
throws IOException, HttpMessageNotReadableException {
JavaType javaType = getJavaType(clazz);
try {
return objectMapper.readValue(inputMessage.getBody(), javaType);
} catch (JsonProcessingException ex) {
throw new HttpMessageNotReadableException("Could not read JSON: "
+ ex.getMessage(), (Throwable) ex);
}
}
protected void writeInternal(Object object, HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException {
if (object instanceof ContainerViewEntity
&& ((ContainerViewEntity) object).hasView()) {
writeView((ContainerViewEntity) object, outputMessage);
} else {
super.writeInternal(object, outputMessage);
}
}
protected void writeView(ContainerViewEntity view,
HttpOutputMessage outputMessage) throws IOException,
HttpMessageNotWritableException {
JsonEncoding encoding = getJsonEncoding(outputMessage.getHeaders()
.getContentType());
ObjectWriter writer = getWriterForView(view.getView());
JsonGenerator jsonGenerator = writer.getFactory().createGenerator(
outputMessage.getBody(), encoding);
try {
writer.writeValue(jsonGenerator, view.getObject());
} catch (IOException ex) {
throw new HttpMessageNotWritableException("Could not write JSON: "
+ ex.getMessage(), (Throwable) ex);
}
}
private ObjectWriter getWriterForView(Class<?> view) {
objectMapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false);
return objectMapper.writer().withView(view);
}
protected JavaType getJavaType(Class<?> clazz) {
return TypeFactory.defaultInstance().constructType(clazz);
}
public ObjectMapper getObjectMapper() {
return objectMapper;
}
protected JsonEncoding getJsonEncoding(MediaType contentType) {
if (contentType != null && contentType.getCharset() != null) {
Charset charset = contentType.getCharset();
for (JsonEncoding encoding : JsonEncoding.values()) {
if (!charset.name().equals(encoding.getJavaName()))
continue;
return encoding;
}
}
return JsonEncoding.UTF8;
}
public void setObjectMapper(ObjectMapper objectMapper) {
Assert.notNull((Object) objectMapper,
(String) "ObjectMapper must not be null");
this.objectMapper = objectMapper;
}
和我的配置
@Bean
public JsonViewMessageConverter mappingJackson2HttpMessageConverter() {
JsonViewMessageConverter jsonConverter = new JsonViewMessageConverter();
ObjectMapper objectMapper = new HibernateAwareObjectMapper();
jsonConverter.setObjectMapper(objectMapper);
return jsonConverter;
}
@Override
public void configureMessageConverters(
List<HttpMessageConverter<?>> converters) {
converters.add(mappingJackson2HttpMessageConverter());
}