Friday, August 23, 2013

JAX-WS Call WS using Maven

Use Maven to Generate all Stubs using "wsimport" utility . After generating the webservice you can call the service using Java class. This method will can be easily reused , you just have to use different wsdl url every time you have to consume the webservice and modify the java code , it's light weight and no need to use any third party libraries . So you will need the following
  • wsdl file
  • pom.xml
  • Java Class

First step is to copy the wsdl file and place it in the src/wsdl directory
Setup the pom.xml pom.xml
Folder : Project root dir
<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/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.myws.testconsumews</groupId>
 <artifactId>TestWSConsume</artifactId>
 <packaging>war</packaging>
 <version>1.0-SNAPSHOT</version>

 <name>Test Webservice</name>
 <url>http://maven.apache.org</url>
 
 
 <build>
  
  <plugins>
   
   <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>jaxws-maven-plugin</artifactId>
    <version>1.7</version>
    <executions>
     
      <execution>
      <id>generate-stubs</id>
      <phase>generate-sources</phase>
      <goals>
       <goal>wsimport</goal>
      </goals>
      <configuration>
          <wsdlDirectory>src/wsdl</wsdlDirectory>
       <wsdlFiles>
        <wsdlFile>myservice.wsdl</wsdlFile>
       </wsdlFiles>
       <wsdlLocation>http://localhost:8080/TestWS/myservice?wsdl</wsdlLocation>
        <sourceDestDir>src/main/java</sourceDestDir>
                       <packageName>com.myservice.proxy</packageName>
                  </configuration>
       
     </execution> 
    </executions>
   </plugin>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.2</version>
     <configuration>
     <failOnMissingWebXml>false</failOnMissingWebXml>
    </configuration>
   </plugin>

  </plugins>
 </build>
</project>


run "mvn generate-sources" command , this will generate the files in src/main/java folder . Its specified as <sourceDestDir>src/main/java</sourceDestDir> in pom.xml
Stubs are generated ,You can call the webservice with following Java program
package com.myservice.client;

import com.myservice.proxy.MyService;
import com.myservice.proxy.MyServiceService;

public class CallMyService {
 
 
 
 public static void main(String[] args) {
  MyServiceService mss = new MyServiceService();
  MyService serv = mss.getMyServicePort();
   
     System.out.println(serv.sayHello());
 }
 
 
  
}

Now that was simple right . So what if you want to generate numerous webservice clients for testing and scripting needs you can simply reuse the pom.xml ( change the wsdl file ) , generate the stubs and change the program to call the new webservice .
Note
If your webservice requires authentication , you can set that up as shown below
     ((BindingProvider) serv).getRequestContext().put(BindingProvider.USERNAME_PROPERTY, userName);
      ((BindingProvider) serv).getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, passWord);
          
Cheers, hope that was helpful

Thursday, August 22, 2013

JAX-WS Create WS using Maven

Article describes setting up JAX-WS using Maven and various ways to deploy the webservice . Creating WS using JAX-WS is super simple , simply annotate the class with @WebService and its basically done. Simpicity is good not only for development but also creating mock services and testing error cases. Maven makes it more reusable be able to distibute easily to developers. Also maven takes care of wsgen and dependent libraries. I use Tomcat 7 and running Java 6 . If you are using Java7 , wsgen fails ( yeah it has to be fixed) , so till then configure the IDE to use Java6 for the project   
Java Code - Your WebService ( Simple .. basically you can return POJO or collection of objects for practical use case)
folder - src/main/java
package com.services;

import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService

public class MyService {

 @WebMethod
 public String sayHello()
 {
  return "Hello World";
 }
}

Pom.xml
folder - project folder
<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/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.myws.testws</groupId>
 <artifactId>TestWS</artifactId>
 <packaging>war</packaging>
 <version>1.0-SNAPSHOT</version>

 <name>Test Webservice</name>
 <url>http://maven.apache.org</url>
 
 <dependencies>
    <dependency>
  <groupId>com.sun.xml.ws</groupId>
  <artifactId>jaxws-rt</artifactId>
  <version>2.1.3</version>
  </dependency>
 </dependencies>

 <build>
  
  <plugins>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.2</version>
     <configuration>
     <failOnMissingWebXml>false</failOnMissingWebXml>
    </configuration>
   </plugin>
   <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>jaxws-maven-plugin</artifactId>
    <version>1.7</version>
    <executions>
     <execution>
      <id>generate-wsdl</id>
      <phase>process-classes</phase>
      <goals>
       <goal>wsgen</goal>
      </goals>
      <configuration>
       <sei>com.services.MyService</sei>
       <genWsdl>true</genWsdl>
      </configuration>
     </execution>
    
    </executions>
   </plugin>

   
  </plugins>
 </build>
</project>

Running it using EndPoint
Using Endpoint just use the publisher and WebService is running .
package com.services;

import javax.xml.ws.Endpoint;
 

public class MyServicePublisher{
 
 public static void main(String[] args) {
    Endpoint.publish("http://localhost:8888/TestWS/MyService", new MyService());
    }
 
}

test service : http://localhost:888/TestWS/MyService?wsdl
Running it in Tomcat.
My preferred method is to run it in tomcat . You will need couple of more files
web.xml
folder :src/webapp/WEB-INF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, 
Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_3.dtd">
 
