如何向WADL添加其他元素?

时间:2016-05-26 14:24:57

标签: java jaxb jax-rs cxf wadl

我尝试将错误对象添加到响应中(使用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的语法中添加(仅用于文档)一个额外的元素?

1 个答案:

答案 0 :(得分:0)

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

<grammars>区域,您可以添加自定义XSD或链接一个

在CXF spring文件中定义WadlGenerator,并包含在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进行了测试

<强> EDITED

自定义WADL生成器

CXF WadlGenerator使用XSD,外部链接或自动生成的wadl填充'grammars'部分,但不允许将它们组合在一起。要创建自定义WadlGenerator

,需要向生成的语法添加外部资源链接
<grammars>
    <include href="http://books"/>
    <!-- The autogenerated grammar-->
</grammars>

这是CXF 2.7的全功能类,但我希望它可以在任何更高版本中使用,因为它使用继承并只重写代码的一小部分。它使用自动生成的代码或XSD

连接externalLinks(如果存在)
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 config

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