com.github.developframework.jsonview.springmvc.JsonviewHttpMessageConverter Maven / Gradle / Ivy
package com.github.developframework.jsonview.springmvc;
import java.io.IOException;
import java.lang.reflect.Type;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonGenerator;
import com.github.developframework.jsonview.core.JsonCreator;
import com.github.developframework.jsonview.core.JsonviewFactory;
import com.github.developframework.jsonview.springmvc.res.JsonviewResponse;
/**
* jsonview framework HttpMessageConverter
*
* @author qiuzhenhao
*
*/
@Deprecated
public class JsonviewHttpMessageConverter extends MappingJackson2HttpMessageConverter {
private static final Logger logger = LoggerFactory.getLogger(JsonviewHttpMessageConverter.class);
protected JsonviewFactory jsonviewFactory;
public JsonviewHttpMessageConverter() {
logger.warn("JsonviewHttpMessageConverter is deprecated, please use JsonviewResponseReturnValueHandler after version 3.1.0.");
}
public JsonviewHttpMessageConverter(JsonviewFactory jsonviewFactory) {
this();
this.jsonviewFactory = jsonviewFactory;
super.objectMapper = jsonviewFactory.getObjectMapper();
}
@Override
public boolean canWrite(Class> clazz, MediaType mediaType) {
return JsonviewResponse.class.isAssignableFrom(clazz);
}
@Override
public boolean canWrite(Type type, Class> clazz, MediaType mediaType) {
return this.canWrite(clazz, mediaType);
}
@Override
protected void writeInternal(Object object, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
JsonviewResponse jsonviewResponse = (JsonviewResponse) object;
JsonCreator creator = jsonviewFactory.getJsonCreator();
JsonEncoding encoding = super.getJsonEncoding(outputMessage.getHeaders().getContentType());
JsonGenerator generator = super.objectMapper.getFactory().createGenerator(outputMessage.getBody(), encoding);
creator.printJson(generator, jsonviewResponse.getDataModel(), jsonviewResponse.getNamespace(), jsonviewResponse.getJsonviewId());
}
@Override
protected void writeInternal(Object object, Type type, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
this.writeInternal(object, outputMessage);
}
public void setJsonviewFactory(JsonviewFactory jsonviewFactory) {
this.jsonviewFactory = jsonviewFactory;
super.objectMapper = jsonviewFactory.getObjectMapper();
}
public JsonviewFactory getJsonviewFactory() {
return jsonviewFactory;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy