SPRING MVC JSON null idling "" empty string

Source: Internet
Author: User

The workaround is not the same as on the Web because Serializerprovider is the parent class for Stdserializerprovider, the direct from the Serializerprovider. Setnullvalueserializer () method does not allow So call this method from subclass Stdserializerprovider

Applicationcontext.xml


<bean
class= "Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >
<property name= "Messageconverters" >
<list>
<bean class= "Org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" >
<property name= "Supportedmediatypes" >
<list>
<value>text/plain;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
<property name= "Objectmapper" >
<bean class= "Com.interceptor.ObjectMappingCustomer" ></bean>
</property>
</bean>
</list>
</property>
</bean>


Objectmappingcustomer.java

    package com.interceptor;             import java.io.IOException;import org.codehaus.jackson.JsonGenerator;import  org.codehaus.jackson.jsonparser;import org.codehaus.jackson.jsonprocessingexception;import  org.codehaus.jackson.map.jsonserializer;import org.codehaus.jackson.map.objectmapper;import  org.codehaus.jackson.map.serializerprovider;import org.codehaus.jackson.map.ser.stdserializerprovider;           public class ObjectMappingCustomer  extends objectmapper      {                 public objectmappingcustomer ()            {               super ();              //  Allow single quotes                this.configure (JsonParser.Feature.ALLOW_SINGLE_QUOTES,  true);              //  Fields and values are quoted               this.configure ( Jsonparser.feature.allow_unquoted_field_names, true);               //  numbers are also quoted                this.configure (jsongenerator.feature.write_numbers_as_strings, true);               this.configure ( Jsongenerator.feature.quote_non_numeric_numbers, true);              //  null processing is empty string               stdserializerprovider sp  =  (Stdserializerprovider)  this.getserializerprovider ();             sp.setnullvalueserializer (new jsonserializer<object> () {                  @Override                  public void  serialize (object value, jsongenerator jg,                          SERIALIZERPROVIDER&NBSP;SP)  throws IOException,                          jsonprocessingexception {                     jg.writestring ("");                   }                             });               }      }


And then add @ResponseBody Note to SPRINGMVC Controller.

Need Jackson-all.jar


SPRING MVC JSON null idling "" empty string

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.