Moxy忽略JSON中的无效字段 [英] Moxy ignore invalid fields in json
问题描述
当我发送此请求时:
{"invalidField": "value", "date": "value"}
我的休息服务:
@PUT
@Consumes("application/json")
public void putJson(Test content) {
System.out.println(content.toString());
}
我希望得到一个例外,因为:
I expected to get an exception because:
- 我的域模型中没有invalidField.
- 日期格式无效.
但是实际上我得到的测试对象具有空值.我的主要模型是:
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?
感谢您的帮助.
正如Blaise Doughan所说,需要扩展MOXy的MOXyJsonProvider并重写preReadFrom方法以设置自定义javax.xml.bind.ValidationEventHandler.但是问题在于,除非您编写一个MessageBodyWriter/MessageBodyReader参数化的类型比Object更具体的类型,否则将始终首先选择Jersey的ConfigurableMoxyJsonProvider.若要解决此问题,必须禁用MOXy,然后启用CustomMoxyJsonProvider.
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.
示例:
-
创建您自己的扩展javax.ws.rs.core.Feature的功能:
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;
}
}
创建您自己的扩展MOXyJsonProvider的提供程序:
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;
}
});
}
}
在应用程序配置中添加此资源:
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);
}
}
推荐答案
MOXy将报告有关无效属性值的信息,但是默认情况下,它不会失败.报告是针对 javax.xml.bind.ValidationEventHandler
的实例完成的.您可以覆盖Unmarshaller
上设置的ValidationEventHandler
来完成此操作.
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.
您可以创建自己的MesageBodyReader
/MessageBodyWriter
,以扩展MOXy的MOXyJsonProvider
并覆盖preReadFrom
方法来执行此操作.
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);
}
这篇关于Moxy忽略JSON中的无效字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!