1. The code priority of the writing process of jax-ws webservice

Before writing webservice programs, I basically used axis or xfire. A recent project used jax-ws. I studied it and summarized the content. There are generally two ways to implement webservice: code first and contract first. The so-called contract priority is to define a data exchange format before implementation, so that everyone has a unified specification to follow when implementing, which is very necessary for large system design; this article first describes the code priority way, specific steps:

1. Design a webservice interface

package gjs.text.ws.inter;

import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
/**
 * Define a webservice interface
 * @author gaojingsheng
 *
 */
@WebService
public interface TestService {
	/**
	 * Summation
	 * @param a addend a
	 * @param b addend b
	 * @return
	 */
	@WebResult(name = "sumResult")
	public int sum(@WebParam(name = "a") int a, @WebParam(name = "b") int b);

	/**
	 * Add a person, return the person who was added
	 * @param person
	 * @return
	 */
	@WebResult(name = "person")
	public Person addPerson(@WebParam(name = "person") Person person);
}

 

 It should be noted here that you need to be familiar with three annotations: @WebService, @WebResult, @WebParam. @WebService specifies the service breakpoint interface (SEI); @WebResult specifies the name of the packaged element of the returned result; @WebParam represents the name of the packaged request parameter

 

2. Implement Service Interface (SIB)

 

 

package gjs.text.ws.inter;

import java.util.ArrayList;
import java.util.List;

import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;

@WebService(endpointInterface="gjs.text.ws.inter.TestService")
public class TestServiceImpl implements TestService{
	//First define a storage queue
	private List<Person> personList = new ArrayList<Person>();

	@Override
	@WebResult(name = "sumResult")
	public int sum(@WebParam(name = "a") int a, @WebParam(name = "b") int b) {
		return a+b;
	}

	@Override
	@WebResult(name = "person")
	public Person addPerson(@WebParam(name = "person") Person person) {
		this.personList.add(person);
		return person;
	}

}

 

 

3. Publishing services

 

package startService;

import javax.xml.ws.Endpoint;

import gjs.test.service.MyServiceImpl;

/**
 * Used to start webservice, the start method is to use the publish method of Endpoint
 * @author gaojingsheng
 *
 */
public class StartService {
	public static void main(String[] args) {
		//How jaxws publishes services
		Endpoint.publish("http://localhost:8889/testService", new TestServiceImpl());
	}
}

 

 4. Take a look at our release effect (wsdl file)

 

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<!--
Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6.
-->
<!--
Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6.
-->
<definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://inter.ws.text.gjs/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/"targetNamespace="http://inter.ws.text.gjs/" name="TestServiceImplService">
<types>
<xsd:schema>
<xsd:import namespace="http://inter.ws.text.gjs/" schemaLocation="http://localhost:8889/testService?xsd=1"/>
</xsd:schema>
</types>
<message name="sum">
<part name="parameters" element="tns:sum"/>
</message>
<message name="sumResponse">
<part name="parameters" element="tns:sumResponse"/>
</message>
<message name="addPerson">
<part name="parameters" element="tns:addPerson"/>
</message>
<message name="addPersonResponse">
<part name="parameters" element="tns:addPersonResponse"/>
</message>
<portType name="TestService">
<operation name="sum">
<input message="tns:sum"/>
<output message="tns:sumResponse"/>
</operation>
<operation name="addPerson">
<input message="tns:addPerson"/>
<output message="tns:addPersonResponse"/>
</operation>
</portType>
<binding name="TestServiceImplPortBinding" type="tns:TestService">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<operation name="sum">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
<operation name="addPerson">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<service name="TestServiceImplService">
<port name="TestServiceImplPort" binding="tns:TestServiceImplPortBinding">
<soap:address location="http://localhost:8889/testService"/>
</port>
</service>
</definitions>

 

 

 At this point, a simple service is built.

 

Guess you like

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