jax-rs jax-ws_Tomcat服务器上的JAX-WS Web服务部署

jax-rs jax-ws

We learned how to use JAX-WS to create SOAP web services and publish it using javax.xml.ws.Endpoint but most of the times we want to deploy our services on a servlet container. So today we will learn how to create a web service and deploy it on Apache Tomcat server and then test it out with the client application.

我们学习了如何使用JAX-WS创建SOAP Web服务并使用javax.xml.ws.Endpoint发布它,但是大多数时候,我们希望将服务部署在servlet容器上。 因此,今天我们将学习如何创建Web服务并将其部署在Apache Tomcat服务器上,然后使用客户端应用程序对其进行测试。

For this, our first step is to create a Dynamic Web Project to write our business logic. Our final project will look like below image.

为此,我们的第一步是创建一个动态Web项目来编写我们的业务逻辑。 我们的最终项目将如下图所示。

For JAX-WS web service deployment on a servlet container, we need to add some jar files into it. There are two ways to do this.

为了在servlet容器上部署JAX-WS Web服务,我们需要向其中添加一些jar文件。 有两种方法可以做到这一点。

  1. Download jar files from https://jax-ws.java.net/ and include it in the dynamic web project.

    https://jax-ws.java.net/下载jar文件,并将其包含在动态Web项目中。
  2. Convert Dynamic web project into Maven and add below dependencies.
    <dependency>
    	<groupId>com.sun.xml.ws</groupId>
    	<artifactId>jaxws-rt</artifactId>
    	<version>2.2.10</version>
    </dependency>

    This will automatically download all the required jar files as maven dependencies shown in the below image. I like this approach because I can upgrade the versions easily and it will also make sure none of the jars are getting missed.

    <dependency>
    	<groupId>com.sun.xml.ws</groupId>
    	<artifactId>jaxws-rt</artifactId>
    	<version>2.2.10</version>
    </dependency>

    这将自动下载所有必需的jar文件作为maven依赖关系,如下图所示。 我喜欢这种方法,因为我可以轻松升级版本,并且还可以确保不会丢失任何jar。

Now let’s go through the business logic. First, we will create a model bean class.

现在,让我们来看一下业务逻辑。 首先,我们将创建一个模型bean类。

package com.journaldev.jaxws.beans;

import java.io.Serializable;

public class Person implements Serializable{

	private static final long serialVersionUID = -5577579081118070434L;
	
	private String name;
	private int age;
	private int id;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}
	
	@Override
	public String toString(){
		return id+"::"+name+"::"+age;
	}

}

Now we will create our service interface and implementation classes.

现在,我们将创建我们的服务接口和实现类。

package com.journaldev.jaxws.service;

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;

import com.journaldev.jaxws.beans.Person;

@WebService
@SOAPBinding(style = SOAPBinding.Style.RPC)
public interface PersonService {

	@WebMethod
	public boolean addPerson(Person p);
	
	@WebMethod
	public boolean deletePerson(int id);
	
	@WebMethod
	public Person getPerson(int id);
	
	@WebMethod
	public Person[] getAllPersons();
}
package com.journaldev.jaxws.service;


import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.jws.WebService;

import com.journaldev.jaxws.beans.Person;

@WebService(endpointInterface = "com.journaldev.jaxws.service.PersonService")  
public class PersonServiceImpl implements PersonService {

	private static Map<Integer,Person> persons = new HashMap<Integer,Person>();
	
	@Override
	public boolean addPerson(Person p) {
		if(persons.get(p.getId()) != null) return false;
		persons.put(p.getId(), p);
		return true;
	}

	@Override
	public boolean deletePerson(int id) {
		if(persons.get(id) == null) return false;
		persons.remove(id);
		return true;
	}

	@Override
	public Person getPerson(int id) {
		return persons.get(id);
	}

	@Override
	public Person[] getAllPersons() {
		Set<Integer> ids = persons.keySet();
		Person[] p = new Person[ids.size()];
		int i=0;
		for(Integer id : ids){
			p[i] = persons.get(id);
			i++;
		}
		return p;
	}

}

Our web service code is ready, next steps are required to create it as a web archive that we can deploy in the servlet container.

