tomcat publishes web service tutorial

I have been preparing to find a job for the past few days, taught myself some basic content about web services, and encountered a lot of problems. Now I will share what I have learned with you. Since I am a beginner, please correct me if I am wrong, I am very grateful~~! !laughing out loud

1. Download the jax-ws dependency package

Because tomcat does not have the dependency environment required by jax-ws, the first step is to download the Jax-ws RI, that is, jax-ws reference implemantation, address: http://jax-ws.java.net .




2. Install jax-ws RI to the tomcat server

First download ant and tomcat, set the environment variables ANT_HOME and CATALINA_HOME, then introduce the respective bin directory under the path to open the command prompt, and run ant install in the directory of the jax-ws ri package.


This command will directly import the required packages into the ${tomcat}\shared\lib directory, in fact, copy the packages under the jaxws RI lib to the shared\lib under the tomcat installation directory.

3. Set up tomcat in Eclipse

Since eclipse is a tomcat configuration file defined by itself, you need to add something, add shared\lib, and open the ctalina.properties file.



After opening it is (excerpt):

# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#
# List of comma-separated packages that start with or equal this string
# will cause a security exception to be thrown when
# passed to checkPackageAccess unless the
# corresponding RuntimePermission ("accessClassInPackage."+package) has
# been granted.
package.access=sun.,org.apache.catalina.,org.apache.coyote.,org.apache.tomcat.,org.apache.jasper.
#
# List of comma-separated packages that start with or equal this string
# will cause a security exception to be thrown when
# passed to checkPackageDefinition unless the
# corresponding RuntimePermission ("defineClassInPackage."+package) has
# been granted.
#
# by default, no packages are restricted for definition, and none of
# the class loaders supplied with the JDK call checkPackageDefinition.
#
package.definition=sun.,java.,org.apache.catalina.,org.apache.coyote.,org.apache.tomcat.,org.apache.jasper.

#
#
# List of comma-separated paths defining the contents of the "common"
# classloader. Prefixes should be used to define what is the repository type.
# Path may be relative to the CATALINA_HOME or CATALINA_BASE path or absolute.
# If left as blank,the JVM system loader will be used as Catalina's "common"
# loader.
# Examples:
#     "foo": Add this folder as a class repository
#     "foo/*.jar": Add all the JARs of the specified folder as class
#                  repositories
#     "foo/bar.jar": Add bar.jar as a class repository
common.loader=${catalina.home}/shared/lib/*.jar,${catalina.home}/shared/lib,${catalina.base}/lib,${catalina.base}/lib/*.jar,${catalina.home}/lib,${catalina.home}/lib/*.jar

Find the common.loader configuration item and add the two paths ${catalina.home}/shared/ lib /*.jar , ${catalina.home}/shared/lib 

4. Create a project

Create a new web project, webservice_web, with the following directory structure


 

HelloService.java is an interface that provides web service. The code is as follows:

package com.zxuqian.webservice;

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

@WebService
public interface HelloService {
     
      @WebMethod
     String greetings (String name);

}

HelloServiceImpl.java is the implementation class, the code is as follows:

package com.zxuqian.webservice.impl;

import javax.jws.WebService;

import com.zxuqian.webservice.HelloService;

@WebService (endpointInterface = "com.zxuqian.webservice.HelloService" )
public class HelloServiceImpl implements HelloService {

      @Override
      public String greetings(String name) {
            return "Hello: " + name;
     }

}

5, added sun-jaxws.xml

sun-jaxws.xml is a description file for publishing web service applications through the web. The contents are as follows:

<?xml version="1.0" encoding="UTF-8"?>
<endpoints xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime" version="2.0">
	<endpoint name="HelloWorld" implementation="com.zxuqian.webservice.impl.HelloServiceImpl"
		url-pattern="/hello" />
</endpoints>

For the specific description of each node, please refer to the docs document in the downloaded jaxws ri package, here is a brief description, the endpoint needs to specify
the interface and implementation class of the web service service, as well as its url relative path

6. Configure web.xml

The content is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>webservice_web</display-name>
  
  <listener>
  	<listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
  </listener>
  <servlet>
  	<servlet-name>hello</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>hello</servlet-name>
  	<url-pattern>/hello</url-pattern>
  </servlet-mapping>
</web-app>

7. Test

Start tomcat and enter the web service address http://localhost:8088/webservice_web/hello in the browser.  The port number of my tomcat is 8088. You can modify it according to your own port number.


8. References

http://www.mkyong.com/webservices/jax-ws/deploy-jax-ws-web-services-on-tomcat/







Guess you like

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