gpt4 book ai didi

java - 具有 JSON 表示语法的 WADL

转载 作者:行者123 更新时间:2023-12-01 10:01:11 24 4
gpt4 key购买 nike

我尝试使用 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是必要的。属性(property)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 - 具有 JSON 表示语法的 WADL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36794232/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com