SPRINGMVC Unified Conversion Null value is an empty string method __null value Conversion

Source: Internet
Author: User
Tags java web

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:







Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.