1.寫一個接口和實現java
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?wsdlweb