The Kat's Work - Blog
Main | Blog | Registration | Login
Sunday
2017-09-24
3:19 AM
Welcome Guest | RSS
Main » 2011 » March » 30 » Consuming WebService using Jax-ws in Spring - Basic
5:54 PM
Consuming WebService using Jax-ws in Spring - Basic
 
Following on from my guide to Web service in Lotus Notes, I will now discuss the ins and outs of consuming them in java. Specfically Java spring and Jax-WS.

First off a few pre-cursors in this guide I will not explain wsdl in any great detail so for some background please read the excellent guides from w3c. Also the eclipse plugin "SoapUI” is amazingly useful for working with webservices.

The JAX-WS classes are bundled in with the springframework so no need to go find them.

Firstly lets set up a very basic consumer service.

In your applicationContext-servlet.xml you’ll need to set up your JAX-WS bean. As below.



<bean id="TestNotesService" class="org.springframework.remoting.jaxws.JaxWsPortProxyFactoryBean">
        <property name="wsdlDocumentUrl"  value="http://<url>/TestService?wsdl" />
        <property name="namespaceUri"     value="urn:DefaultNamespace" />
        <property name="serviceName"      value="Test" />
        <property name="portName"         value="domino" />
        <property name="serviceInterface" value="testweb.webservice.TestService" />
 </bean>


As can be seen its very basic functionality. The wsdl url, default namespace, service and port names as specified in the wsdl. The only thing "java” is the definition of a service Interface.

The service interface is what it says on the tin. It is an interface in which we are telling java how to deal with the services methods.

package testweb.webservice;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.ParameterStyle;


@WebService(name = "Test",
        targetNamespace = "urn:DefaultNamespace")
        @SOAPBinding(parameterStyle=ParameterStyle.BARE)
public interface TestService {
   
    @WebMethod(operationName = "SAYHELLO",
            action = "http://<url>/TestService")
            public String SayHello();
   
    @WebMethod(operationName = "SAYMESSAGE",
            action = "http://<url>/TestService")
            public String SayMessage(@WebParam(name = "MESSAGE",
                    targetNamespace = "urn:DefaultNamespace")String message);   
   

}




The sample above shows the setup for simple calls to simple methods of the webservice.

·    @WebService annotataion sets simple information about the service.
·    @SOAPBinding annotation sets the parameter style of the service. In simple methods such as those above where the most being passed in the soap message is a simple string. Than use Bare.
·    @WebMethod() annotation assigns the webservice method and url.
·    @WebParam() annotation specifies a parameter to be sent to the webservice.

So in this example we have a webservice called test. It has at least two methods (SAYHELLO and SAYMESSAGE). SAYHELLO is a very basic method that is called and returns a string. SAYMESSAGE accepts a single parameter and returns a single string.

Usage.

@Controller
@Configurable
public class TestController {

    @Autowired private transient TestService testService;
   

   
    @RequestMapping("/sayHello.htm")
    public void sayHello (HttpServletRequest request, HttpServletResponse response){
try {
                       
           
            if(testService!=null) {
                monthStr = testService.SayHello();
            }
           
           
} catch (Exception e) {
            e.printStackTrace();
        }
   
       
       
    }
}




As you can see after the work to set it up the usage is simple. You may also notice we never set up a class for TestService, we don’t have to JAX-WS has done it for us.

Next up consuming complex webservices. Including creation of xml wrappers for soap messages. Exciting!!!


Views: 13956 | Added by: The_Kat | Tags: Jax-ws, Webservice, Java Spring | Rating: 4.0/1
Total comments: 3
3  
I would also like an example of this when dealing with complex types.

1  
What happened to your follow up on comlex webservices? I'd like to see this approach for passing complex objects instead of strings. Such as a Person object with member variables like (date of birth, gender, first name etc).

I really like this solution though.

2  
Sorry for the delay Gary, between work and other projects not got much time.

Expect it in the next few weeks.

Thanks for the kick to get a move on with it. I need it at the moment.

Login form
Adverts
Search
Calendar
«  March 2011  »
SuMoTuWeThFrSa
  12345
6789101112
13141516171819
20212223242526
2728293031
Entries archive
Site friends
  • Create your own site
  • Spree4.com
  • My Blog
  • Statistics

    Total online: 1
    Guests: 1
    Users: 0
    spree4
    Copyright MyCorp © 2017
    Website builderuCoz