[8.5.5.4 or later]

Generating sample client code for a JAX-RS 2.0 resource

You can generate sample client code for a Java™ API for RESTful Web Services (JAX-RS) resource from a REST project address.

Before you begin

Important: Applicable to Liberty
Complete the following setup for WebSphere® Application Server Developer Tools for Eclipse:
  • Add the Liberty run time to the preferences.
  • Create a Liberty server in the Servers view.
  • Create a web project with one or more REST services that are defined in the project. Otherwise, if you do not define any services, the REST node has no children.

Procedure

  1. Expand your_REST_project > Services > REST.
  2. Right-click the address or one of its children and select Generate > JAX-RS Client. The JAX-RS 2.0 Client dialog is displayed.
    1. In the Source Folder field, accept the default or specify the destination folder of the generated client.
    2. In the Name field, accept the default or specify the Java class name of the client.
    3. In the Package field, accept the default or specify the Java package name of the client.
    4. Select the resources that you want to use to generate the client code.
    5. Generate a new filter class.
      1. Select Generate JAX-RS Client Filter.
      2. Accept the default or specify the name of the filter class.

      This option creates a bare-bones class that implements the ClientRequestFilter and ClientResponseFilter interfaces from the JAX-RS 2.0 javax.ws.rs.client package. You can complete the implementation details for the interface methods after you generate the new filter class.

  3. Optional: [8.5.5.6 or later]To generate JAXB classes and the client into the same project, select Use XML Schema for JAXB Generation.

    The resource method of the JAX-RS service might have JAXB classes as method arguments or return types. If you do not have the JAXB source, but you do have the XML Schema, you can generate the JAXB classes so that your client code can access them.

    1. Click Next.

      The JAX-RS Client with JAXB dialog is displayed.

    2. Optional: In the JAXB Target Java Package field, provide the Java Package that you want to use for the JAXB generated classes.

      This value is passed to the binding compiler, or the Java Architecture for XML Binding Compiler (XJC) tool, with the -p option.

    3. Click Browse... to browse to the XSD file from which you want to generate the JAXB class.

      You can also enter the XSD file into the XML Schema. This value is passed to the binding compiler, or the XJC tool, with the -b option.

    4. Optional: To provide user-defined JAXB binding files, select Specify JAXB binding files.

      If you select this option, you can click Add to add custom binding declaration files. You can also remove files by highlighting them, and then selecting Remove.

    5. Optional: To allow vender extensions, select Allow vender extensions.

      This selection might be necessary for processing some XML Schema documents. This value is passed to the binding compiler, or the XJC tool, with the -extension option.

  4. Click Finish.

Results

You generated sample client code for a JAX-RS resource.

[8.5.5.6 or later]If you completed the optional step to generate JAXB classes, you also generated JAXB classes into the same project as your JAX-RS client.

You also generated a new Java class file by using the JAX-RS 2.0 Client API that can be used to access the JAX-RS service. Every annotated method of the JAX-RS service has a corresponding JAX-RS client method, which is generated from annotations on the JAX-RS service methods. The JAX-RS service methods include @PathParam; @QueryParam; @Consumes; @Produces; @Path; and resource methods such as @GET, @PUT, or @POST.

Restriction: All classes that are affected by the JAX-RS client generation algorithm must adhere to the following restrictions:
  • These classes must be a string, a primitive, a JAX-RS API class, or a user class that is annotated by Java Architecture for XML Binding (JAXB). These classes cannot be binary.
  • All classes must be in source form in order for the algorithm to locate them. If the classes are not in the same project as the JAX-RS service, the JAX-RS client cannot be generated.

What to do next

Add code to the sample so that the client code works and meets your JAX-RS 2.0 client code needs. To test the sample, add the client code to a JSP or a servlet. Your client code must be in a servlet container because there is no support for stand-alone Java applications. After you have working JAX-RS 2.0 client code, use the client code to start the JAX-RS 2.0 service.
Icon that indicates the type of topic Task topic
Timestamp icon Last updated: July 17, 2017 21:58

File name: t_jaxrs_scc_jaxrs.html