具有 JSON 表示语法的 WADL
WADL with grammar for JSON representation
我尝试为具有 JSON 表示的 JAX-B 对象自动生成带有语法的 WADL,但它无法完全工作。
代码:
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_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"/>
A customizing of the WADL generation is neccessary. The property linkJsonToXmlSchema
of the WadlGenerator
添加 link.
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
已弃用。
引用 Sergey Beryozkin at https://issues.apache.org/jira/browse/CXF-5479:
In 3.0.1 "linkJsonToXmlSchema" has been deprecated so the plugin supports a new "linkAnyMediaTypeToXmlSchema",
所以你应该使用:
<property name="linkAnyMediaTypeToXmlSchema" value="true" />
我尝试为具有 JSON 表示的 JAX-B 对象自动生成带有语法的 WADL,但它无法完全工作。
代码:
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_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"/>
A customizing of the WADL generation is neccessary. The property linkJsonToXmlSchema
of the WadlGenerator
添加 link.
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
已弃用。
引用 Sergey Beryozkin at https://issues.apache.org/jira/browse/CXF-5479:
In 3.0.1 "linkJsonToXmlSchema" has been deprecated so the plugin supports a new "linkAnyMediaTypeToXmlSchema",
所以你应该使用:
<property name="linkAnyMediaTypeToXmlSchema" value="true" />