jdk開發webservice例子

1、建立一個maven項目,

 

2、pom.xml文件內容:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>mavenDemo2</groupId>
<artifactId>mavenDemo22</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>mavenDemo22 Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>




<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins </groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3 </version>
<configuration>
<source>8 </source>
<target>8 </target>
</configuration>
</plugin>
</plugins>
</build>


</project>

3、編寫接口類HelloWorld類:

package main.com.webservice;
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
public interface HelloWorld {
@WebMethod
public String sayHello(String name);
}

4、編寫HelloWorldImpl類:

package main.com.webservice;
import javax.jws.WebService;
@WebService
public class HelloWorldImpl implements HelloWorld{
@Override
public String sayHello(String name) {
System.out.println("name:"+name);
return "hello"+name;
}

}

5、編寫Mains類:

package main.com.webservice;
import javax.xml.ws.Endpoint;
public class Mains {
public static void main(String[] args)
{
String address="http://localhost:8088/mavenDemo22/HelloWorld";//端口8088隨便寫
Endpoint.publish(address, new HelloWorldImpl());
System.out.println("success:");
}
}

6、點擊ecplise中的瀏覽器,下圖:

7、點擊ecplise中的webservice瀏覽器,輸入參數得到結果:

 

8、如果用cxf寫webservice,代碼一樣,需要引入cxf的jar包,後續編寫用cxf編寫的代碼,及發佈服務器後,client怎麼調用

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章