Publish WebService using JDK (JAX-WS)

 

There are many ways to publish webservices in Java, such as CXF, XFire, Axis2, etc. Here is another way, a way that is natively supported by JDK, and does not require any additional jar package, that is JAX-WS.

 

Using JAX-WS to develop WebService only requires a few simple steps:

1. Write interfaces and implementations (or not split, that is, ordinary business processing classes that do not inherit any interfaces)

2. Release

3. Use the command wsimport to build the WebService client .

4. Service call

 

1. The first is to write the interface,

In the interface, you only need to mark the class as @WebService,

Just mark the method to be exposed to the client as @WebMethod.

package com.service;

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

@WebService
public class UserService {
    @WebMethod
    public String get(String name){
    	return "Hello,"+name;
    }
}

 

2. Release

import javax.xml.ws.Endpoint;

import com.service.UserService;

public class Test{
	
	public static void main(String[] args) throws Exception {
        
        String address2 = "http://localhost/webservice/user";
        Endpoint.publish(address2, new UserService());
        
        System.out.println("Webservice published successfully!");
    }
	
}

 

After the service is successfully released, the main method does not terminate, but starts the backend thread to listen for the service call and provide the service. 


 

 

After the service is published successfully, the service description file can be accessed.

http://localhost/webservice/user?wsdl

<?xml version="1.0" encoding="UTF-8"?><!-- Published by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e. --><!-- Generated by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e. --><definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://service.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://service.com/" name="UserServiceService">
<types>
<xsd:schema>
<xsd:import namespace="http://service.com/" schemaLocation="http://localhost/webservice/user?xsd=1"></xsd:import>
</xsd:schema>
</types>
<message name="get">
<part name="parameters" element="tns:get"></part>
</message>
<message name="getResponse">
<part name="parameters" element="tns:getResponse"></part>
</message>
<portType name="UserService">
<operation name="get">
<input wsam:Action="http://service.com/UserService/getRequest" message="tns:get"></input>
<output wsam:Action="http://service.com/UserService/getResponse" message="tns:getResponse"></output>
</operation>
</portType>
<binding name="UserServicePortBinding" type="tns:UserService">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"></soap:binding>
<operation name="get">
<soap:operation soapAction=""></soap:operation>
<input>
<soap:body use="literal"></soap:body>
</input>
<output>
<soap:body use="literal"></soap:body>
</output>
</operation>
</binding>
<service name="UserServiceService">
<port name="UserServicePort" binding="tns:UserServicePortBinding">
<soap:address location="http://localhost/webservice/user"></soap:address>
</port>
</service>
</definitions>

 

  

3. Use the command wsimport to build the WebService client .

任意处新建文件夹,并cmd到该路径下,执行命令:wsimport -keep http://localhost/webservice/user?wsdl


 
 进入该文件夹下,可以看到生成的客户端代码。

生成的这些文件不需要修改,我们调用上面发布的webservice服务时,会用到。

 

4、借助自动生成的客户端代码调用webservice服务。

 

public class Test {

	public static void main(String[] args) {
		UserServiceService userServiceService = new UserServiceService();
		UserService userService = userServiceService.getUserServicePort();
        String result = userService.get("张三");
        System.out.println(result);

	}

}

上面代码中UserServiceService 和 UserService两个类就是自动生成的类。

上面代码输出结果:你好,张三

 

捕获请求数据说明:

上面调用webservice接口时,webservice客户端会自动发送两次请求,

第一次请求是拉取(GET)服务描述信息,

第二次请求是调用(POST)业务API。

抓包数据(为方便抓包,重新发布了服务地址:http://localhost:8080/cloud-web/user):

 

=====================首次请求========================

请求url              http://localhost:8080/cloud-web/user

请求编码           UTF-8

请求method:    GET

请求body:

=====================header信息==========

user-agent    Java/1.8.0_131

host              localhost:8080

accept           text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2

connection    keep-alive

===================parameter信息==========

wsdl       

=====================二次请求========================

 

请求url            http://localhost:8080/cloud-web/user

请求编码         UTF-8

请求method   POST

请求body    

<?xml version="1.0" ?>

<S:Envelope

    xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">

    <S:Body>

        <ns2:get

            xmlns:ns2="http://service.com/">

            <arg0>张三</arg0>

        </ns2:get>

    </S:Body>

</S:Envelope>

=====================header信息=========

accept               text/xml, multipart/related

content-type     text/xml; charset=utf-8

soapaction        "http://service.com/UserService/getRequest"

user-agent        JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e

host                  localhost:8080

connection        keep-alive

content-length  186

 

 

补充说明1;

业务类分为接口和实现类的示例:

接口代码

@WebService
public interface HelloWorld {
    @WebMethod
    public String hello(String name);
}

 

实现类代码

@WebService
public class HelloWordImpl implements HelloWorld {

    @Override
    public String hello(String name) {
    	System.out.println("接收到参数:"+name);
        return "hello :"+name;
    }

}

发布服务代码

 String address="http://localhost/webservice/hello";
        Endpoint.publish(address, new HelloWordImpl());

  

 

 

 

补充说明2:

以上在jdk8下开发测试。

 

 

 

 

 

 

 

 


 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326392611&siteId=291194637