如何构造WADL文件以获取以下Java接口

时间:2014-09-13 11:32:14

标签: java rest interface wadl

我是REST服务的新手。我想创建一个WADL,我将转换为Java接口来创建和公开Rest Web服务..所以..我需要创建一个WADL来获取以下java界面: -

@Path("/getData")
public interface MainData {


    @GET
    @Produces("application/json")
    @Path("/retrieve/")
    public DataResponse retrieveDataOperation(@QueryParam("id") int id);

    @POST
    @Produces("application/json")
    @Path("/insert/")
    public DataResponse insertDataOperation(String message);

    @PUT
    @Produces("application/json")
    @Path("/update/")
    public DataResponse updateDataOperation(String message);

    @DELETE
    @Produces("application/json")
    @Path("/delete/")
    public DataResponse deleteDataOperation(@QueryParam("id") int id);

}

到目前为止,我尝试创建以下WADL以获取上述界面: -

<application xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://wadl.dev.java.net/2009/02 wadl.xsd"
    xmlns:ns1="http://services.test.com/schema/bs/bom/v01/GetAllTypeDesignators"

    xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://wadl.dev.java.net/2009/02">
<grammars>
  <include href="MainData.xsd" />
</grammars>

<resources base="http://localhost:8082">

<resource path="getData" id="com.test.services.schema.maindata.v1.MainData">

        <resource path="/retrieve/" >
             <method name="GET" id="retrieveDataOperation">
                <request>
                    <param  type="xs:int" style="query" name="id"/>
                </request>
                <response>
                     <representation mediaType="application/json" element="ns1:DataResponse"/>
                 </response>
               </method>
        </resource>

   <resource path="/delete/" >
     <method name="DELETE" id="deleteDataOperation">
        <request>
          <param  type="xs:int" style="query" name="id"/>
         </request>
       <response>
         <representation mediaType="application/json" element="ns1:DataResponse"/>
       </response>
      </method>
    </resource>

   <resource path="/insert/" >
       <method name="POST" id="insertDataOperation">
          <request>
             <param name="message" type="xs:string" />
           </request>
               <response>
                   <representation mediaType="application/json" element="ns1:DataResponse"/>
                </response>
            </method>
          </resource>

    <resource path="/update/" >
  <method name="PUT" id="updateDataOperation">
     <request>
        <param name="message" type="xs:string" />
     </request>
    <response>
      <representation mediaType="application/json" element="ns1:DataResponse"/>
    </response>
   </method>
</resource>

</resource>
</resources>
</application>

但是在将此WADL转换为Java之后,我无法获得上面的Java接口。在Apache CXF中进行转换时,它还显示以下异常: - WADLToJava Error: java.lang.NullPointerException 请帮我修改WADL以获得上面的确切java界面..我正在以正确的方式做..请帮助..

1 个答案:

答案 0 :(得分:3)

我刚检查你的WADL,它看起来不错,几个小错误,但没有任何指向你的错误。另外,我使用了命令行工具wadl2java;

wadl2java -d /output/dir -interface /input/wadl/application.wadl

这是更正的WADL;

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<application xmlns="http://wadl.dev.java.net/2009/02" xmlns:ns1="http://services.test.com/schema/bs/bom/v01/GetAllTypeDesignators">
    <grammars>
        <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://services.test.com/schema/bs/bom/v01/GetAllTypeDesignators">
            <xs:element name="dataResponse" type="ns1:dataResponse"/>
            <xs:complexType name="dataResponse">
                <xs:sequence>
                    <xs:element name="param1" type="xs:int"/>
                    <xs:element name="param2" type="xs:string" nillable="true" minOccurs="0" maxOccurs="unbounded"/>
                </xs:sequence>
            </xs:complexType>
        </xs:schema>
    </grammars>
    <resources base="http://localhost:8082">
        <resource path="getData" id="com.test.services.schema.maindata.v1.MainData">
            <resource path="/retrieve/" >
                <method name="GET" id="retrieveDataOperation">
                    <request>
                        <param  type="xs:int" style="query" name="id"/>
                    </request>
                    <response>
                        <representation mediaType="application/json" element="ns1:dataResponse"/>
                    </response>
                </method>
            </resource>
            <resource path="/delete/" >
                <method name="DELETE" id="deleteDataOperation">
                    <request>
                        <param  type="xs:int" style="query" name="id"/>
                    </request>
                    <response>
                        <representation mediaType="application/json" element="ns1:dataResponse"/>
                    </response>
                </method>
            </resource>
            <resource path="/insert/" >
                <method name="POST" id="insertDataOperation">
                    <request>
                        <representation mediaType="*/*">
                            <param name="message" style="plain" type="xs:string" />
                        </representation>
                    </request>
                    <response>
                        <representation mediaType="application/json" element="ns1:dataResponse"/>
                    </response>
                </method>
            </resource>
            <resource path="/update/" >
                <method name="PUT" id="updateDataOperation">
                    <request>
                        <representation mediaType="*/*">
                            <param name="message" style="plain" type="xs:string" />
                        </representation>
                    </request>
                    <response>
                        <representation mediaType="application/json" element="ns1:dataResponse"/>
                    </response>
                </method>
            </resource>
        </resource>
    </resources>
</application>

你应该发布你的MainData.xsd,我敢打赌你的XSD内部会依赖你的错误。

<强>更新

如果在“表示”中使用param名称,那么param名称必须是param类型的lowerCase。我刚刚检查了wadl2java的源代码(cxf-tools-wadlto-jaxrs-3.0.1-sources.jar),并得出结论,需要修改生成器。有问题的行在类org.apache.cxf.tools.wadlto.jaxrs.SourceGenerator:1044中,看起来像elementParamName = elementParamType.toLowerCase();您应该在Apache CXF jira上报告或自行修改代码。

替代方法是将message参数设置为@QueryParam,您可以通过替换来实现;

    <representation mediaType="*/*">
        <param name="message" style="plain" type="xs:string" />
    </representation> 

有了这个;

    <param name="message" style="query" type="xs:string" />

但在这种情况下你的界面方法看起来像;

@POST
@Produces("application/json")
@Path("/insert/")
DataResponse insertDataOperation(@QueryParam("message") String message);

希望它有所帮助。