Moxy ignore invalid fields in json

不打扰是莪最后的温柔 提交于 2019-12-07 10:08:15

问题


When I send this request:

{"invalidField": "value", "date": "value"}

to my rest service:

@PUT
@Consumes("application/json")
public void putJson(Test content) {
    System.out.println(content.toString());
}

I expected to get an exception because:

  1. There is no invalidField in my domain model.
  2. Date format is not valid.

But really I get test object with null values. My dmain model is:

public class Test {
    private String name;
    private Date date; 
    //getters and setters here  
}

I think this is not a valid behavior. How can I fix that?

Thanks for help.

Solution:

As Blaise Doughan said, it is required to extend MOXy's MOXyJsonProvider and override the preReadFrom method to set custom javax.xml.bind.ValidationEventHandler. But the problem is that Jersey's ConfigurableMoxyJsonProvider will always be picked first, unless you write a MessageBodyWriter/MessageBodyReader that is parameterized with a more specific type than Object. To solve this problem it is necessary to disable MOXy and then enable CustomMoxyJsonProvider.

Example:

  1. Create your own feature that extends javax.ws.rs.core.Feature:

    @Provider
    public class CustomMoxyFeature implements Feature {
        @Override
        public boolean configure(final FeatureContext context) {
            final String disableMoxy = CommonProperties.MOXY_JSON_FEATURE_DISABLE + '.' + context.getConfiguration().getRuntimeType().name().toLowerCase();
            context.property(disableMoxy, true);
           return true;
        }
    }
    
  2. Create your own provider that extends MOXyJsonProvider:

    @Provider
    public class CustomMoxyJsonProvider extends MOXyJsonProvider {
        @Override
        protected void preReadFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, Unmarshaller unmarshaller) throws JAXBException {
        unmarshaller.setEventHandler(new ValidationEventHandler() {
            @Override
            public boolean handleEvent(ValidationEvent event) {
                return false;
            }
        });
    }
    

    }

  3. Add this resources in Application config:

    package com.vinichenkosa.moxyproblem;
    
    import java.util.Set;
    import javax.ws.rs.core.Application;
    
    @javax.ws.rs.ApplicationPath("webresources")
    public class ApplicationConfig extends Application {
    
        @Override
        public Set<Class<?>> getClasses() {
            Set<Class<?>> resources = new java.util.HashSet<>();
            addRestResourceClasses(resources);
            return resources;
        }
    
        private void addRestResourceClasses(Set<Class<?>> resources) {
            resources.add(com.vinichenkosa.moxyproblem.TestResource.class);
            resources.add(com.vinichenkosa.moxyproblem.custom_provider.CustomMoxyFeature.class);
            resources.add(com.vinichenkosa.moxyproblem.custom_provider.CustomMoxyJsonProvider.class);
        }
    }
    

回答1:


MOXy will report information about invalid property values, but by default it does not fail on them. The reporting is done to an instance of javax.xml.bind.ValidationEventHandler. You can override the ValidationEventHandler set on the Unmarshaller to do this.

You can create your own MesageBodyReader/MessageBodyWriter that extends MOXy's MOXyJsonProvider and override the preReadFrom method to do this.

@Override
protected void preReadFrom(Class<Object> type, Type genericType,
        Annotation[] annotations, MediaType mediaType,
        MultivaluedMap<String, String> httpHeaders,
        Unmarshaller unmarshaller) throws JAXBException {
    unmarshaller.setEventHandler(yourValidationEventHandler);
}


来源:https://stackoverflow.com/questions/27658173/moxy-ignore-invalid-fields-in-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!