使用 JAX-B 对象作为参数验证 JAX-RS 请求
Validation of JAX-RS request with JAX-B object as parameter
我尝试使用 JAX-B 对象作为参数来验证 JAX-RS 请求。
代码:
JAX-B 模型 class:
@XmlRootElement(namespace = "http://www.test.com/test")
@XmlAccessorType(value = XmlAccessType.FIELD)
public class TestModel {
@XmlElement(required = true)
private String id;
@XmlElement
private String name;
}
JAX-RS 资源class:
@Path("test")
public class TestResource {
@POST
@Consumes({ MediaType.APPLICATION_XML, MediaType.TEXT_XML })
public void create(TestModel testModel) {
// some code
}
}
CXF 配置:
<jaxrs:server address="/rest" id="test" staticSubresourceResolution="true">
<jaxrs:serviceBeans>
<ref bean="testResource" />
</jaxrs:serviceBeans>
<jaxrs:providers>
<bean class="org.apache.cxf.jaxrs.provider.JAXBElementProvider" />
</jaxrs:providers>
</jaxrs:server>
示例:
请求正文:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:testModel xmlns:ns2="http://www.test.com/test">
<name>testName</name>
</ns2:testModel>
缺少id
,所以我应该得到一个HTTP status 400, but I get HTTP status 204。
研究:
我发现 Schema validation:
- Using jaxrs:schemaLocations element
[...]
- Configuring providers individually
[...]
- Using SchemaValidation annotation
但我没有 XSD 文件(只有 JAX-B classes)。
有没有办法在没有 XSD 文件的情况下验证 JAX-B 对象?
一个糟糕的解决方法是使用 Maven 生成 XSD 文件:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<goals>
<goal>schemagen</goal>
</goals>
<phase>generate-resources</phase>
<configuration>
<includes>
<include>*.java</include>
</includes>
<outputDirectory>${basedir}/src/main/resources/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
并将 XSD 个文件添加到 CXF 配置:
<jaxrs:schemaLocations>
<jaxrs:schemaLocation>classpath:schema1.xsd</jaxrs:schemaLocation>
<jaxrs:schemaLocation>classpath:schema2.xsd</jaxrs:schemaLocation>
</jaxrs:schemaLocations>
我尝试使用 JAX-B 对象作为参数来验证 JAX-RS 请求。
代码:
JAX-B 模型 class:
@XmlRootElement(namespace = "http://www.test.com/test")
@XmlAccessorType(value = XmlAccessType.FIELD)
public class TestModel {
@XmlElement(required = true)
private String id;
@XmlElement
private String name;
}
JAX-RS 资源class:
@Path("test")
public class TestResource {
@POST
@Consumes({ MediaType.APPLICATION_XML, MediaType.TEXT_XML })
public void create(TestModel testModel) {
// some code
}
}
CXF 配置:
<jaxrs:server address="/rest" id="test" staticSubresourceResolution="true">
<jaxrs:serviceBeans>
<ref bean="testResource" />
</jaxrs:serviceBeans>
<jaxrs:providers>
<bean class="org.apache.cxf.jaxrs.provider.JAXBElementProvider" />
</jaxrs:providers>
</jaxrs:server>
示例:
请求正文:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:testModel xmlns:ns2="http://www.test.com/test">
<name>testName</name>
</ns2:testModel>
缺少id
,所以我应该得到一个HTTP status 400, but I get HTTP status 204。
研究:
我发现 Schema validation:
- Using jaxrs:schemaLocations element
[...]
- Configuring providers individually
[...]
- Using SchemaValidation annotation
但我没有 XSD 文件(只有 JAX-B classes)。
有没有办法在没有 XSD 文件的情况下验证 JAX-B 对象?
一个糟糕的解决方法是使用 Maven 生成 XSD 文件:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<goals>
<goal>schemagen</goal>
</goals>
<phase>generate-resources</phase>
<configuration>
<includes>
<include>*.java</include>
</includes>
<outputDirectory>${basedir}/src/main/resources/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
并将 XSD 个文件添加到 CXF 配置:
<jaxrs:schemaLocations>
<jaxrs:schemaLocation>classpath:schema1.xsd</jaxrs:schemaLocation>
<jaxrs:schemaLocation>classpath:schema2.xsd</jaxrs:schemaLocation>
</jaxrs:schemaLocations>