Detailed explanation of JAX-WS development webservice example

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:

  1. JAX-RPC 2.0 renamed to JAX-WS 2.0
  2. The Java web service Tutorial
  3. javax.jws.WebService

[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:

 

start webservice client ...
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:

1 MyFirstJAX-WS
2. Build a package <package name according to your needs, here is com.wx.jaxws.example>
  Create a java class inside
 
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;
}
}
 
There are a lot of commented places regardless. This class is very simple to receive a string to print and return
3. Build Services
Right click on the project File->New->others->Myeclipse->Web Service->Web Service 
The screen that appears after clicking, select in Strategy --> <Create web service from Java Bean>Bottom-up scenario
Click next In the next dialog enter the name of the class and find it Click FINISH
Because we have created the class and want to build JAX-WS service based on it.
A file with class name +Delegate.java will be generated. What I generate here is MyFirstOneDelegate.java
 
4. The WebService has been done in the previous step. Next, we will publish it.
Right-click on the project name->properties->click on the Java Build Path on the left->select the tab Libraries->click the button on the right Add Library->
Select Myeclipse Libraries->Check JAX-WS 2.1 Runtime Labraries(Project Metro 1.1) and JAX-WS 2.1 API Labraries
<Actually the last two>
 
5. Import several Jar packages <I have put them in the directory> In fact, these Jar packages are in 
MyEclipse 6.5\myeclipse\eclipse\plugins\com.genuitec.eclipse.ws.xfire_6.5.1.zmyeclipse650200806\lib
The directory also already exists, but we want to import it into the project
In fact, what is needed is this package webservices-tools.jar
 
6. Then you can run the URL is not the same as xfire
http://127.0.0.1:8080/MyFirstJAX-WS/MyFirstOnePort?wsdl
is the project name/class name Port?wsdl
If the xml document appears correctly, it means ok!
 
---------------------use-----------------------
7. Create a new project, either a java project or a web project
I am here to test the built java project MyTestJAX-WS_New
Right click on the project File->New->others->Myeclipse->Web Service->Web Service Client 
Click next to select JAX-WS -> select WSDL URL, enter the URL that just passed the test in this, then click next, etc. to process it and click FINISH.
A lot of classes will be generated here I let the automatically generated all in the test package

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326968627&siteId=291194637