Spring boot 整合CXF webservice 全部被拦截的问题_springboot cxf 被拦截-程序员宅基地

技术标签: java  webservice  

Spring Boot集成webService

服务端

使用idea创建spring boot工程:

“File”→“New”→“Project”→“Spring Initializr”……

在pom添加依赖

<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.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-frontend-jaxws</artifactId>
    <version>3.1.6</version>
</dependency>
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-transports-http</artifactId>
    <version>3.1.6</version>
</dependency>

在application.properties中添加配置

这个配置根据实际需求添加,如果不修改server.port,服务器会默认从8080端口启动,为避免冲突,这里设置服务端口为8090。

server.port=8090
  •  

提供webservice接口

import javax.jws.WebService;

@WebService
public interface DemoService {
    public String sayHello(String user);
}

实现webservice的方法

import java.util.Date;

public class DemoServiceImpl implements DemoService {
    @Override
    public String sayHello(String user) {
        return user+":hello"+"("+new Date()+")";
    }
}

配置并发布

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 javax.xml.ws.Endpoint;


@Configuration
public class CxfConfig {

    @Bean
    public ServletRegistrationBean dispatcherServlet() {
        return new ServletRegistrationBean(new CXFServlet(),"/demo/*");
    }
    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }
    @Bean
    public DemoService demoService() {
        return new DemoServiceImpl();
    }
    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), demoService());
        endpoint.publish("/api");
        return endpoint;
    }
}

启动服务

直接启动WebserviceApplication,看到服务器正常启动 
这里写图片描述

查看系统提供的webservice接口

在浏览器输入http://localhost:8090/demo/ 可以看到系统提供的webservice服务 
这里写图片描述

客户端

创建新项目

通过wsdl生成Java代码

这里写图片描述
Web service wsdl url 填入服务端WSDL地址 
这里写图片描述 
如果使用的是JDK1.8可能会有bug,生成时报错:由于 accessExternalSchema 属性设置的限制而不允许 ‘file’ 访问, 因此无法读取方案文档 ‘xjc.xsd’

org.xml.sax.SAXParseException; 
systemId: jar:file:/D:/apache-cxf-2.7.11/apache-cxf-2.7.11/lib/jaxb-xjc2.2.6.jar!/com/sun/tools/xjc/reader/xmlschema/bindinfo/binding.xsd; 
lineNumber: 52; columnNumber: 88; schema_reference: 
由于 accessExternalSchema 属性设置的限制而不允许 'file' 访问, 因此无法读取方案文档 'xjc.xsd'。

解决方法: 
在jdk的安装路径下,如 C:\Java\jdk1.8.0_65\jre\lib,添加一个属性文件jaxp.properties,并写上如下内容javax.xml.accessExternalSchema = all

成功执行后可以看到mypackage多了很多文件

这里写图片描述

我们可以直接调用DemoServiceImplService提供的webservice接口,就像使用本地的方法一样。这里在单元测试中直接调用:

import mypackage.DemoServiceImplService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class WebserviceApplicationTests {

    @Test
    public void contextLoads() {
        DemoServiceImplService webServiceImpl = new DemoServiceImplService();
        String result = webServiceImpl.getDemoServiceImplPort().sayHello("没有说");
        System.out.println("===========================================");
        System.out.println(result);
        System.out.println("===========================================");
    }
}

执行结果: 
这里写图片描述

 

成功集成cxf后,发现只有webservice服务可以正常使用,其他请求url全部无法正常访问。

然后在cxf 配置文件中

WebServiceCxfCfg.java

更改此方法名:public ServletRegistrationBean dispatcherServlet()  

 

@Bean
public ServletRegistrationBean disServlet(){

    return new ServletRegistrationBean(new CXFServlet() , "/services/*");
}

即可成功访问其他url

评论中说的是public ServletRegistrationBean dispatcherServlet() 把默认映射覆盖掉了,把这个名字改掉,控制类方法就能访问了。

更改此方法明后可以正常其他请求url,webservice服务也正常。

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_16660569/article/details/91990842

智能推荐

(代码准确运行)如何获取浏览器URL中查询字符串中的参数_获取浏览器url中查询字符串的参数-程序员宅基地

如何获取浏览器URL中查询字符串中的参数首先要了解的是URL中查询字符串中的参数是什么,例如有一个URL地址为:"http://www/book.com/jquery/bookmannager.html?channelid=12345&amp;name=xiaoming&amp;age=23"; 那么?号后边的一串字符就是获取到的参数,下面请看代码:&lt;!DOCTYPE html&g..._获取浏览器url中查询字符串的参数