我们的Web服务代码已准备就绪,需要执行下一步以将其创建为可在Servlet容器中部署的Web存档。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
	xmlns="https://xmlns.jcp.org/xml/ns/javaee"
	xsi:schemaLocation="https://xmlns.jcp.org/xml/ns/javaee https://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
	id="WebApp_ID" version="3.1">
	<display-name>JAXWS-Tomcat</display-name>

	<listener>
		<listener-class>
			com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
	</listener>
	
	<servlet>
		<servlet-name>JAXWSServlet</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>JAXWSServlet</servlet-name>
		<url-pattern>/personWS</url-pattern>
	</servlet-mapping>
	<session-config>
		<session-timeout>30</session-timeout>
	</session-config>
	
</web-app>

There is nothing project-specific, these are generic changes to add listener class and front servlet class for web service.

没有特定于项目的内容,这些是对Web服务添加侦听器类和前端servlet类的通用更改。

Next step is to create sun-jaxws.xml file inside WEB-INF directory where we will provide endpoint details. URL-pattern should be same as defined in the web.xml file.

下一步是在WEB-INF目录中创建sun-jaxws.xml文件,我们将在其中提供端点详细信息。 URL模式应与web.xml文件中定义的相同。

sun-jaxws.xml

sun-jaxws.xml

<?xml version="1.0" encoding="UTF-8"?>
<endpoints xmlns="https://java.sun.com/xml/ns/jax-ws/ri/runtime" version="2.0">
  <endpoint
     name="PersonServiceImpl"
     implementation="com.journaldev.jaxws.service.PersonServiceImpl"
     url-pattern="/personWS"/>
</endpoints>

That’s it, we are done. Just export project as a WAR file and deploy it into tomcat container. Access the web service URL as shown in below image.

就是这样,我们完成了。 只需将项目导出为WAR文件并将其部署到tomcat容器中即可。 如下图所示访问Web服务URL。

Access the WSDL URL and take note of targetNamespace and name attributes, we will use them in the client side program.

访问WSDL URL,并记下targetNamespacename属性,我们将在客户端程序中使用它们。

Below is a simple test program to use our web service and run some tests.

以下是使用我们的Web服务并运行一些测试的简单测试程序。

package com.journaldev.jaxws.test;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;

import com.journaldev.jaxws.beans.Person;
import com.journaldev.jaxws.service.PersonService;

public class SOAPPublisherClient {

	public static void main(String[] args) throws MalformedURLException {
		URL wsdlURL = new URL("https://localhost:8080/JAXWS-Tomcat/personWS?wsdl");
		//check above URL in browser, you should see WSDL file
		
		//creating QName using targetNamespace and name
		QName qname = new QName("https://service.jaxws.journaldev.com/", "PersonServiceImplService"); 
		
		Service service = Service.create(wsdlURL, qname);  
		
		//We need to pass interface and model beans to client
		PersonService ps = service.getPort(PersonService.class);
		
		Person p1 = new Person(); p1.setName("Pankaj"); p1.setId(1); p1.setAge(30);
		Person p2 = new Person(); p2.setName("Meghna"); p2.setId(2); p2.setAge(25);
		
		//add person
		System.out.println("Add Person Status="+ps.addPerson(p1));
		System.out.println("Add Person Status="+ps.addPerson(p2));
		
		//get person
		System.out.println(ps.getPerson(1));
		
		//get all persons
		System.out.println(Arrays.asList(ps.getAllPersons()));
		
		//delete person
		System.out.println("Delete Person Status="+ps.deletePerson(2));
		
		//get all persons
		System.out.println(Arrays.asList(ps.getAllPersons()));
		
	}

}

As explained in previous tutorial, we can use wsimport utility to generate client stubs and use it. When we run above program, we get output as shown in below image.

前面的教程中所述 ,我们可以使用wsimport实用程序生成并使用客户端存根。 当我们运行上面的程序时,我们得到的输出如下图所示。

That’s all for now, I hope you liked the tutorial and it will help you in deploying web services in Tomcat container.

到此为止,我希望您喜欢本教程,它将帮助您在Tomcat容器中部署Web服务。

GitHub Repository. GitHub Repository下载最终的项目代码。

翻译自: https://www.journaldev.com/9133/jax-ws-web-service-deployment-on-tomcat-server

jax-rs jax-ws

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值