<web-app>
    <listener>
        <listener-class>
                com.sun.xml.ws.transport.http.servlet.WSServletContextListener
        </listener-class>
    </listener>
    <servlet>
        <servlet-name>myservice</servlet-name>
        <servlet-class>
         com.sun.xml.ws.transport.http.servlet.WSServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>myservice</servlet-name>
        <url-pattern>/myservice</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>120</session-timeout>
    </session-config>
</web-app>

sun-jaxws.xml
folder :src/webapp/WEB-INF
<?xml version="1.0" encoding="UTF-8"?>
<endpoints
  xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime"
  version="2.0">
  <endpoint
      name="MyService"
      implementation="com.services.MyService"
      url-pattern="/myservice"/>
</endpoints>

Deploy the war file to tomcat
test deployment : http://localhost:8080/TestWS/myservice?wsdl

Saturday, August 10, 2013

Spring Mongo Integration

Setting up and using MongoDB in the Spring application is a simple two step process.Specify the Mongo template in the application configuration , then customize the ApplicationConfig . Step 1 Include the Mongo configuration in the application context

<mongo:db-factory id="mongoDbFactory" dbname="my-store" />
 
 <mongo:mapping-converter id="mongoConverter" base-package="springdata.mongodb">
  <mongo:custom-converters base-package="springdata.mongodb" />
 </mongo:mapping-converter>
 
 <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
  <constructor-arg ref="mongoDbFactory" />
  <constructor-arg ref="mongoConverter" />
  <property name="writeConcern" value="SAFE" />
 </bean>
 
 <mongo:repositories base-package="springdata.mongodb" />
Step2 Setup the ApplicationConfig in the base-package
@ComponentScan
@EnableMongoRepositories
class ApplicationConfig extends AbstractMongoConfiguration {

 
 @Override
 protected String getDatabaseName() {
 return "my-store";
 
 }

 
 @Override
 public Mongo mongo() throws Exception {

  Mongo mongo = new Mongo();
  mongo.setWriteConcern(WriteConcern.SAFE);

  return mongo;
 }

}
After the two steps the Mongo can be included in the application by simply autowiring
@Autowired Mongo mongo;

Spring Integration to Consume and Publish REST


Spring Integration using gateways makes it possible to consume services from multiple sources and expose them as api's . API's can be exposed with or without further processing .  Lets look at an example without Processing .

If you need a code ref you can find it here https://github.com/arun2dot0/spring-integration-example

getFoo is a Restful service  consumed by Spring and exposes back as service .
First thing to define is the publish-subscribe-channel ,then configure the input-channel with all the mappings .Example shows the method  getFoo but there can be many mappings added for each of the service .

Next step is to specify the gateway to accept the incoming request to the channel . Service interface with the list of interface methods is specified .

Finally the http-oubound gateway is defined with the url , http-method with the transformer.
Transformer can be easily implemented by json-to-object trasnfromation as show.

Update :
It can be done using Java DSL as well and you can find the implementation here
https://github.com/arun2dot0/spring-integration-example-dsl
DSL uses newer version of spring integration and http library .


Spring Configuration
<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
 xmlns:int="http://www.springframework.org/schema/integration"  
 xmlns:http="http://www.springframework.org/schema/integration/http"
 xsi:schemaLocation="http://www.springframework.org/schema/beans   
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
 http://www.springframework.org/schema/integration/http http://www.springframework.org/schema/integration/http/spring-integration-http-2.1.xsd 
http://www.springframework.org/schema/integration   
http://www.springframework.org/schema/integration/spring-integration-2.1.xsd">  
  
<!-- Channel for incoming request-->
<int:publish-subscribe-channel id="pubsubChannel"></int:publish-subscribe-channel>
 
 <!-- Define header with all the necessary mapping--> 
<int:header-value-router input-channel="pubsubChannel" header-name="REQUEST_TYPE">
 <int:mapping value="getFoo" channel="getFooChannel"/>
</int:header-value-router> 

<int:channel id="getFooChannel"/>

<!-- Simple facade method which accepts messages into channel -->
<int:gateway  id="firstGatewayID" service-interface="services.api.FirstGatewayService"     default-request-channel="pubsubChannel" >
  <!--Provide method in the gateway -->
 <int:method name="getFoo">
   <int:header name="REQUEST_TYPE" value="getFoo"/>
  </int:method>
</int:gateway>  


 <!--outbound gateway to get data from url-->
<http:outbound-gateway id="getFooGateway" 
                   url="http://somehost.com/GetFoo" 
                request-channel="getFooChannel" 
                reply-channel="getFooChannel_response"
                http-method="GET" 
                expected-response-type="java.lang.String">
 </http:outbound-gateway>
 
 <int:json-to-object-transformer input-channel="getFooChannel_response" type="model.FooResponse"/>  
</beans>

Java Code

Gateway Service
package services.api;
import org.springframework.integration.annotation.Header;
import org.springframework.integration.annotation.Payload;
public interface FirstGatewayService {
 @Payload("new java.util.Date()")
 public FooResponse getFoo();
}

Response
package model;
import org.codehaus.jackson.annotate.JsonProperty;
public class FooResponse {
 @JsonProperty("status")
 boolean status;
 @JsonProperty("data")
 String data;
 public boolean isStatus() {
  return status;
 }
 public void setStatus(boolean status) {
  this.status = status;
 }
 public String getData() {
  return data;
 }
 public void setData(String data) {
  this.data = data;
 }
}
Controller
@Autowired
private FirstGatewayService gatewayService;
@Override
 @RequestMapping(value = <url to access>, method = RequestMethod.GET)
 public @ResponseBody BaseResponse getFoo() {
  return gatewayService.getFoo();
 }