content:
- Overview
- lab environment
- server-side implementation
- Client implementation
[1], Overview
Java API for XML Web Services (JAX-WS) is an API of the Java programming language used to create Web services.
On the server side, the user only needs to define the interface SEI (service endpoint interface) that needs to be implemented for remote invocation through the Java language, and provide the relevant implementation. By calling the service publishing interface of JAX-WS, it can be published as a WebService interface.
On the client side, the user can create a proxy (replacing remote services with local objects) through the JAX-WS API to implement calls to the remote server side.
Of course, JAX-WS also provides a set of API calls that operate on the underlying messages. You can directly use SOAP messages or XML messages to send requests through Dispatch, or use Provider to process SOAP or XML messages.
JAX-WS2.0 ( JSR 224 ) is Sun's new web services protocol stack and is a completely standards-based implementation. In the binding layer, the Java Architecture for XML Binding (JAXB, JSR 222 ) is used, and in the parsing layer, the Streaming API for XML (StAX, JSR 173 ) is used, and it also fully supports the schema specification.
The difference between JAX-WS and JAX-RPC can be found at: http://java.sun.com/xml/faq.html#JAX-WS-and-JAX-RPC-difference
Some references for JAX-WS:
[2], the experimental environment
- java version “1.6.0_18″、Eclipse3.7
- maven构建项目:mvn archetype:create -DgroupId=com.micmiu.jaxws.demo -DartifactId=jaxws-demo -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
[three], the realization of the server
1. The most basic example
Write the interface code: ReportService.java
package com.jshx.http.ws; public interface ReportService { String queryDate(String jsonStr); }
Implement the interface and add the webservice annotation: ReportServiceImpl.java
package com.jshx.http.ws.impl; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebService; import javax.jws.soap.SOAPBinding; import com.jshx.http.ws.ReportService; @WebService @SOAPBinding(style = SOAPBinding.Style.RPC) public class ReportServiceImpl implements ReportService { @WebMethod public String queryDate(@WebParam(name="jsonStr") String jsonStr) { return "hi," + jsonStr + " welcom to www.micmiu.com"; } }
Write server-side publishing code: WsServerStart .java
package com.jshx.http.ws; import javax.xml.ws.Endpoint; import com.jshx.http.ws.impl.ReportServiceImpl; public class WsServerStart { public static void main(String[] args) { ReportService ws = new ReportServiceImpl(); Endpoint.publish("http://localhost:8080/ReportServer", ws); } }
Open the browser: http://localhost:8080/ReportServer?wsdl and press Enter to display the following:
It can be seen that the server has been successfully released.
If something goes wrong:
Reason: cxf needs the support of jaxws-api-2.1.jar and jaxb-api-2.1.jar
solution
1. Copy the 2.1 jar required by cxf to the jre\lib\endorsed folder in the jdk directory. If the endorsed file does not exist, create a new one. If not, you may also need to add @SOAPBinding(style = SOAPBinding.Style.RPC) above public class XXX .
2. Upgrade jdk to version 1.6.0_22 or above.
2. MyEclipse uses the Internet to publicly publish the WSDL file, create a WebService Client, and call the WebService
1. Open MyEclipse, create a new Web Project; then create a new package named com.test;
2. Then create a new Web Service Client;
Click next, then enter the WSDL URL: http://localhost:8080/ReportServer?wsdl
Click next, click finish; ok, the system will automatically help generate a lot of code.
2. Write a client test program : ReportClient.java
package com.jshx.http.ws.client; public class ReportClient { public static void main(String[] args) { ReportServiceImplService service = new ReportServiceImplService(); ReportServiceImpl report = service.getReportServiceImplPort (); System.out.println("start webservice client ..."); System.out.println("send Michael to server "); System.out.println(report.queryDate("Michael")); System.out.println("test client end."); } }
Run the test program, the log is as follows:
send Michael to server
hi,Michael welcom to www.micmiu.com
test client end.
It can be seen that the client call is successful.
Another way:
package com.wx.jaxws.example; //import javax.jws.WebMethod; //import javax.jws.WebService; //import javax.jws.soap.SOAPBinding; //@WebService(serviceName = "HelloService", portName = "HelloServicePort", targetNamespace = "http://example.jaxws.wx/jaxws/MyFirstOne") //@SOAPBinding(style = SOAPBinding.Style.DOCUMENT, use = SOAPBinding.Use.LITERAL, parameterStyle = SOAPBinding.ParameterStyle.WRAPPED) public class MyFirstOne { //@WebMethod public String sayHello(String s) { System.out.println("hello," + s); return "hello," + s; } }