JAXB Marshalling with null fields

后端 未结 5 1941
挽巷
挽巷 2020-12-05 04:19

This is a pretty simple request, but I just didn\'t find a way to do it.

I\'m basically trying to set up a role in JAXB which says that whenever an null field is enc

5条回答
  •  悲&欢浪女
    2020-12-05 04:35

    In MOXy u can specify how the jsonProvider must do its job for JAXB.

    So when doing JAX-RS, add following code in your class derived from Application

    I used this code on Tomcat 7 with good results. (eclipselink 2.4.1)

    @ApplicationPath("/rest")
    public class RestApplication extends Application
    {
    
      ...
    
     public Set< Object> getSingletons()
      {
    
        HashSet set = new HashSet(1);
        set.add( newMoxyJsonProvider());
    
        return set;
      }
    
    
     public static MOXyJsonProvider newMoxyJsonProvider()
      {
    
        MOXyJsonProvider result = new MOXyJsonProvider();
    
        //result.setAttributePrefix("@");
        result.setFormattedOutput( false);
        result.setIncludeRoot( false);
        result.setMarshalEmptyCollections( true);
        //result.setValueWrapper("$");
    
        return result;
      }
    
    
    

    On Glassfish 3.1.2 and WAS 8.5 however, newMoxyJsonProvider() is not needed, but then the JAXB provider gets configured by the server. In the case of Glassfish, which comes with MOXy, i witnessed same problems with null values. Did not check yet, but guess the answer is in configuring JAXB at application server level if possible at all.

    提交回复
    热议问题