简单的webservice例子(jax-ws)

编写一个接口
package com.cist.gps.webservice.service;


public interface AddNumbersIF {
    
    public int addNumbers(int number1,int number2);

}

接口实现类
package com.cist.gps.webservice.service;

import javax.jws.WebService;

@WebService(targetNamespace = "http://service.webservice.gps.cist.com/", serviceName = "AddNumbersService", portName = "AddNumbersPort")
public class AddNumbersImpl implements AddNumbersIF{
    
    /**
     * @param number1 an int > 0
     * @param number2 an int > 0
     * @return The sum
     * @throws AddNumbersException
     *             if any of the numbers to be added is negative.
     */
    public int addNumbers(int number1, int number2){
        
        return number1 + number2;
    }
}

编写sun-jaxws.xml,web.xml文件放到WebRoot->WEB-INF

sun-jaxws.xml
<?xml version = "1.0"?>
<endpoints version="2.0"
    xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime">
    <endpoint 
        name="AddNumbersPort"
        implementation="com.cist.gps.webservice.service.AddNumbersImpl"
        url-pattern="/AddNumbersPort"/>
    </endpoints>


web.xml
<?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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>CISTGPS</display-name>
   <servlet>
      <servlet-name>AddNumbersService</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>AddNumbersService</servlet-name>
      <url-pattern>/AddNumbersPort</url-pattern>
  </servlet-mapping>
  <listener>
      <listener-class>
          com.sun.xml.ws.transport.http.servlet.WSServletContextListener
      </listener-class>
  </listener>
  
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
</web-app>

需要的jar包
简单的webservice例子(jax-ws)
启动tomcat访问地址
http://192.168.1.61:8080/CISTGPS/AddNumbersPort?wsdl

你可能感兴趣的:(java,Web,xml,webservice,sun)