0

I have created REST Web services and generating war files. And I am able to deploy and test API by using REST client. I need to provide client jar for my REST API for those method which is going to take Complex Object as parameter. I have those complex object is in my local classpath( as source founder I have added as depended classes). But the Problem i am facing how can i generate and give those depended client jar to my REST API clients by using Maven.

So any can please tell me how can I generate client jar for those complex object and give that client jar to REST API clients using Maven My Maven ( pom.xml) as below : =======================================POM.xml=================================

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>opentext</groupId>
  <artifactId>opentext</artifactId>
  <version>${project.version}.${svn.version}</version>
  <packaging>war</packaging>
  <dependencies>      
    <dependency>
    <groupId>com.wordnik</groupId>
    <artifactId>swagger-jersey-jaxrs_2.10</artifactId>
    <version>1.3.0</version>
    </dependency>
    <dependency>
    <groupId>org.fusesource.restygwt</groupId>
    <artifactId>restygwt</artifactId>
    <version>1.3.1</version>
    </dependency>
    <dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-bundle</artifactId>
    <version>1.17.1</version>
</dependency>
<dependency>
       <groupId>org.codehaus.jackson</groupId>
       <artifactId>jackson-jaxrs</artifactId>
       <version>1.9.13</version>
</dependency>
<dependency>
      <groupId>org.codehaus.jackson</groupId>
      <artifactId>jackson-xc</artifactId>
      <version>1.6.1</version>
 </dependency>
 <dependency>
      <groupId>commons-httpclient</groupId>
      <artifactId>commons-httpclient</artifactId>
      <version>3.1</version>
    </dependency>
    </dependencies>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <resources>
      <resource>
        <directory>src</directory>
        <excludes>
          <exclude>**/*.java</exclude>
        </excludes>
      </resource>
    </resources>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.3</version>
        <configuration>
          <warSourceDirectory>WebContent</warSourceDirectory>
          <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
      </plugin>
    </plugins>
    <finalName>BPMService</finalName>
  </build>
</project>

I am having my REST Services in following package : REST Service Package com.rest.entity.services

My Complex object are in com.rest.entity.bean package. Those Complex object which i am using as parameter in my REST service method.

Gautam
  • 3,707
  • 5
  • 36
  • 57
  • What is actually send via REST? binary data, JSON? If it is JSON you can provide an additional library containing the entities. Wich is then not a "client jar" it would be a jar with common classes for client and server side – Zarathustra Jun 18 '14 at 09:57
  • I am expecting client should send as XML but my method will support for both because i have created my services as : ---------------------------------------------- @POST @Consumes({ "application/json", "application/xml"}) @Produces({ "application/json", "application/javascript", "application/xml" }) @JSONP(callbackParam = "callback") public Response claimTask(com.business.entity.bean.ClaimTask) -------------------------------------------------- I am expecting Clients should send XML like below but i don't know how i can pass JSON so i am working with JAXB. – Gautam Jun 18 '14 at 10:06
  • In general I would use something more framework like where you do not have to care about JSON or xml, in most cases. Anyway, since you do not send binary data you can publish an additional jar with the entities. But you are using jaxb so there you may be have an xsd? It would be enough to publish that. Since you can generate the client code from it. – Zarathustra Jun 18 '14 at 10:10
  • My Each bean class is JXBI annotated class com.business.entity.bean.AssignTask assignTaskObj = new com.business.entity.bean.AssignTask(); assignTaskObj.setTaskId("002481F0-8E9F"); assignTaskObj.setAssignedTo("cn=lgautam"); StringWriter writer = new StringWriter(); JAXBContext jaxbContext = JAXBContext.newInstance(com.business.jaxws.task.assigntask.AssignTask.class); Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); jaxbMarshaller.marshal(assignTaskObj, writer); DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost postRequest = new HttpPost("URL"); – Gautam Jun 18 '14 at 10:11
  • So what do you need to publish in order to have the entities? An xsd. Youc an even generate an xsd from your existing code see here: http://stackoverflow.com/questions/7212064/is-it-possible-to-generate-a-xsd-from-a-jaxb-annotated-class – Zarathustra Jun 18 '14 at 10:19
  • I need to give client jar which should contain all bean class. I wanted to generate it by using maven, can any one tell me what i need to add in above pom.xml to access generate client jar as well as war file. – Gautam Jun 18 '14 at 14:59

1 Answers1

1

According to our conversation in the comment section of your question:

The following shoes a simple maven project which creates a Shiporder.class inside your target directory.

The resulting structure:

.
|-- pom.xml
|-- src
|   |-- main
|   |   |-- java
|   |   `-- resources
|   |       `-- shiporder.xsd
|   `-- test
|       `-- java
`-- target
    |-- classes
    |   |-- META-INF
    |   |   `-- sun-jaxb.episode
    |   |-- generated
    |   |   |-- ObjectFactory.class
    |   |   |-- Shiporder$Item.class
    |   |   |-- Shiporder$Shipto.class
    |   |   `-- Shiporder.class
    |   `-- shiporder.xsd
    |-- generated-sources
    |   `-- xjc
    |       |-- META-INF
    |       |   `-- sun-jaxb.episode
    |       `-- generated
    |           |-- ObjectFactory.java
    |           `-- Shiporder.java
    |-- maven-archiver
    |   `-- pom.properties
    |-- someservice-shared-0.0.1-SNAPSHOT.jar
    `-- surefire

The xsd which I just copied from here http://www.w3schools.com/schema/schema_example.asp:

<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="shiporder">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="orderperson" type="xs:string"/>
      <xs:element name="shipto">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="name" type="xs:string"/>
            <xs:element name="address" type="xs:string"/>
            <xs:element name="city" type="xs:string"/>
            <xs:element name="country" type="xs:string"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
      <xs:element name="item" maxOccurs="unbounded">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="title" type="xs:string"/>
            <xs:element name="note" type="xs:string" minOccurs="0"/>
            <xs:element name="quantity" type="xs:positiveInteger"/>
            <xs:element name="price" type="xs:decimal"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
    <xs:attribute name="orderid" type="xs:string" use="required"/>
  </xs:complexType>
</xs:element>
</xs:schema>

And finally the maven pom:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.github.zarathustra</groupId>
        <artifactId>someservice-shared</artifactId>
        <version>0.0.1-SNAPSHOT</version>
<properties>
</properties>

        <build>
                <plugins>
                        <plugin>
                                <!-- The right documentation: http://confluence.highsource.org/display/MJIIP/User+Guide
                                        Interested in more? See also: https://stackoverflow.com/questions/2432859/difference-of-maven-jaxb-plugins -->
                                <groupId>org.jvnet.jaxb2.maven2</groupId>
                                <artifactId>maven-jaxb2-plugin</artifactId>
                                <version>0.8.3</version>
                                <executions>
                                        <execution>
                                                <goals>
                                                        <goal>generate</goal>
                                                </goals>
                                        </execution>
                                </executions>
                                <configuration>
                                        <extension>true</extension>
                                        <args>
                                                <arg>-XtoString</arg>
                                                <arg>-Xequals</arg>
                                                <arg>-XhashCode</arg>
                                                <arg>-Xcopyable</arg>
                                        </args>
                                        <plugins>
                                                <plugin>
                                                        <groupId>org.jvnet.jaxb2_commons</groupId>
                                                        <artifactId>jaxb2-basics</artifactId>
                                                        <version>0.6.5</version>
                                                </plugin>
                                        </plugins>
                                </configuration>
<dependencies>
                                        <dependency>
                                                <groupId>org.aspectj</groupId>
                                                <artifactId>aspectjweaver</artifactId>
                                                <version>1.7.3</version>
                                        </dependency>
                                </dependencies>
                        </plugin>
                </plugins>
        </build>
        <dependencies>
              <!-- To create Request & Responses -->
               <dependency>
                       <groupId>org.jvnet.jaxb2_commons</groupId>
                       <artifactId>jaxb2-basics-runtime</artifactId>
                       <version>0.6.5</version>
               </dependency>
        </dependencies>
</project>

The generated jar whould be ready to use as a dependency in your service project as well as in any client project.

So your steps are:

  1. Create an xsd from your existing code: Is it possible to generate a XSD from a JAXB-annotated class?

  2. create a maven project like above a) Check the configuration, maybe there are some additional options which you like. b) Please note the 2 links in the pom, there are several jaxb code-gen implementations. c) Check for dependency updates, just copied from code from an older project ;)

  3. Add the resulting jar via maven dependency to your service project.

Benefits? xsd which could be used for other programming languages as well. Also its a nice as a reference in any documentation.

As an alternative and if you like spring: https://github.com/spring-by-example/spring-by-example/tree/master/enterprise/spring-rest-services/

Community
  • 1
  • 1
Zarathustra
  • 2,853
  • 4
  • 33
  • 62