首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Maven WADL插件没有生成可用的WADL

Maven WADL插件没有生成可用的WADL
EN

Stack Overflow用户
提问于 2016-10-04 18:58:45
回答 1查看 2.2K关注 0票数 7

我正在尝试使用maven插件来生成一个WADL。该项目构建和服务工作。下面是带有插件的POM的一部分:

代码语言:javascript
复制
             <plugin>
                <groupId>com.sun.jersey.contribs</groupId>
                <artifactId>maven-wadl-plugin</artifactId>
                <version>1.19.2</version>
                <executions>
                    <execution>
                        <id>generate</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <phase>package</phase>
                    </execution>
                </executions>

                <configuration>
                    <wadlFile>${basedir}/src/main/docs/ThingREST.wadl</wadlFile>
                    <formatWadlFile>true</formatWadlFile>
                    <baseUri>http://localhost:8080/ThingREST</baseUri>
                    <packagesResourceConfig>
                        <param>samp.rest.ws.controller</param>
                    </packagesResourceConfig>
                    <wadlGenerators>
                        <wadlGeneratorDescription>
                            <className>com.sun.jersey.server.wadl.generators.WadlGeneratorApplicationDoc
                            </className>
                            <properties>
                                <property>
                                    <name>applicationDocsFile</name>
                                    <value>${basedir}/src/main/docs/xml/app-wadl-doc.xml</value>
                                </property>
                            </properties>
                        </wadlGeneratorDescription>
                        <wadlGeneratorDescription>
                            <className>com.sun.jersey.server.wadl.generators.WadlGeneratorGrammarsSupport
                            </className>
                            <properties>
                                <property>
                                    <name>grammarsFile</name>
                                    <value>${basedir}/src/main/docs/xml/app-wadl-grammar.xml</value>
                                </property>
                            </properties>
                        </wadlGeneratorDescription>                        
                    </wadlGenerators>
                </configuration>
            </plugin>

applicationDocFile和grammarFile存在,但它们不包含任何重要信息。我不知道该包括什么。

这里是控制器:

代码语言:javascript
复制
package samp.rest.ws.controller;

import java.util.List;
import samp.rest.ws.ThingDB;
import samp.rest.ws.vo.Thing;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 *
 * @author Grayson
 */
@RestController
public class ThingController {

    @RequestMapping(value = "/things", method = RequestMethod.GET, headers = "Accept=application/json") //produces = "application/json")
    public List<Thing> getThings() {
        return ThingDB.getThings();
    }

    @RequestMapping(value = "/thing/{id}", method = RequestMethod.GET, headers = "Accept=application/json")
    public Thing getThingById(@PathVariable int id) {
        return ThingDB.getThing(id);
    }

    @RequestMapping(value = "/thingy/{id}", method = RequestMethod.GET)
    public String getThingy(@PathVariable int id) {
        return ThingDB.getThing(id).toString();
    }

    @RequestMapping(value = "/addthing", method = RequestMethod.PUT) //, headers = "Accept=application/json")    
    public void addThing(@RequestBody Thing thing) {
        ThingDB.addThing(thing);

        List<Thing> lt = ThingDB.getThings();
        for (Thing t : lt) {
            System.out.println(" Post add: " + t.toString());
        }
    }
}

最后,这里是所产生的WADL:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://wadl.dev.java.net/2009/02">
    <doc
        jersey:generatedBy="Jersey: 1.19.2 08/25/2016 12:00 AM"
        xmlns:jersey="http://jersey.java.net/"/>
    <doc
        title="Maven Spring REST Sample WADL"
        xml:lang="en"><![CDATA[
        This is the Web Application Description Language (WADL) for
        a Maven Spring REST Service. This service is intended to serve as
        a workign model/sample.
    ]]></doc>
    <grammars/>
    <resources base="http://localhost:8080/ThingREST"/>
</application>

我期望WADL包括可用的服务调用,并标识输入类型。很明显,我不会明白的。

我搜索了大量的例子和文档来使用这个插件。任何和所有的帮助都将不胜感激。遗憾的是,确实存在的文件/例子似乎已经过时了。

提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-18 04:58:05

不幸的是,这个插件不支持Spring控制器。它将只处理带有@Path和其他JAX注释的类。没有其他插件可以满足您的需要。你有两个选择:

1)重写控制器以使用JAX。例如,我将您的控制器更改为:

代码语言:javascript
复制
package samp.rest.ws.controller;

import java.util.List;
import samp.rest.ws.ThingDB;
import samp.rest.ws.vo.Thing;

import javax.ws.rs.*;

@Path("/thing")
public class ThingController {

    @GET
    @Path(value = "/things")
    @Produces("application/json")
    public List<Thing> getThings() {
        return ThingDB.getThings();
    }

    @GET
    @Path(value = "/thing/{id}")
    @Produces("application/json")
    public Thing getThingById(@PathParam("id") int id) {
        return ThingDB.getThing(id);
    }

    @GET
    @Path(value = "/thingy/{id}")
    public String getThingy(@PathParam("id") int id) {
        return ThingDB.getThing(id).toString();
    }

    @PUT
    @Path(value = "/addthing")
    @Produces("application/json")
    public void addThing(Thing thing) {
        ThingDB.addThing(thing);

        List<Thing> lt = ThingDB.getThings();
        for (Thing t : lt) {
            System.out.println(" Post add: " + t.toString());
        }
    }
}

然后插件生成这个WADL文件:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://wadl.dev.java.net/2009/02">
    <doc jersey:generatedBy="Jersey: 1.19.2 08/25/2016 12:00 AM" xmlns:jersey="http://jersey.java.net/"/>
    <doc title="A message in the WADL" xml:lang="en"><![CDATA[This is the Web Application Description Language (WADL) for
        a Maven Spring REST Service. This service is intended to serve as
        a workign model/sample.]]></doc>
    <grammars>
        <include href="xsd0.xsd">
            <doc/>
        </include>
    </grammars>
    <resources base="http://localhost:8080/ThingREST">
        <resource path="/thing">
            <resource path="/things">
                <method id="getThings" name="GET">
                    <response>
                        <representation mediaType="application/json"/>
                    </response>
                </method>
            </resource>
            <resource path="/addthing">
                <method id="addThing" name="PUT">
                    <request>
                        <representation mediaType="*/*"/>
                    </request>
                </method>
            </resource>
            <resource path="/thing/{id}">
                <param name="id" style="template" type="xs:int" xmlns:xs="http://www.w3.org/2001/XMLSchema"/>
                <method id="getThingById" name="GET">
                    <response>
                        <representation mediaType="application/json"/>
                    </response>
                </method>
            </resource>
            <resource path="/thingy/{id}">
                <param name="id" style="template" type="xs:int" xmlns:xs="http://www.w3.org/2001/XMLSchema"/>
                <method id="getThingy" name="GET">
                    <response>
                        <representation mediaType="*/*"/>
                    </response>
                </method>
            </resource>
        </resource>
    </resources>
</application>

2)编写自己的Maven WADL插件,支持Spring控制器。作为参考,您可以以web服务的形式查看这个生成器:

https://javattitude.com/2014/05/26/wadl-generator-for-spring-rest/

以及Maven WADL插件的源代码:

https://github.com/jersey/jersey-1.x/tree/master/contribs/maven-wadl-plugin

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39859814

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档