Moxy ignores invalid fields in json

When I submit this request:

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

to my leisure service:

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

I expected to get an exception because:

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

But actually I get a test object with null values. My dmain model:

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

I think this is implausible behavior. How can i fix this?

Thanks for the help.

Decision:

, MOXy MOXyJsonProvider preReadFrom javax.xml.bind.ValidationEventHandler. , Jersey ConfigurableMoxyJsonProvider , MessageBodyWriter/MessageBodyReader, , Object. , MOXy, CustomMoxyJsonProvider.

:

  • , 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;
        }
    }
    
  • , 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;
            }
        });
    }
    

    }

  • :

    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);
        }
    }
    
+3
1

MOXy , . javax.xml.bind.ValidationEventHandler. ValidationEventHandler, Unmarshaller, .

MesageBodyReader/MessageBodyWriter, MOXy MOXyJsonProvider preReadFrom .

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

All Articles