我尝试为带有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"/>

最佳答案

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

关于java - WADL具有用于JSON表示的语法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36794232/

10-12 02:48