1.UserService接口
package zhou;
public interface UserService {
String sayHello(String name);
}
2.UserServiceImpl接口实现类
package zhou;
import javax.jws.WebService;
@WebService
public class UserServiceImpl implements UserService {
@Override
public String sayHello(String name) {
return name + "go to back";
}
}
3.PersonService服务
package zhou;
import javax.jws.WebMethod;
import javax.jws.WebService;
/*
* 可写成接口形式去实现
* 实现类必须指定接口包路径
* @WebService(endpointInterface = "...")
*
* jdk发布webservice服务的时候必须加上该注解
* 服务类中至少必须有一个方法
*/
@WebService(targetNamespace = "http://192.168.0.108:8099/hello")
public class PersonService {
/*
* 不能是静态的
* 不能是final
* */
public String sayHello(String name) {
return name + "hello";
}
/*
* exclude:true/false可以禁用某个方法禁止发布
* */
@WebMethod(exclude = true)
public String sayLove(String name) {
return name + "love miss you";
}
}
4.运行发布
import zhou.PersonService;
import zhou.UserServiceImpl;
import javax.xml.ws.Endpoint;
public class Publisher {
public static void main(String[] args) {
// 第一个参数:服务的地址,第二个参数服务类的对象
// Endpoint publish = Endpoint.publish("http://192.168.0.108:8099/hello", new PersonService());
// System.out.println("发布成功");
publishHello();
}
public static void publishHello () {
Endpoint publish = Endpoint.publish("http://192.168.0.108:8098/hello", new UserServiceImpl());
System.out.println("发布成功");
}
}
5.访问
http://192.168.0.108:8098/hello?wsdl
6.https://blog.csdn.net/qq_36957885/article/details/100001934