生成资源列表的WADL

Svi*_*len 6 list jax-rs jaxb jersey wadl

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

设置问题域...

我在Tomcat 7中使用Jersey 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;
    }

}
Run Code Online (Sandbox Code Playgroud)

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

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

我也配置了 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>
Run Code Online (Sandbox Code Playgroud)

就像我说的那样,几乎是完美的,其中存在问题.

<ns2:representation xmlns:ns2="http://wadl.dev.java.net/2009/02"
                            xmlns="" element="thing" mediaType="application/xml" />
Run Code Online (Sandbox Code Playgroud)

WADL没有描述/getThings返回a List<Thing>.相反,它看起来像它指的是一个单一的元素thingxsd0.xsd.因此,当我在例如wadl2java中提供它时,它会生成无类型的客户端.为了得到一个List<Thing>我必须手动编码,类似的东西

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

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

而且我希望创建额外的"ThingList" JAXB注释类,并在我的球衣资源返回代替.

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

非常感谢你!

小智 4

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

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

application-doc.xml 用于高级 WADL 概述注释

application-grammers.xml 定义架构的位置(包含事物和事物元素以及复杂类型)

resourcedoc.xml ,这是由 Maven 插件生成的,它读取您的球衣类,其中包含您的响应元素 javadoc 注释。

只需将此 HrWadlGeneratorConfig 类添加到您的项目中,并将其作为初始化参数添加到球衣 servlet 中

    <init-param>
        <param-name>com.sun.jersey.config.property.WadlGeneratorConfig</param-name>
        <param-value>nl.amis.hr.wadl.HrWadlGeneratorConfig</param-value>
    </init-param> 
Run Code Online (Sandbox Code Playgroud)

班级

 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();                                                                          

     }                                                                                                           

 }                                                                                                               
Run Code Online (Sandbox Code Playgroud)

这是 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();
  }
Run Code Online (Sandbox Code Playgroud)

以及生成供 WADL 生成器使用的 resourcesdoc.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>
Run Code Online (Sandbox Code Playgroud)

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