Java使用jaxws搭建webservice

2017.9.23 jaxws
package com.company.ws.hello;

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

@WebService
public interface IHelloWorld {
    @WebMethod
    @WebResult(name = "returnResult")
    public String greet(@WebParam(name = "name", mode = WebParam.Mode.IN) String name,
            @WebParam(name = "word", mode = WebParam.Mode.IN) String word);
}

实现代码

package com.company.ws.hello;

import javax.jws.WebService;

@WebService(endpointInterface = "com.company.ws.hello.IHelloWorld")
public class HelloWorldImpl implements IHelloWorld {

    @Override
    public String greet(String name, String word) {
        return "Hello: " + name + "," + word;
    }

}
<?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.company.ws.hello.HelloWorldImpl"
     url-pattern="/HelloWorld"/>
</endpoints>
<?xml version="1.0" encoding="UTF-8"?>
<web-app
     xmlns="http://java.sun.com/xml/ns/j2ee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
                         http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
     version="2.4">
     <display-name>company_ws</display-name>
   <listener>
     <listener-class>
        com.sun.xml.ws.transport.http.servlet.WSServletContextListener
     </listener-class>
   </listener>
   <servlet>
      <servlet-name>company_ws</servlet-name>
      <servlet-class>
        com.sun.xml.ws.transport.http.servlet.WSServlet
      </servlet-class>
   </servlet>
   <servlet-mapping>
     <servlet-name>company_ws</servlet-name>
     <url-pattern>/</url-pattern>
   </servlet-mapping>
</web-app>
cd /d C:\download\eclipse\workspace\company_ws
wsgen -s src/main/java -d target -cp target/classes com.company.ws.hello.HelloWorldImpl

此时,会在com.company.ws.hello目录下生成一个jaxws目录,它下面有Greet.java和GreetResponse.java

cd /d C:\download\eclipse\workspace\company_ws\src\test\java
wsimport -s . -p com.company.ws.hello.client -encoding utf-8 http://127.0.0.1:8080/company_ws/HelloWorld?wsdl
package com.company.ws.hello.client;

public class Test {
    public static void main(String[] args) {
        HelloWorldImplService helloWorldImplService = new HelloWorldImplService();
        IHelloWorld helloWorldImplPort = helloWorldImplService.getHelloWorldImplPort();
        String greet = helloWorldImplPort.greet("ndh", "最近如何");
        System.out.println(greet);
    }
}
Hello: ndh,最近如何
<build>
    <plugins>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <configuration>
              <source>1.6</source>
              <target>1.6</target>
              <encoding>UTF-8</encoding>
              <compilerArguments>
               <extdirs>src\main\webapp\WEB-INF\lib</extdirs>
             </compilerArguments>
          </configuration>
        </plugin>
    </plugins>
</build>

更新列表:

*

参考文章:

相关阅读