• springBoot 整合 CXF 开发 webService 接口


    创建springboot项目

    pom.xml文件

    <?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>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.3.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.juli</groupId>
        <artifactId>WebServiceDemo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
        <name>WebServiceDemo</name>
        <description>Demo project for Spring Boot</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-starter-web-services</artifactId>
            </dependency>
            
            <dependency>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-rt-frontend-jaxws</artifactId>
                <version>3.2.6</version>
            </dependency>
            
            <dependency>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-rt-frontend-jaxws</artifactId>
                <version>3.2.6</version>
            </dependency>
            
            <dependency>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-rt-transports-http</artifactId>
                <version>3.2.6</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
                <scope>provided</scope>
            </dependency>
            
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>

     编写service类

      为了看出效果随便写了几个业务接口。

    package com.juli.webServiceDemo.service;
    
    import javax.jws.WebService;
    
    @WebService(name="UserService",targetNamespace="http://service.webServiceDemo.juli.com")
    public interface UserService {
    
        public String getUser(String name);
     
        public String listUser();
        
        public String deleteUser(String id);
        
        public String updateUser(String id);
    }
    package com.juli.webServiceDemo.service.impl;
    
    import java.util.Date;
    
    import javax.jws.WebService;
    
    import com.juli.webServiceDemo.service.UserService;
    
    @WebService(name="UserService",targetNamespace="http://service.webServiceDemo.juli.com",endpointInterface="com.juli.webServiceDemo.service.UserService")
    public class UserServiceImpl implements UserService{
    
        @Override
        public String getUser(String name) {
            String result = "";
            try {
                result = "UserName:" + name + " - Date:"+ new Date(); 
            } catch (Exception e) {
                e.printStackTrace();
            }
            return result;
        }
    
        @Override
        public String listUser() {
            String result = "";
            try {
                result = "Date:"+ new Date(); 
            } catch (Exception e) {
                e.printStackTrace();
            }
            return result;
        }
    
        @Override
        public String deleteUser(String id) {
            String result = "";
            try {
                result = "UserId:" + id + " - Date:"+ new Date(); 
            } catch (Exception e) {
                e.printStackTrace();
            }
            return result;
        }
    
        @Override
        public String updateUser(String id) {
            String result = "";
            try {
                result = "UserId:" + id + " - Date:"+ new Date(); 
            } catch (Exception e) {
                e.printStackTrace();
            }
            return result;
        }
    
    }

    config类

    package com.juli.webServiceDemo.config;
    
    import javax.xml.ws.Endpoint;
    
    import org.apache.cxf.Bus;
    import org.apache.cxf.bus.spring.SpringBus;
    import org.apache.cxf.jaxws.EndpointImpl;
    import org.apache.cxf.transport.servlet.CXFServlet;
    import org.springframework.boot.web.servlet.ServletRegistrationBean;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    import com.juli.webServiceDemo.service.UserService;
    import com.juli.webServiceDemo.service.impl.UserServiceImpl;
    
    @Configuration
    public class WebServiceConfig {
    
        @Bean
        public ServletRegistrationBean<CXFServlet> dispatcherServlet() {
            return new ServletRegistrationBean<CXFServlet>(new CXFServlet(),"/webServiceDemo/*");
        }
     
        @Bean(name = Bus.DEFAULT_BUS_ID)
        public SpringBus springBus() {
            return new SpringBus();
        }
     
        @Bean
        public UserService userService() {
            return new UserServiceImpl();
        }
     
        @Bean
        public Endpoint endpoint() {
            EndpointImpl endpoint = new EndpointImpl(springBus(), userService());
            endpoint.publish("/api");
            return endpoint;
        }
    }

    启动springboot 访问 http://localhost:8888/webServiceDemo/

    遇到问题

      第一次启动报错,网上查了一下应该是版本的问题,一开始使用的是

      parent ~> 2.1.4 后来改成  2.0.3版本的就没有问题

    ***************************
    APPLICATION FAILED TO START
    ***************************
    
    Description:
    
    Parameter 1 of constructor in org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration required a bean of type 'org.springframework.boot.autoconfigure.web.servlet.DispatcherServletPath' that could not be found.
    	- Bean method 'dispatcherServletRegistration' not loaded because DispatcherServlet Registration found non dispatcher servlet dispatcherServlet
    
    
    Action:
    
    Consider revisiting the entries above or defining a bean of type 'org.springframework.boot.autoconfigure.web.servlet.DispatcherServletPath' in your configuration.

    2.1.4、2.1.3好像都有这个问题

  • 相关阅读:
    Nginx日志
    Aapche日志
    IIS日志
    pikachu 不安全的url重定向
    pikachu php反序列化、XXE、SSRF
    pikachu 越权漏洞
    pikachu 不安全的文件下载和上传
    pikachu Files Inclusion(文件包含漏洞)
    pikachu RCE部分(远程命令、代码执行漏洞)
    pikachu SQL部分(下)
  • 原文地址:https://www.cnblogs.com/pengjf/p/10490571.html
Copyright © 2020-2023  润新知