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");
}
}
运行结果是: