基于Java的WebService服务端

⒈引入pom相关依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>cn.coreqi</groupId>
    <artifactId>wsdl_server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>wsdl_server</name>
    <description>wsdl_server</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- webService-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web-services</artifactId>
        </dependency>

        <!-- CXF webservice -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
            <version>3.4.5</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>3.4.5</version>
        </dependency>
        <!-- CXF webservice -->
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

⒉配置文件中指定暴露路径

server.port=10088
cxf.path=/services

⒊封装相关模型,包括实体模型,数据请求及相应模型。

package cn.coreqi.wsdl_server.model;

import lombok.Data;

@Data
public class User {
    private String userName;
    private String passWord;
    private Integer age;
    private String realName;
    private String email;
}

 

package cn.coreqi.wsdl_server.model;

import lombok.Data;

@Data
public class RequestParam<T> {
    private String token;
    private String time;
    private T t;
}

 

package cn.coreqi.wsdl_server.model;

import lombok.Data;

@Data
public class ResponseParam<T> {
    private String msg;
    private String status;
    private T t;
}

⒋编写业务相关处理代码,注意添加相关注解

package cn.coreqi.wsdl_server.services;

import cn.coreqi.wsdl_server.model.RequestParam;
import cn.coreqi.wsdl_server.model.ResponseParam;

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

@WebService(name = "getPriceService",targetNamespace = "http://services.wsdl_server.coreqi.cn")
public interface GetPriceService {
    @WebMethod
    ResponseParam getPrice(RequestParam param);
}

 

package cn.coreqi.wsdl_server.services.impl;

import cn.coreqi.wsdl_server.model.RequestParam;
import cn.coreqi.wsdl_server.model.ResponseParam;
import cn.coreqi.wsdl_server.services.GetPriceService;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

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

@Component
@WebService(name = "getPriceService",targetNamespace = "http://services.wsdl_server.coreqi.cn",
        endpointInterface = "cn.coreqi.wsdl_server.services.GetPriceService")
public class GetPriceServiceImpl implements GetPriceService {
    @Override
    public @WebResult ResponseParam getPrice(@WebParam RequestParam param) {
        ResponseParam response = new ResponseParam();
        if(StringUtils.hasLength(param.getTime()) && StringUtils.hasLength(param.getToken())){
            if(param.getT().equals(String.class)){
                String itemId = (String) param.getT();
                String price = "0";
                if(itemId.equals("1")){
                    price = "500";
                }else if(itemId.equals("2")){
                    price = "800";
                }
                response.setMsg("success");
                response.setStatus("200");
                response.setT(price);
            }
        }else{
            response.setMsg("fail");
            response.setStatus("404");
        }
        return response;
    }
}

 

package cn.coreqi.wsdl_server.services;

import cn.coreqi.wsdl_server.model.RequestParam;
import cn.coreqi.wsdl_server.model.ResponseParam;

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

@WebService(name = "sayHelloService",targetNamespace = "http://services.wsdl_server.coreqi.cn")
public interface SayHelloService {
    @WebMethod
    ResponseParam sayHello(RequestParam param);
}

 

package cn.coreqi.wsdl_server.services.impl;

import cn.coreqi.wsdl_server.model.RequestParam;
import cn.coreqi.wsdl_server.model.ResponseParam;
import cn.coreqi.wsdl_server.model.User;
import cn.coreqi.wsdl_server.services.SayHelloService;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

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

@Component
@WebService(name = "sayHelloService",targetNamespace = "http://services.wsdl_server.coreqi.cn",
endpointInterface = "cn.coreqi.wsdl_server.services.SayHelloService")
public class SayHelloServiceImpl implements SayHelloService {
    @Override
    public @WebResult ResponseParam sayHello(@WebParam RequestParam param) {
        ResponseParam response = new ResponseParam();
        if(StringUtils.hasLength(param.getTime()) && StringUtils.hasLength(param.getToken())){
            if(param.getT().equals(User.class)){
                User user = (User)param.getT();
                user.setPassWord("fanqi");
                user.setRealName("fanqi");
                user.setAge(22);
                user.setEmail("fanqisoft@vip.163.com");
                response.setMsg("success");
                response.setStatus("200");
                response.setT(user);
            }
        }else{
            response.setMsg("fail");
            response.setStatus("404");
        }
        return response;
    }
}

⒌编写WebService配置文件

package cn.coreqi.wsdl_server.config;

import cn.coreqi.wsdl_server.services.GetPriceService;
import cn.coreqi.wsdl_server.services.SayHelloService;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.xml.ws.Endpoint;

@Configuration
public class WebServiceConfig {

    @Autowired
    private SayHelloService sayHelloService;

    @Autowired
    private GetPriceService getPriceService;

    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }
    @Bean
    public Endpoint sayHelloEndpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), sayHelloService);
        endpoint.publish("/sayHello");
        return endpoint;
    }

    @Bean
    public Endpoint getPriceEndpoint(){
        EndpointImpl endpoint = new EndpointImpl(springBus(), getPriceService);
        endpoint.publish("/getPrice");
        return endpoint;
    }
}

 

posted @ 2022-04-01 15:16  SpringCore  阅读(381)  评论(0编辑  收藏  举报