Nginx解决前端跨域问题_nginx access-control-max-age: 前端访问显示-程序员宅基地

记一次使用nginx解决跨域问题的配置server{ listen 80; server_name xxx.xxx.com; location /ok { proxy_pass http://xxx.xx.xx:3000; # 指定允许跨域的方法,*代表所有 add_header Access-Control-A..._nginx access-control-max-age: 前端访问显示

在ts中使用dayjs-程序员宅基地

分为两种情况:没有在tsconfig.json中进行如下配置"compilerOptions": { "esModuleInterop": true, "allowSyntheticDefaultImports": true, }在使用dayjs的页面中通过如下进行导入important * as dayjs from 'dayjs';如果进行了配置,则需要通过如下方式导入important dayjs from 'dayjs';...

HOWTO: Connect to a Running Instance of Internet Explorer-程序员宅基地

PSS ID Number: Q176792Article Last Modified on 07-20-2001The information in this article applies to: Microsoft Internet Explorer (Programming) 4.0, 4.01, 5 Microsoft Internet Clie

No timezone mapping entry for ‘Shanghai‘-程序员宅基地

用UTC即可,com.mysql.cj.exceptions.WrongArgumentException: No timezone mapping entry for ‘UTC+8’.

Java-routine_java routine-程序员宅基地

基础知识编程语言:java python c++基本算法思想基本网络知识:tcp/ip http/https工具方面操作系统:linux (CentOS\Ubuntu\Fe…)代码管理:svn/git持续集成(CI/CD):jenkinsjava的项目管理工具:maven/gradle框架方面ssh (spring+structs+hibernate)(已过时)ssm:spr..._java routine

随便推点

Windows10远程桌面连接提示:出现身份验证错误,要求的函数不受支持...-程序员宅基地

错误信息:出现身份验证错误,要求的函数不受支持...解决办法:1、按Windows键+R,或者在CMD命令窗口,输入“运行”,打开运行窗口,2、输入regedit,打开注册表编辑器,找到路径:计算机\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System然后在System文件夹内创建文件夹项:\CredSSP\Parameters然后在Parameters文件夹内,新建 DW

asm diskgroup中的failgroup--故障组_asm diskgroup failgrop-程序员宅基地

[grid@rhel63single ~]$ sqlplus / as sysdbaSQL*Plus: Release 11.2.0.4.0 Production on Fri Sep 18 02:10:06 2015Copyright (c) 1982, 2013, Oracle. All rights reserved.Connected to:Oracle Database_asm diskgroup failgrop

试题录入办法-程序员宅基地

http://ka.hanwang.com.cn/2016/jyly_0629/53.html https://wenku.baidu.com/activity/topic/2016technical_cooperation

“被遗忘的宇航员”逝世,他是阿波罗11号上唯一没登月的人_QbitAl的博客-程序员宅基地

梦晨 发自 凹非寺 量子位 报道 | 公众号 QbitAI迈克尔·柯林斯因癌症去世,享年90岁。这个名字可能对你而言很陌生,他是阿波罗11号进行人类第一次登月时,唯一留在飞船上的那个人,没...

无线自组织网的通信协议栈-程序员宅基地

无线自组织网的通信协议栈(1)物理层。在无线自组织网的通用协议栈结构中,物理层主要负责频率选择,无线信号的检测、调制解调、信道编码、信号的收/发等,IEEE802.11系列、蓝牙、ZigBee和超宽带等规范都是具体的物理层协议(2)数据链路层。无线自组网的数据链路层负责在不可靠的无线链路上间建立可靠和安全的逻辑链路,完成媒体接入控制、数据的传送、同步、纠错及流量控制等功能。数据链路层了细分为媒体访问控制子层和逻辑链路控制子层。数据链路层中的MAC子层控制移动节点对无线信道的访问,他可以采用随机竞争机制,

Cohen-Sutherland算法(转载)-程序员宅基地

一、Cohen-Sutherland算法思想:  该算法也称为编码算法,首先对线段的两个端点按所在的区域进行分区编码,根据编码可以迅速地判明全部在窗口内的线段和全部在某边界外侧的线段。只有不属于这两种情况的线段,才需要求出线段与窗口边界的交点,求出交点后,舍去窗外部分。  对剩余部分,把它作为新的线段看待,又从头开始考虑。两遍循环之后,就能确定该线段是部分截留下来,还是全部舍弃。

推荐文章

热门文章

相关标签