gpt4 book ai didi

java - 如何向 WADL 添加附加元素?

转载 作者:行者123 更新时间:2023-12-02 10:32:42 25 4
gpt4 key购买 nike

我尝试向响应添加错误对象(使用 HTTP status 422 )。它工作正常,但我还想将错误对象的方案添加到自动生成的 WADL 中。

代码:

JAX-B 模型类:

@XmlRootElement(namespace = "http://www.test.com/test")
@XmlAccessorType(value = XmlAccessType.FIELD)
public class UnprocessableEntityError {

@XmlElement
private String key;

public String getKey() {
return key;
}

public void setKey(final String key) {
this.key = key;
}
}

@XmlRootElement(namespace = "http://www.test.com/")
public class TestModel {
}

JAX-RS 资源类:

@Path("test")
public class TestResource {

@POST
public TestModel doSomething() {
throw new WebApplicationException("Error", Response.status(422).entity(new UnprocessableEntityError()).build());
}
}

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" />
<bean class="com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider" />
</jaxrs:providers>
</jaxrs:server>

WADL:

<?xml version="1.0"?>
<application xmlns:prefix1="http://www.test.com/" 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/" targetNamespace="http://www.test.com/" elementFormDefault="unqualified" attributeFormDefault="unqualified">
<xs:complexType name="testModel">
<xs:sequence/>
</xs:complexType>
</xs:schema>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.test.com/" targetNamespace="http://www.test.com/" elementFormDefault="unqualified" attributeFormDefault="unqualified">
<xs:import/>
<xs:element name="testModel" type="testModel"/>
</xs:schema>
</grammars>
<resources base="http://localhost:8080/test-app/services/rest/1">
<resource path="/test">
<method name="POST">
<response>
<representation mediaType="*/*" element="prefix1:testModel"/>
</response>
</method>
</resource>
</resources>
</application>

有没有办法向自动生成的 WADL 的语法中添加(仅用于文档)附加元素?

最佳答案

可以自定义WADL自动生成,但不太灵活。一般CXF文档是here但没有帮助。

<grammars>区域您可以包含自定义 XSD 或链接一个

定义 WadlGenerator在 CXF spring 文件中,并包含在 jaxrs 提供程序中。例如,使用 books.xsd

<jaxrs:server address="/rest" id="test" >
<jaxrs:providers>
<ref bean="wadlGenerator" />
</jaxrs:providers>
</jaxrs:server>

<bean id="wadlGenerator" class="org.apache.cxf.jaxrs.model.wadl.WadlGenerator">
<property name="schemaLocations" value="classpath:/books.xsd"/>
</bean>

也可以通过编程方式

WadlGenerator wg = new WadlGenerator();
wg.setSchemaLocations(Collections.singletonList("classpath:/books.xsd"));

生成的WADL将是这样的

<application xmlns="http://wadl.dev.java.net/2009/02" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<grammars>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://superbooks" attributeFormDefault="unqualified" elementFormDefault="unqualified" targetNamespace="http://superbooks">
<xs:element name="thebook" type="tns:book"/>
<xs:complexType name="book">
<xs:sequence>
<xs:element minOccurs="0" name="chapter" type="xs:string"/>
<xs:element name="id" type="xs:int"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
</grammars>

此外,您还可以添加链接

<bean id="wadlGenerator" class="org.apache.cxf.jaxrs.model.wadl.WadlGenerator">
<property name="externalLinks" value="http://books"/>
</bean>

WADL

<grammars>
<include href="http://books"/>
</grammars>

WadlGenerator 的完整文档。我已经用 CXF 2.7 进行了测试

已编辑

自定义 WADL 生成器

CXF WadlGenerator 使用 XSD、外部链接或自动生成的 wadl 填充“语法”部分,但不允许将它们组合起来。要将外部资源链接添加到生成的语法中,需要创建自定义 WadlGenerator

<grammars>
<include href="http://books"/>
<!-- The autogenerated grammar-->
</grammars>

这是 CXF 2.7 的功能齐全的类,但我希望它可以在任何更高版本中工作,因为使用继承并仅重写代码的最小部分。它连接 externalLinks (如果存在)使用自动生成的代码或 XSD

package com.wadl;

import java.net.URI;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Logger;

import javax.ws.rs.core.UriInfo;
import javax.xml.bind.JAXBContext;

import org.apache.cxf.common.logging.LogUtils;
import org.apache.cxf.jaxrs.model.ResourceTypes;
import org.apache.cxf.jaxrs.model.wadl.WadlGenerator;

public class CustomWadlGenerator extends WadlGenerator {
private List<URI> externalSchemaLinks;
private static final Logger LOG = LogUtils.getL7dLogger(CustomWadlGenerator.class);

//Overwrite setExternalLink so that it is not used in the superclass
@Override
public void setExternalLinks(List<String> externalLinks) {
externalSchemaLinks = new LinkedList<URI>();
for (String s : externalLinks) {
try {
String href = s;
if (href.startsWith("classpath:")) {
int index = href.lastIndexOf('/');
href = index == -1 ? href.substring(9) : href.substring(index + 1);

}
externalSchemaLinks.add(URI.create(href));
} catch (Exception ex) {
LOG.warning("Not a valid URI : " + s);
externalSchemaLinks = null;
break;
}
}
}


private class ExternalSchemaWriter implements WadlGenerator.SchemaWriter {

private List<URI>links;
private UriInfo uriInfo;
private SchemaWriter writer;

public ExternalSchemaWriter(List<URI>links, UriInfo ui, SchemaWriter writer){
this.links = links;
this.uriInfo = ui;
this.writer = writer;
}
public void write(StringBuilder sb) {
//write links
for (URI link : links) {
try {
URI value = link.isAbsolute() ? link : uriInfo.getBaseUriBuilder().path(link.toString()).build(new Object[0]);

sb.append("<include href=\"").append(value.toString()).append("\"/>");
} catch (Exception ex) {
CustomWadlGenerator.LOG.warning("WADL grammar section will be incomplete, this link is not a valid URI : " + link.toString());
}
}

//concat with default writer
writer.write(sb);
}
}

@Override
protected SchemaWriter createSchemaWriter(ResourceTypes resourceTypes, JAXBContext context, UriInfo ui) {
SchemaWriter schemaCollectionWriter = super.createSchemaWriter(resourceTypes, context, ui);

if (externalSchemaLinks == null){
//default behaviour
return schemaCollectionWriter;
} else {
//use custom writer
return new ExternalSchemaWriter(externalSchemaLinks,ui,schemaCollectionWriter);
}
}
}

在 spring 配置中设置 WadlGenerator

<bean id="wadlGenerator" class="com.wadl.CustomWadlGenerator">
<property name="externalLinks" value="http://books"/>
</bean>

关于java - 如何向 WADL 添加附加元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37463653/

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