In the Java Web, if the value in the database is null without any conversion, uploading to the front-end page will appear null and affect the appearance. For example, this looks like this on the Zhaopin website:
In Springmvc, you can solve this problem by configuring <mvc:message-converters> in <mvc:annotation-driven>, and converting null values uniformly into empty strings. The following example of JSON interaction illustrates how to:
First step: Create a Objectmapper
Package com.xjj.anes.mvc.converter;
Import java.io.IOException;
Import Com.fasterxml.jackson.core.JsonGenerator;
Import com.fasterxml.jackson.core.JsonProcessingException;
Import Com.fasterxml.jackson.databind.JsonSerializer;
Import Com.fasterxml.jackson.databind.ObjectMapper;
Import Com.fasterxml.jackson.databind.SerializerProvider;
/**
* @description: Convert null object to empty string
*/Public
class Jsonobjectmapper extends Objectmapper {
private Static final Long serialversionuid = 1L;
Public Jsonobjectmapper () {
super ();
Null-value processing is an empty string
this.getserializerprovider (). Setnullvalueserializer (New jsonserializer<object> () {
@ Override public
void Serialize (Object value, Jsongenerator JG, Serializerprovider sp) throws IOException, jsonprocessingexception {
jg.writestring ("");}}
);
}
Step Two:In the SPRINGMVC configuration file, inject the new objectmapper into the Mappingjackson2httpmessageconverter
<!--register requestmappinghandlermapping and requestmappinghandleradapter two beans. -->
<mvc:annotation-driven>
<mvc:message-converters>
<bean class= " Org.springframework.http.converter.json.MappingJackson2HttpMessageConverter ">
<property name=" Objectmapper ">
<bean class=" Com.xjj.anes.mvc.converter.JsonObjectMapper "></bean>
</ property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
After restarting the server, you can see the effect.
Before conversion:
After conversion: