简体   繁体   English

如何使用 wadl2java 生成 REST 服务模板,其中 POST/PUT 方法有参数?

[英]How do you generate REST service template with wadl2java where POST/PUT methods have parameters?

I've read that the wadl2java code generator and cxf-wadl2java-plugin Maven plugin “can be used to generate server and client JAX-RS code…” However, for anything besides GET requests, the generated code seems useless.我读过 wadl2java 代码生成器和 cxf-wadl2java-plugin Maven 插件“可用于生成服务器和客户端 JAX-RS 代码……”但是,对于 GET 请求之外的任何内容,生成的代码似乎毫无用处。

For example, if I use the following WADL file:例如,如果我使用以下 WADL 文件:

<?xml version="1.0" encoding="UTF-8"?>
<application
  xmlns="http://wadl.dev.java.net/2009/02"
  xmlns:ns="http://superbooks">
  <grammars>
    <include
      href="book.xsd"/>
  </grammars>
  <resources
    base="http://localhost:8080/">
    <resource
      path="/bookstore/put"
      id="poster">
      <method
        name="POST"
        id="postBook">
        <request>
          <representation
            mediaType="application/xml"
            element="ns:Book"/>
        </request>
        <response>
          <representation
            mediaType="*/*"/>
        </response>
      </method>
    </resource>
  </resources>
</application>

And this as a schema:这是一个模式:

<?xml version="1.0"?>
<xs:schema
  id="bookschema"
  targetNamespace="bookschema"
  elementFormDefault="qualified"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:ns="bookschema"
  >

  <xs:complexType name="Book">
    <xs:sequence>
      <xs:element name="id" type="xs:integer"/>
      <xs:element name="name" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
  <xs:element name="Book" type="ns:Book"/>
</xs:schema>

The code that is generated looks like this:生成的代码如下所示:

/**
 * Created by Apache CXF WadlToJava code generator
**/
package com.cxf.test;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;

@Path("/bookstore/put")
public interface Poster {

    @POST
    @Consumes("application/xml")
    @Produces("*/*")
    Response postBook();

}

I would have expected the method to look more like this:我本来希望该方法看起来更像这样:

    @POST
    @Consumes("application/xml")
    @Produces("*/*")
    Response postBook(Book book);

How is it useful to have a POST method that takes no parameters?有一个不带参数的 POST 方法有什么用?

Ultimately, I want to receive JSON instead of XML, but I thought first I should get XML, which is the normal use-case for CXF, working.最终,我想接收 JSON 而不是 XML,但我想首先我应该得到 XML,这是 CXF 的正常用例,工作。

I suspect this has something to do with a binding file, but I can't find anything concrete about how.我怀疑这与绑定文件有关,但我找不到任何具体的方法。

The WADL is using XML namespace http://superbooks for Book element, whereas the XML schema is using bookschema . WADL 使用 XML 命名空间http://superbooks作为 Book 元素,而 XML 模式使用bookschema I assume this is a mistake.我认为这是一个错误。 So make sure they match in order for the generation to work.因此,请确保它们匹配,以便这一代工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM