博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring boot 整合CXF webservice 全部被拦截的问题
阅读量:5748 次
发布时间:2019-06-18

本文共 3914 字,大约阅读时间需要 13 分钟。

hot3.png

Spring Boot集成webService

服务端

使用idea创建spring boot工程:

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

在pom添加依赖

org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-web-services
org.springframework.boot
spring-boot-starter-test
test
org.apache.cxf
cxf-rt-frontend-jaxws
3.1.6
org.apache.cxf
cxf-rt-transports-http
3.1.6

在application.properties中添加配置

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

server.port=8090
  •  

提供webservice接口

import javax.jws.WebService;@WebServicepublic 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;@Configurationpublic 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接口

在浏览器输入 可以看到系统提供的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)@SpringBootTestpublic 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()  

 

@Beanpublic ServletRegistrationBean disServlet(){    return new ServletRegistrationBean(new CXFServlet() , "/services/*");}

即可成功访问其他url

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

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

转载于:https://my.oschina.net/openoschina/blog/1941772

你可能感兴趣的文章
Spring之旅第八站:Spring MVC Spittr舞台的搭建、基本的控制器、请求的输入、表单验证、测试(重点)...
查看>>
数据结构与算法——常用排序算法及其Java实现
查看>>
你所不知的Webpack-多种配置方法
查看>>
webpack+typescript+threejs+vscode开发
查看>>
python读excel写入mysql小工具
查看>>
如何学习区块链
查看>>
搜索问题的办法
查看>>
微信分销系统商城营销5大重点
查看>>
求职准备 - 收藏集 - 掘金
查看>>
Linux-Centos启动流程
查看>>
php 设计模式
查看>>
后端技术精选 - 收藏集 - 掘金
查看>>
Laravel 服务容器
查看>>
mac安装kubernetes并运行echoserver
查看>>
多页架构的前后端分离方案(webpack+express)
查看>>
算法(第4版) Chapter 1
查看>>
前端技术选型的遗憾和经验教训
查看>>
“亲切照料”下的领域驱动设计
查看>>
SRE工程师到底是做什么的?
查看>>
解读:Red Hat为什么收购Ansible
查看>>