Java 7 contains a JAX-WS (RI) implementation. But it's functionality is limited.
Metro is a high-performance, extensible, easy-to-use web service stack that is build on top of RI: https://jax-ws.java.net/.
1. Download the latest Metro runtime from here(https://metro.java.net/2.3/) and extract it.
2. Copy the jars available from the download into the tomcat/lib.
3. Creating webservice
a) Create a dynamic web project. (WSServer)
You write your interface and implementation class.
Bean classes,
b) Create WEB-INF/sun-jaxws.xml
c) Entries in web.xml
Note that if you are planning to test the webservice on a main method instead of a container then all you need to do is,
Here is a snapshot of project hierarchy.
d) Deploy on tomcat.
Check
WSDL definition,
4. Writing client.
For Document style you just specify the annotations,
metro really easy to work with.
Metro is a high-performance, extensible, easy-to-use web service stack that is build on top of RI: https://jax-ws.java.net/.
1. Download the latest Metro runtime from here(https://metro.java.net/2.3/) and extract it.
2. Copy the jars available from the download into the tomcat/lib.
3. Creating webservice
a) Create a dynamic web project. (WSServer)
You write your interface and implementation class.
package com.ws.inf;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
import com.ws.bean.MyBean;
@WebService
@SOAPBinding(style = Style.RPC)
public interface HelloWorld{
@WebMethod MyBean getHelloWorldAsString();
}
package com.ws.imp;
import javax.jws.WebService;
import com.ws.bean.InnerBean;
import com.ws.bean.MyBean;
import com.ws.inf.HelloWorld;
@WebService(endpointInterface = "com.ws.inf.HelloWorld")
public class HelloWorldImpl implements HelloWorld{
@Override
public MyBean getHelloWorldAsString() {
MyBean b = new MyBean(2);
InnerBean ib = new InnerBean();
ib.setValue("hello");
b.setIb(ib);
return b;
}
}
Bean classes,
package com.ws.bean;
public class InnerBean {
private String beanVal;
public String getBeanVal() {
return beanVal;
}
public void setBeanVal(String beanVal) {
this.beanVal = beanVal;
}
}
package com.ws.bean;
public class MyBean {
private InnerBean ib;
private int value;
public MyBean() {
}
public MyBean(int v) {
value = v;
}
public void setValue(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public InnerBean getIb() {
return ib;
}
public void setIb(InnerBean ib) {
this.ib = ib;
}
}
b) Create WEB-INF/sun-jaxws.xml
<?xml version="1.0" encoding="UTF-8"?>
<endpoints
xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime"
version="2.0">
<endpoint
name="HelloWorld"
implementation="com.ws.imp.HelloWorldImpl"
url-pattern="/hello"/>
</endpoints>
c) Entries in web.xml
<listener>
<listener-class>
com.sun.xml.ws.transport.http.servlet.WSServletContextListener
</listener-class>
</listener>
<servlet>
<servlet-name>hello</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>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>120</session-timeout>
</session-config>
Note that if you are planning to test the webservice on a main method instead of a container then all you need to do is,
Endpoint.publish("http://localhost:8085/WSServer/hello", new HelloWorldImpl());
Here is a snapshot of project hierarchy.
d) Deploy on tomcat.
Check
http://localhost:8085/WSServer/hello
WSDL definition,
http://localhost:8085/WSServer/hello?wsdl
Namespace definition,http://localhost:8085/WSServer/hello?wsdl=1
XSD definition,http://localhost:8085/WSServer/hello?xsd=1
4. Writing client.
URL wsdlUrl = new URL("http://localhost:8085/WSServer/hello?wsdl");
// qualifier name ...
QName serviceName = new QName("http://imp.ws.com/",
"HelloWorldImplService");
QName portName = new QName("http://imp.ws.com/",
"HelloWorldImplPort");
Service service = Service.create(wsdlUrl, serviceName);
HelloWorld helloWorldInterface = service.getPort(portName, HelloWorld.class);
System.out.println(helloWorldInterface.getHelloWorldAsString().getValue());
System.out.println(helloWorldInterface.getHelloWorldAsString().getInbean().getValue());
For Document style you just specify the annotations,
@SOAPBinding(style = Style.DOCUMENT, use=Use.ENCODED)
It's optional to use JAXB annotations and I guess that makesmetro really easy to work with.
No comments:
Post a Comment