These days have been preparing to find a job, self-taught about the Web service of some basic content, also encountered a lot of problems. Now the knowledge I learned and share with you, because it is a beginner, so there is something wrong, please help me correct, thank you ~~!!
1. Download Jax-WS Dependency Package
Because Tomcat does not have the dependent environment required by JAX-WS, the first step is to download jax-ws RIS, the JAX-ws reference Implemantation, Address: http://jax-ws.java.net.
2. Installing JAX-ws RIS to the Tomcat server
First download Ant and Tomcat, set the environment variables Ant_home and Catalina_home, and then introduce the respective bin directory under path to open a command prompt and run the ant install in the directory of the JAX-ws RIS package.
This command will directly import the required package into the ${tomcat}\shared\lib directory, in fact, the Jaxws RI Lib package is copied to the Tomcat installation directory shared\lib inside.
3. Set up Tomcat in eclipse
Since eclipse is a tomcat configuration file of its own definition, you need to add something, add the shared\lib, and open the Ctalina.properties file.
After opening for (excerpt):
[HTML]View Plaincopy
- # 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"); Except in compliance with
- # The License. Obtain a copy of the License at
- #
- # http://www.apache.org/licenses/LICENSE-2.0
- #
- # unless required by applicable or agreed to writing, software
- # Distributed under the License is distributed on a "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
- # would cause a security exception to being 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
- # would cause a security exception to being thrown when
- # passed to Checkpackagedefinition unless the
- # corresponding Runtimepermission ("Defineclassinpackage.") +package) has
- # been granted.
- #
- # By default, no packages is 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 was used to define, what's the repository type.
- # path is relative to the Catalina_home or catalina_base path or absolute.
- # If left as blank,the JVM system loader would 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
Locate the Common.loader configuration item to add ${catalina.home}/shared/lib/*.jar,${catalina.home}/shared/lib both paths
4. Set up the project
Create a new Web project, Webservice_web, with the following directory structure
Helloservice.java is an interface that provides a Web service with the following code:
[Java]View Plaincopy
- 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 with the following code:
[Java]View Plaincopy
- 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. Add Sun-jaxws.xml
Sun-jaxws.xml is a Web service application that publishes a profile with the following content:
[HTML]View Plaincopy
- <? 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 specific instructions on each node, please refer to the Docs document in the Jaxws RI package that you downloaded, and here is a brief explanation that endpoint needs to specify
The interface and implementation class for the Web Service service, and its URL-relative path
6. Configure Web. xml
The contents are as follows:
[HTML]View Plaincopy
- <? 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 " < span class= "attribute" >version= "2.5" >&NBSP;&NBSP;
- <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. Testing
Start Tomcat, enter the Web service address in the browser Http://localhost:8088/webservice_web/hello my Tomcat's port number is 8088, and you make the appropriate changes based on your own port number.
8. Reference documents
http://www.mkyong.com/webservices/jax-ws/deploy-jax-ws-web-services-on-tomcat/
Tomcat Publishing Web Service tutorial