springboot系类代码:cxf-spring-boot-starter-jaxws

田志尚
2023-12-01

Apache CXF = Celtix + XFire,开始叫 Apache CeltiXfire,后来更名为 Apache CXF 了,以下简称为 CXF。CXF 继承了 Celtix 和 XFire 两大开源项目的精华,提供了对 JAX-WS 全面的支持,并且提供了多种 Binding 、DataBinding、Transport 以及各种 Format 的支持,并且可以根据实际项目的需要,采用代码优先(Code First)或者 WSDL 优先(WSDL First)来轻松地实现 Web Services 的发布和使用。Apache CXF已经是一个正式的Apache顶级项目。

Web Service是一个平台独立的,低耦合的,自包含的、基于可编程的web的应用程序,可使用开放的XML(标准通用标记语言下的一个子集)标准来描述、发布、发现、协调和配置这些应用程序,用于开发分布式的交互操作的应用程序。
Web Service技术, 能使得运行在不同机器上的不同应用无须借助附加的、专门的第三方软件或硬件, 就可相互交换数据或集成。依据Web Service规范实施的应用之间, 无论它们所使用的语言、 平台或内部协议是什么, 都可以相互交换数据。Web Service是自描述、 自包含的可用网络模块, 可以执行具体的业务功能。Web Service也很容易部署, 因为它们基于一些常规的产业标准以及已有的一些技术,诸如标准通用标记语言下的子集XML、HTTP。Web Service减少了应用接口的花费。Web Service为整个企业甚至多个组织之间的业务流程的集成提供了一个通用机制。

package com.programb.webservice;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}


package com.programb.webservice.client;

import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.WebEndpoint;
import javax.xml.ws.WebServiceClient;
import javax.xml.ws.WebServiceException;
import javax.xml.ws.WebServiceFeature;


@WebServiceClient(name = "CommonService", targetNamespace = "http://model.webservice.programb.com/", wsdlLocation = "http://localhost:8092/services/CommonService?wsdl")
public class CommonService_Service
    extends Service
{

    private final static URL COMMONSERVICE_WSDL_LOCATION;
    private final static WebServiceException COMMONSERVICE_EXCEPTION;
    private final static QName COMMONSERVICE_QNAME = new QName("http://model.webservice.programb.com/", "CommonService");

    static {
        URL url = null;
        WebServiceException e = null;
        try {
            url = new URL("http://localhost:8092/services/CommonService?wsdl");
        } catch (MalformedURLException ex) {
            e = new WebServiceException(ex);
        }
        COMMONSERVICE_WSDL_LOCATION = url;
        COMMONSERVICE_EXCEPTION = e;
    }

    public CommonService_Service() {
        super(__getWsdlLocation(), COMMONSERVICE_QNAME);
    }

    public CommonService_Service(WebServiceFeature... features) {
        super(__getWsdlLocation(), COMMONSERVICE_QNAME, features);
    }

    public CommonService_Service(URL wsdlLocation) {
        super(wsdlLocation, COMMONSERVICE_QNAME);
    }

    public CommonService_Service(URL wsdlLocation, WebServiceFeature... features) {
        super(wsdlLocation, COMMONSERVICE_QNAME, features);
    }

    public CommonService_Service(URL wsdlLocation, QName serviceName) {
        super(wsdlLocation, serviceName);
    }

    public CommonService_Service(URL wsdlLocation, QName serviceName, WebServiceFeature... features) {
        super(wsdlLocation, serviceName, features);
    }

    /**
     * 
     * @return
     *     returns CommonService
     */
    @WebEndpoint(name = "CommonServiceImplPort")
    public CommonService getCommonServiceImplPort() {
        return super.getPort(new QName("http://model.webservice.programb.com/", "CommonServiceImplPort"), CommonService.class);
    }

    /**
     * 
     * @param features
     *     A list of {@link WebServiceFeature} to configure on the proxy.  Supported features not in the <code>features</code> parameter will have their default values.
     * @return
     *     returns CommonService
     */
    @WebEndpoint(name = "CommonServiceImplPort")
    public CommonService getCommonServiceImplPort(WebServiceFeature... features) {
        return super.getPort(new QName("http://model.webservice.programb.com/", "CommonServiceImplPort"), CommonService.class, features);
    }

    private static URL __getWsdlLocation() {
        if (COMMONSERVICE_EXCEPTION!= null) {
            throw COMMONSERVICE_EXCEPTION;
        }
        return COMMONSERVICE_WSDL_LOCATION;
    }

}


