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怎么调用

猜你喜欢

转载自blog.csdn.net/nannan1232/article/details/78080404
今日推荐