java 验证 JPA/JAX-RS Web 服务中的 JAXBElement
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3428273/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Validate JAXBElement in JPA/JAX-RS Web Service
提问by sdoca
I have a JAX-RS webservice (Jersey) that is a CRUD interface for JPA (EclipseLink) entities. My entities were autogenerated from the database tables and I have annotated them with JAXB annotations so that they can be marshalled/unmarshalled to/from XML. My resource methods take JAXBElement objects as a parameter where required.
我有一个 JAX-RS webservice (Jersey),它是 JPA (EclipseLink) 实体的 CRUD 接口。我的实体是从数据库表自动生成的,我已经用 JAXB 注释对它们进行了注释,以便可以将它们编组/解组为 XML。我的资源方法在需要时将 JAXBElement 对象作为参数。
I don't have an XSD, however, I'm willing to write one to validate the XML received in the requests. But, I don't know how to initiate the validation. Jersey is automatically handling the marshalling/unmarshalling and any references I've found about validation is done at that level.
我没有 XSD,但是,我愿意编写一个来验证请求中收到的 XML。但是,我不知道如何启动验证。Jersey 会自动处理编组/解组,我发现的任何有关验证的参考都是在该级别完成的。
Does someone know of an example/tutorial that shows how to do this?
有人知道展示如何执行此操作的示例/教程吗?
Thanks!
谢谢!
回答by bdoughan
You could handle this by creating a custom MessageBodyReader. The example below is based on a Customer model:
您可以通过创建自定义 MessageBodyReader 来处理此问题。下面的示例基于客户模型:
import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.net.URL;
import javax.ws.rs.Consumes;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.MessageBodyReader;
import javax.ws.rs.ext.Provider;
import javax.ws.rs.ext.Providers;
import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
@Provider
@Consumes("application/xml")
public class ValidatingReader implements MessageBodyReader<Customer> {
@Context
protected Providers providers;
private Schema schema;
public ValidatingReader() {
try {
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
URL schemaURL = null;
schema = sf.newSchema(schemaURL);
} catch(Exception e) {
throw new RuntimeException(e);
}
}
public boolean isReadable(Class<?> arg0, Type arg1, Annotation[] arg2, MediaType arg3) {
return arg0 == Customer.class;
}
public Customer readFrom(Class<Customer> arg0, Type arg1, Annotation[] arg2, MediaType arg3, MultivaluedMap<String, String> arg4, InputStream arg5)
throws IOException, WebApplicationException {
try {
JAXBContext jaxbContext = null;
ContextResolver<JAXBContext> resolver = providers.getContextResolver(JAXBContext.class, arg3);
if(null != resolver) {
jaxbContext = resolver.getContext(arg0);
}
if(null == jaxbContext) {
jaxbContext = JAXBContext.newInstance(arg0);
}
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
unmarshaller.setSchema(schema);
return (Customer) unmarshaller.unmarshal(arg5);
} catch(JAXBException e) {
throw new RuntimeException(e);
}
}
}
回答by GWTNewbie
We can go one step further and create a generic (abstract) ValidatingReader which can be sub-classed as and when needed. This is what I've done, thanks to the idea from Blaise:
我们可以更进一步,创建一个通用的(抽象的)ValidatingReader,它可以在需要时进行子类化。这就是我所做的,感谢 Blaise 的想法:
import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.MessageBodyReader;
import javax.ws.rs.ext.Providers;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
public abstract class AbstractValidatingReader<T> implements
MessageBodyReader<T> {
@Context
protected Providers providers;
@SuppressWarnings("unchecked")
@Override
public boolean isReadable(Class<?> arg0, Type arg1, Annotation[] arg2,
MediaType arg3) {
Class<T> readableClass = (Class<T>) ((ParameterizedType) getClass()
.getGenericSuperclass()).getActualTypeArguments()[0];
return arg0 == readableClass;
}
@SuppressWarnings("unchecked")
@Override
public T readFrom(Class<T> arg0, Type arg1, Annotation[] arg2,
MediaType arg3, MultivaluedMap<String, String> arg4,
InputStream arg5) throws IOException, WebApplicationException {
T type = null;
JAXBContext jaxbContext = null;
ContextResolver<JAXBContext> resolver = providers.getContextResolver(
JAXBContext.class, arg3);
try {
if (resolver != null) {
jaxbContext = resolver.getContext(arg0);
}
if (jaxbContext == null) {
jaxbContext = JAXBContext.newInstance(arg0);
}
type = (T) jaxbContext.createUnmarshaller().unmarshal(arg5);
validate(type);
} catch (JAXBException e) {
throw new WebApplicationException(
Response.Status.INTERNAL_SERVER_ERROR);
}
return type;
}
protected abstract void validate(T arg0) throws WebApplicationException;
}
Override the validate method and annotate the sub-class with @Provider and we're done.
覆盖验证方法并使用@Provider 注释子类,我们就完成了。

