简体   繁体   中英

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.

For example, if I use the following WADL file:

<?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?

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.

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 . I assume this is a mistake. So make sure they match in order for the generation to work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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