package com.programb.webservice.client;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.bind.annotation.XmlSeeAlso;
import javax.xml.ws.RequestWrapper;
import javax.xml.ws.ResponseWrapper;



@WebService(name = "CommonService", targetNamespace = "http://model.webservice.programb.com/")
@XmlSeeAlso({
    ObjectFactory.class
})
public interface CommonService {



    @WebMethod
    @WebResult(targetNamespace = "")
    @RequestWrapper(localName = "getUser", targetNamespace = "http://model.webservice.programb.com/", className = "com.programb.webservice.client.GetUser")
    @ResponseWrapper(localName = "getUserResponse", targetNamespace = "http://model.webservice.programb.com/", className = "com.programb.webservice.client.GetUserResponse")
    public User getUser(
            @WebParam(name = "userName", targetNamespace = "")
                    String userName);


    @WebMethod
    @WebResult(targetNamespace = "")
    @RequestWrapper(localName = "sayHello", targetNamespace = "http://model.webservice.programb.com/", className = "com.programb.webservice.client.SayHello")
    @ResponseWrapper(localName = "sayHelloResponse", targetNamespace = "http://model.webservice.programb.com/", className = "com.programb.webservice.client.SayHelloResponse")
    public String sayHello(
            @WebParam(name = "userName", targetNamespace = "")
                    String userName);

}


package com.programb.webservice.client;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;



@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "getUser", propOrder = {
    "userName"
})
public class GetUser {

    protected String userName;


    public String getUserName() {
        return userName;
    }


    public void setUserName(String value) {
        this.userName = value;
    }

}


package com.programb.webservice.client;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;


@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "getUserResponse", propOrder = {
    "_return"
})
public class GetUserResponse {

    @XmlElement(name = "return")
    protected User _return;

 
    public User getReturn() {
        return _return;
    }


    public void setReturn(User value) {
        this._return = value;
    }

}


package com.programb.webservice.client;

import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlElementDecl;
import javax.xml.bind.annotation.XmlRegistry;
import javax.xml.namespace.QName;



@XmlRegistry
public class ObjectFactory {

    private final static QName _GetUser_QNAME = new QName("http://model.webservice.programb.com/", "getUser");
    private final static QName _GetUserResponse_QNAME = new QName("http://model.webservice.programb.com/", "getUserResponse");
    private final static QName _SayHello_QNAME = new QName("http://model.webservice.programb.com/", "sayHello");
    private final static QName _SayHelloResponse_QNAME = new QName("http://model.webservice.programb.com/", "sayHelloResponse");


    public ObjectFactory() {
    }

 
    public GetUserResponse createGetUserResponse() {
        return new GetUserResponse();
    }


    public SayHello createSayHello() {
        return new SayHello();
    }


    public GetUser createGetUser() {
        return new GetUser();
    }


    public SayHelloResponse createSayHelloResponse() {
        return new SayHelloResponse();
    }


    public User createUser() {
        return new User();
    }


    @XmlElementDecl(namespace = "http://model.webservice.programb.com/", name = "getUser")
    public JAXBElement<GetUser> createGetUser(GetUser value) {
        return new JAXBElement<GetUser>(_GetUser_QNAME, GetUser.class, null, value);
    }


    @XmlElementDecl(namespace = "http://model.webservice.programb.com/", name = "getUserResponse")
    public JAXBElement<GetUserResponse> createGetUserResponse(GetUserResponse value) {
        return new JAXBElement<GetUserResponse>(_GetUserResponse_QNAME, GetUserResponse.class, null, value);
    }


    @XmlElementDecl(namespace = "http://model.webservice.programb.com/", name = "sayHello")
    public JAXBElement<SayHello> createSayHello(SayHello value) {
        return new JAXBElement<SayHello>(_SayHello_QNAME, SayHello.class, null, value);
    }


