生成资源列表的WADL

时间:2013-03-29 11:02:24

标签: list jaxb jersey jax-rs wadl

我现在因为以下问题而苦苦挣扎了几天。我搜索了很多答案,在SO中,在泽西邮件列表和网络中,但是无法找到这个特定问题的答案。

设置问题域...

我正在使用 Tomcat 7中的泽西岛1.16。

我创建了一个简单的JAX-RS资源,如下所示:

@Path("/")
@Produces({ "application/xml", "text/plain" })
public class ExampleResource {

    @GET
    public List<Thing> getThings() {
        List<Thing> list = new ArrayList<>();
        list.add(new Thing("a thing 1", "a thing description 1"));
        list.add(new Thing("a thing 2", "a thing description 2"));

        return list;
    }

}

Thing是一个JAXB注释的POJO,看起来像这样

        @XmlRootElement(name = "thing")
        public class Thing {
            private String name;        
            private String description;
// getters, setters and @XmlElement annotations ommited for brevity

我还配置了WadlGeneratorJAXBGrammarGenerator.class

当我要求GET http://localhost:8092/rest时,它就像一个魅力 - 返回了Thing格式良好的集合。

自动生成的WADL http://localhost:8092/rest/application.wadl几乎是完美的,它看起来像这样:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<application xmlns="http://wadl.dev.java.net/2009/02">
    <doc xmlns:jersey="http://jersey.java.net/" jersey:generatedBy="Jersey: 1.16 11/28/2012 02:09 PM" />
    <grammars>
        <include href="application.wadl/xsd0.xsd">
            <doc title="Generated" xml:lang="en" />
        </include>
    </grammars>
    <resources base="http://localhost:8092/rest/">
        <resource path="/">
            <method id="getThings" name="GET">
                <response>
                    <ns2:representation xmlns:ns2="http://wadl.dev.java.net/2009/02"
                        xmlns="" element="thing" mediaType="application/xml" />
                    <representation mediaType="text/plain" />
                </response>
            </method>
        </resource>
    </resources>
</application>
像我说的那样,几乎完美,其中存在问题。

<ns2:representation xmlns:ns2="http://wadl.dev.java.net/2009/02"
                            xmlns="" element="thing" mediaType="application/xml" />

WADL没有描述/getThings返回List<Thing>。 相反,看起来它指的是thing中的单个元素xsd0.xsd。 所以,当我喂它时,例如wadl2java,它生成无类型客户端。 为了获得List<Thing>,我必须手动编码,例如

List<Thing> asXml = root().getAsXml(new GenericType<List<Thing>>(){});

是否有人知道是否有可能生成自动WADL,以某种方式表明此特定资源正在返回特定类型的列表资源?

想要创建额外的“ThingList”JAXB注释类,并将其返回到我的jersey资源中。

我几乎在那里产生了“完美的”WADL,这就是我希望失去的这个(希望)小片......

非常感谢!

1 个答案:

答案 0 :(得分:4)

我遇到了同样的问题并通过生成我自己的WADL来解决它。

为此,您需要将以下文件添加到项目中

application-doc.xml,用于高级WADL概述评论

application-grammers.xml,它定义了模式的位置(包含你的Things和Thing元素以及复杂类型)

resourcedoc.xml,这是由maven插件生成的,它会读取你的jersey类,其中包含你的响应元素javadoc注释。

只需将此HrWadlGeneratorConfig类添加到项目中,并将其作为init param添加到jersey servlet

    <init-param>
        <param-name>com.sun.jersey.config.property.WadlGeneratorConfig</param-name>
        <param-value>nl.amis.hr.wadl.HrWadlGeneratorConfig</param-value>
    </init-param> 

 package nl.amis.hr.wadl;                                                                                        

 import com.sun.jersey.api.wadl.config.WadlGeneratorConfig;                                                      
 import com.sun.jersey.api.wadl.config.WadlGeneratorDescription;                                                 
 import com.sun.jersey.server.wadl.generators.WadlGeneratorApplicationDoc;                                       
 import com.sun.jersey.server.wadl.generators.WadlGeneratorGrammarsSupport;                                      
 import com.sun.jersey.server.wadl.generators.resourcedoc.WadlGeneratorResourceDocSupport;                       
 import com.sun.research.ws.wadl.Grammars;                                                                       
 import com.sun.research.ws.wadl.Include;                                                                        
 import com.sun.research.ws.wadl.ObjectFactory;                                                                  

 import java.util.List;                                                                                          

 public class HrWadlGeneratorConfig extends WadlGeneratorConfig {                                                

     @Override                                                                                                   
     public List<WadlGeneratorDescription> configure() {                                                         
         ObjectFactory obj = new ObjectFactory()  ;                                                              
         Grammars gram = obj.createGrammars();                                                                   
         Include e = obj.createInclude();                                                                        
         e.setHref("schema.xsd");                                                                                
         gram.getInclude().add(e);                                                                               

         WadlGeneratorConfigDescriptionBuilder builder = generator(WadlGeneratorApplicationDoc.class)            
         .prop( "applicationDocsStream", "application-doc.xml" )                                                 
         .generator( WadlGeneratorGrammarsSupport.class )                                                        
         .prop( "grammarsStream", "application-grammars.xml" )                                                   
         .generator( WadlGeneratorResourceDocSupport.class )                                                     
         .prop( "resourceDocStream", "resourcedoc.xml" );                                                        

         return builder.descriptions();                                                                          

     }                                                                                                           

 }                                                                                                               

以下是Jersey类的片段,@ response.representation.200.qname指向您自己的schema.xsd中的元素

  /**
  * Returns the item if existing. 
  * 
  * @response.representation.200.qname employees
  * @response.representation.200.mediaType application/xml,application/json
  * @response.representation.200.doc This is the representation returned by default
  * @response.representation.200.example {@link EmployeeExample#SAMPLE_ITEM}
  *
  *
  * @return the requested item if this service is available
  */
  @GET
  public List<Employee> getEmployees() {
     return  hrBean.getEmployeesFindAll();
  }

和生成我们将由WADL生成器使用的resourcedoc.xml的maven pom。

  <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>2.4</version>
            </plugin>
        </plugins>
  </pluginManagement>                                                                                                                   

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-javadoc-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>javadoc</goal>
                    </goals>
                    <phase>compile</phase>
                </execution>
            </executions>
            <configuration>
                <encoding>UTF-8</encoding>
                <verbose>false</verbose>
                <show>public</show>
                <subpackages>nl.amis.hr.restful</subpackages>
                <doclet>com.sun.jersey.wadl.resourcedoc.ResourceDoclet</doclet>
                    <docletPath>${path.separator}${project.build.outputDirectory}</docletPath>
                    <docletArtifacts>
                    <docletArtifact>
                          <groupId>nl.amis.hr</groupId>
                          <artifactId>Model</artifactId>
                          <version>1.0-SNAPSHOT</version>
                    </docletArtifact>
                    <docletArtifact>
                        <groupId>com.sun.jersey.contribs</groupId>
                        <artifactId>wadl-resourcedoc-doclet</artifactId>
                        <version>1.17.1</version>
                    </docletArtifact>
                    <docletArtifact>
                        <groupId>com.sun.jersey</groupId>
                        <artifactId>jersey-server</artifactId>
                        <version>1.17.1</version>
                    </docletArtifact>
                    <docletArtifact>
                        <groupId>xerces</groupId>
                        <artifactId>xercesImpl</artifactId>
                        <version>2.6.1</version>
                    </docletArtifact>
                </docletArtifacts>
                <!-- the following option is required as a work around for
                     version 2.5 of the javadoc plugin which will be used
                     by a maven version > 2.0.9 -->
                <useStandardDocletOptions>false</useStandardDocletOptions>
                <additionalparam>-output ${project.build.outputDirectory}/resourcedoc.xml</additionalparam>
            </configuration>
        </plugin>

这是github上的完整示例 https://github.com/biemond/JDeveloper12c_12.1.2/tree/master/RestFulOWSM/WebService