java1234 webservice 第1 课 jdk实现
文章目录
1.写一个接口和实现
package com.java1234.webservice;
import java.util.List;
import javax.jws.WebService;
import com.java1234.entity.Role;
import com.java1234.entity.User;
@WebService //注意这里
public interface HelloWorld {
public String say(String str);
public List<Role> getRoleByUser(User user);
}
package com.java1234.webservice.impl;
import java.util.ArrayList;
import java.util.List;
import javax.jws.WebService;
import com.java1234.entity.Role;
import com.java1234.entity.User;
import com.java1234.webservice.HelloWorld;
@WebService //注意这里
public class HelloWorldImpl implements HelloWorld{
public String say(String str) {
return "Hello:"+str;
}
public List<Role> getRoleByUser(User user) {
List<Role> roleList=new ArrayList<Role>();
// 模拟 直接写死
if(user!=null){
if(user.getUserName().equals("java1234") && user.getPassword().equals("123456")){
roleList.add(new Role(1,"技术总监"));
roleList.add(new Role(2,"架构师"));
}else if(user.getUserName().equals("jack") && user.getPassword().equals("123456")){
roleList.add(new Role(3,"程序员"));
}
return roleList;
}else{
return null;
}
}
}
package com.java1234.webservice.impl;
import javax.xml.ws.Endpoint;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
import com.java1234.webservice.HelloWorld;
public class Server {
public static void main(String[] args) {
System.out.println("web service start");
HelloWorld implementor=new HelloWorldImpl();
String address="http://192.168.244.1/helloWorld";
Endpoint.publish(address, implementor); // jdk实现 暴露webservice接口
System.out.println("web service started");
}
}
运行结果是:
访问: http://192.168.244.1/helloWorld?wsdl

评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册⭐️ Admin快速开发: https://show.cool-admin.com/
赞 0⭐️ 官方文档: https://cool-js.com
赞 0⭐️ chatgpt在线体验: https://cool-js.com/ai/chat-vip/index.html?r=helloworld
赞 0