首先写一个服务端:
- package com.;
- import javax.jws.WebMethod;
- import javax.jws.WebParam;
- import javax.jws.WebService;
- @WebService(targetNamespace=";, serviceName="PPLoginService")
- public class LoginService {
- @WebMethod
- public String login(@WebParam(name="username") String username, @WebParam(name="password") String password){
- if(username != null && u(password)){
- return "OK";
- }
- return "Faliure";
- }
- }
- package com.;
- import javax.xml.ws.Endpoint;
- public class AppServer {
- public static void main( String[] args ) {
- End(";, new LoginService());
- }
- }
启动。然后,用wsimport(JDK自带命令,本文章中使用的是JDK1.8)生成源代码,命令如下:
wsimport -s ./src -p com. -encoding utf-8
另外新建一个maven项目,把上面生成的代码(src目录)拷贝到项目的src目录中去
代码结构如下:
并且加入spring依赖
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-web</artifactId>
- <version>4.3.2.RELEASE</version>
- </dependency>
- 调用的示例代码如下:
- package com.;
- import java.net.MalformedURLException;
- import java.net.URL;
- import javax.xml.name;
- import javax.xml.ws.Service;
- import org.;
- import org.;
- import org.;
- import org.;
- import com..LoginService;
- import com..PPLoginService;
- public class AppClient {
- //方式一
- public static void fun1() throws Exception {
- URL url = new URL("");
- //QName的两个参数,参照LoginService上面的注解
- QName qname = new QName(";, "PPLoginService");
- Service service = Service.create(url, qname);
- LoginService ls = );
- Sy("admin", "123"));
- Sy("admin", "admin"));
- }
- //方式二(PPLoginService内部其实使用的是方式一)
- public static void fun2() throws Exception {
- URL url = new URL("");
- PPLoginService ppls = new PPLoginService(url);
- LoginService ls = ();
- Sy("admin", "123"));
- Sy("admin", "admin"));
- }
- //方式三,使用spring封装的方式
- public static void fun3() throws Exception {
- AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContex);
- LoginService ls = con);
- Sy("admin", "123"));
- Sy("admin", "admin"));
- con();
- }
- public static void main(String[] args) throws Exception {
- fun1();
- fun2();
- fun3();
- }
- }
- @Configuration
- class WSConfig {
- @Bean
- public JaxWsPortProxyFactoryBean createMainCouponService() throws MalformedURLException {
- //JaxWsPortProxyFactoryBean 类在里面
- JaxWsPortProxyFactoryBean bean = new JaxWsPortProxyFactoryBean();
- bean.setServiceInterface);
- bean.setServiceName("PPLoginService");
- bean.setWsdlDocumentUrl(new URL(""));
- //设置超时
- bean.addCustomProperty("com.;, 20000);
- bean.addCustomProperty("com.;, 20000);
- return bean;
- }
- }