我尝试使用带有JSON表示的JAX-B对象自动生成带有语法的WADL,但它不能完全正常工作。
代码:
JAX-B模型类:
@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资源类:
@Path("test")
public class TestResource {
@POST
@Consumes(MediaType.APPLICATION_JSON)
public void create(TestModel testModel) {
// some code
}
}
CXF配置:
<jaxrs:server address="/rest/1" id="test" staticSubresourceResolution="true">
<jaxrs:serviceBeans>
<ref bean="testResource" />
</jaxrs:serviceBeans>
<jaxrs:providers>
<bean class="com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider" />
</jaxrs:providers>
</jaxrs:server>
WADL:
<?xml version="1.0"?>
<application xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://wadl.dev.java.net/2009/02">
<grammars>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.test.com/test" targetNamespace="http://www.test.com/test" elementFormDefault="unqualified" attributeFormDefault="unqualified">
<xs:import/>
<xs:element type="testModel" name="testModel"/>
</xs:schema>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.test.com/test" targetNamespace="http://www.test.com/test" elementFormDefault="unqualified" attributeFormDefault="unqualified">
<xs:complexType name="testModel">
<xs:sequence>
<xs:element type="xs:string" name="id"/>
<xs:element type="xs:string" name="name" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
</grammars>
<resources base="http://localhost:8080/test-app/services/rest/1">
<resource path="/test">
<method name="POST">
<request>
<representation mediaType="application/json"/>
</request>
<response status="204"/>
</method>
</resource>
</resources>
</application>
生成语法,但我错过了元素引用,例如:
<representation mediaType="application/json" element="testModel"/>
答案 0 :(得分:1)
需要customizing of the WADL generation。 linkJsonToXmlSchema
的属性WadlGenerator
会添加链接。
CXF配置:
<bean id="wadlGenerator" class="org.apache.cxf.jaxrs.model.wadl.WadlGenerator">
<property name="linkJsonToXmlSchema" value="true" />
</bean>
<jaxrs:server address="/rest/1" id="test" staticSubresourceResolution="true">
<jaxrs:serviceBeans>
<ref bean="testResource" />
</jaxrs:serviceBeans>
<jaxrs:providers>
<bean class="com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider" />
<ref bean="wadlGenerator" />
</jaxrs:providers>
</jaxrs:server>
另请参阅:CXF – Missing WADL method parameter element types with JSON JAX-RS services
<强>更新强>
由于不推荐使用3.1.x linkJsonToXmlSchema
。
答案 1 :(得分:0)
在Sergey Beryozkin处引用https://issues.apache.org/jira/browse/CXF-5479:
在3.0.1中,“linkJsonToXmlSchema”已被弃用,因此该插件支持新的“linkAnyMediaTypeToXmlSchema”,
所以你应该使用:
<property name="linkAnyMediaTypeToXmlSchema" value="true" />