2016-10-04 77 views
5

我正在嘗試使用maven插件生成WADL。項目建立和服務工作。以下是帶插件的POM部分:Maven WADL插件沒有生成可用的WADL

   <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存在,但它們不包含任何重要的信息。我不確定要包括什麼。

這裏是控制器:

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生產:

<?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包括可用的服務電話,並識別輸入的類型。顯然,我沒有得到這個。

我搜索了高和低的例子和文件來使用這個插件。任何和所有的幫助將不勝感激。可悲的是,確實存在的文檔/例子看起來已經過時了。

在此先感謝。

+0

它看起來並不像彈簧安置模塊支持WADL生成,請參見[SPR-8705](https://jira.spring.io/browse/SPR-8705)。 – Tunaki

+0

@Tunaki - 謝謝。 – Grayson

回答

3

不幸的是,這個插件不支持Spring REST控制器。它只會處理用@Path和其他JAX-RS註釋標註的類。沒有其他插件可以滿足您的需求。你有兩種選擇:

1)重寫你的控制器使用JAX-RS。例如,我改變了你的控制器到以下幾點:

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:

<?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。作爲參考,你可以看看這個發電機的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

+0

感謝您的輸入。我可能會在稍後的日子嘗試。由於沒有人付出任何努力,你做了很多,你會得到賞金。恭喜。 – Grayson