    @XmlElementDecl(namespace = "http://model.webservice.programb.com/", name = "sayHelloResponse")
    public JAXBElement<SayHelloResponse> createSayHelloResponse(SayHelloResponse value) {
        return new JAXBElement<SayHelloResponse>(_SayHelloResponse_QNAME, SayHelloResponse.class, null, value);
    }

}


package com.programb.webservice.client;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;


@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "sayHello", propOrder = {
    "userName"
})
public class SayHello {

    protected String userName;


    public String getUserName() {
        return userName;
    }


    public void setUserName(String value) {
        this.userName = value;
    }

}


package com.programb.webservice.client;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;


@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "sayHello", propOrder = {
    "userName"
})
public class SayHello {

    protected String userName;


    public String getUserName() {
        return userName;
    }


    public void setUserName(String value) {
        this.userName = value;
    }

}


package com.programb.webservice.client;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "user", propOrder = {
    "age",
    "id",
    "name"
})
public class User {

    protected Integer age;
    protected Long id;
    protected String name;


    public Integer getAge() {
        return age;
    }


    public void setAge(Integer value) {
        this.age = value;
    }


    public Long getId() {
        return id;
    }


    public void setId(Long value) {
        this.id = value;
    }


    public String getName() {
        return name;
    }


    public void setName(String value) {
        this.name = value;
    }

}

package com.programb.webservice.config;

import javax.xml.ws.Endpoint;

import org.apache.cxf.Bus;
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 com.programb.webservice.service.ICommonService;


@Configuration
public class CxfConfig {
    @Autowired
    private Bus bus;

    @Autowired
    ICommonService commonService;

    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(bus, commonService);
        endpoint.publish("/CommonService");
        return endpoint;
    }
}

package com.programb.webservice.model;


public class User {
    private Long id;
    private String name;
    private Integer age;

    public User() {
    }

    public User(Long id, String name, Integer age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

package com.programb.webservice.service;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import com.programb.webservice.model.User;


@WebService(name = "CommonService", // 暴露服务名称
        targetNamespace = "http://model.webservice.programb.com/"// 命名空间,一般是接口的包名倒序
)
public interface ICommonService {
    @WebMethod
//    @WebResult(name = "String", targetNamespace = "")
    public String sayHello(@WebParam(name = "userName") String name);

    @WebMethod
//    @WebResult(name = "String", targetNamespace = "")
    public User getUser(@WebParam(name = "userName") String name);
}

package com.programb.webservice.service.impl;

import org.springframework.stereotype.Component;

import com.programb.webservice.model.User;
import com.programb.webservice.service.ICommonService;

import javax.jws.WebService;


@WebService(serviceName = "CommonService", // 与接口中指定的name一致
        targetNamespace = "http://model.webservice.programb.com/", // 与接口中的命名空间一致,一般是接口的包名倒
        endpointInterface = "com.programb.webservice.service.ICommonService"// 接口地址
)
@Component
public class CommonServiceImpl implements ICommonService {

    @Override
    public String sayHello(String name) {
        return "Hello ," + name;
    }

    @Override
    public User getUser(String name) {
        return new User(1000L, name, 23);
    }
}

package com.programb.webservice.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;


public class JacksonUtil {
    private static ObjectMapper mapper = new ObjectMapper();

    public static String bean2Json(Object obj) {
        try {
            return mapper.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return null;
        }
    }


    public static <T> T json2Bean(String jsonStr, TypeReference<T> typeReference) {
        try {
            return mapper.readValue(jsonStr, typeReference);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}

server.port: 8092

spring:
  profiles:
    active: dev

cxf:
  path: /services  # 替换默认的/services路径

logging:
  level:
    org.springframework.web.servlet: ERROR

spring:
  profiles: dev

logging:
  level:
    ROOT: INFO
    com:
      xncoding: DEBUG
  file: D:/programb/logs/app.log

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.programb</groupId>
    <artifactId>springboot-cxf</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>springboot-programb-restful</name>
    <description>WebService</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>

    
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
            <version>3.2.4</version>
        </dependency>
 

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-all</artifactId>
            <version>1.3</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.1</version>
                <configuration>
                   
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.20</version>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                </executions>
            </plugin>
        </plugins>

        <resources>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
    </build>

</project>
 类似资料: