jdk8 -11 发布 webservice maven 3.6.3为例

149 阅读1分钟

jdk1.8 - jdk11 已测试

pom 导入

<!-- webService before  -->

       <dependency>
           <groupId>org.apache.cxf</groupId>
           <artifactId>cxf-rt-frontend-jaxws</artifactId>
           <version>3.2.4</version>
       </dependency>
       <dependency>
           <groupId>org.apache.cxf</groupId>
           <artifactId>cxf-rt-transports-http-jetty</artifactId>
           <version>3.2.4</version>
       </dependency>
       
<!--       <dependency>
           <groupId>org.apache.cxf</groupId>
           <artifactId>cxf-core</artifactId>
           <version>3.1.9</version>
       </dependency>-->

       <dependency>
           <groupId>com.sun.xml.ws</groupId>
           <artifactId>jaxws-ri</artifactId>
           <version>2.3.3</version>
           <type>pom</type>
       </dependency>

       <dependency>
           <groupId>org.apache.cxf</groupId>
           <artifactId>cxf-core</artifactId>
           <version>3.2.6</version>
       </dependency>
       
<!-- webService end  -->

service

注:@WebParam 内参数一定要加入 targetNamespace 否则无法接收参数

package com.Tencent.cloud.service;

import javax.jws.WebParam;
import javax.jws.WebService;

@WebService(endpointInterface = "com.Tencent.cloud.service.UserService",
      targetNamespace="http://service.cloud.Tencent.com/UserService")
public interface UserService {
   
   /**
    * 用户登录
    * @param userAccount  账号
    * @param password 密码
    * @return
    */
   public String login(@WebParam(name = "userAccount",targetNamespace = "http://service.cloud.Tencent.com/UserService")String userAccount,
                  @WebParam(name = "password",targetNamespace = "http://service.cloud.Tencent.com/UserService")String password);
   
}

impl

package com.Tencent.cloud.service.impl;

import com.Tencent.cloud.service.UserService;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public class UserServiceImpl implements UserService {

   @WebMethod
   @Override
   public String login(String userAccount, String password) {
   
      /**
       * 进行业务处理
       */
      return "hello" + userAccount + password;
   }
}

启动类

package com.Tencent.cloud.controller;

import com.Tencent.cloud.service.impl.UserServiceImpl;

import javax.xml.ws.Endpoint;

public class ServiceMain {
    public static void main( String[] args ){
        System.out.println("webService服务正在启动");
        String serverIp = "127.0.0.1";//使用时,ip可以通过配置文件获取
        String serverPort = "8080";//使用时,端口可以通过配置文件获取
        String userServiceUrl = "http://"+serverIp+":"+serverPort+"/userService";
        Endpoint.publish(userServiceUrl, new UserServiceImpl());
        System.out.println("userService发布地址:" + userServiceUrl);
        System.out.println("webService服务启动成功");
    }
}