我正在为以下问题苦苦挣扎几天。我在 Jersey 的邮件列表和整个网络中搜索了很多答案,但是找不到该特定问题的答案。

设置问题域...

我在用
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;
    }

}
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,正是我所缺少的(希望如此)小片段...

非常感谢你!

最佳答案

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

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

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

application-grammers.xml,它定义架构的位置(带有Things和Thing元素以及复杂类型)

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

只需将此HrWadlGeneratorConfig类添加到您的项目中,并将其作为init参数添加到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>

类(class)
 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();
  }

还有一个maven pom,它生成了我们将由WADL生成器使用的resourcedoc.xml。
  <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

关于list - 为资源列表生成了WADL,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15701953/

10-14 10:00