WebService:java配置类形式发布WebService接口及遇见的问题总结
配置WebService前需要以下依赖jar包
#版本只供参考,具体看项目
<dependency>
<grouId>org.apache.cxf</grouId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.6</version>
</dependency>
<dependency>
<grouId>org.apache.cxf</grouId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.6</version>
</dependency>
编写配置类
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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.http.MediaType;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBassedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.DispatcherServlet; @Configuration
public class CxfConfig{ //配置WebService的前缀路径
@Bean
public ServletRegistrationBean dispatcherServlet(){
return new ServletRegistrationBean(new CXFServlet(), "/path/*");
} @Bean
public SpringBus springBus(){ return new SpringBus(); } //实例化@webservice接口的实现类
@Bean
public ****Impl *****Impl(){ return new ******Impl(); } //配置接口暴露路径(后缀路径)
@Bean
public Endpoint endpoint(){
EndpointImpl endpoint = new EndpointImpl(springBus(), *****Impl());
endpoint.publish("/Impl");
return endpoint;
} }
问题一:配置webService后,Controller不能访问,失效
解决方法:
//controller失效,需要配置新的DispatcherServlet来扫描Controller的类
@Bean
public ServletRegistrationBean restServlet(){ //注解扫描上下文
AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
//扫描Controller所在的路径
applicationContext.scan("com.brinfo.java.controller");
//通过构建函数指定dispatcherServlet的上下文
DispatcherServlet rest_dispatcherServlet = new DispatcherServlet(applicationContext);
//用SetvletRegistrationBean包装servlet
ServletRegistrationBean registrationBean = new ServletRegistrationBean(rast_dispatcherServlet);
//优先级
registrationBean.setLoadOnStartup(1);
//指定urlmapping
registrationBean.addUrlMappings("/*"); return registrationBean;
}
问题二:前端跨域问题,需要后端配置跨域
private CorsConfiguration corsConfiguration(){
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.addAllowedOrigin("*");
corsConfiguration.addAllowedHeader("*");
corsConfiguration.addAllowedMethod("*");
corsConfiguration.setAllowCredentials(true);
corsConfiguration.setMaxAge(360011);
return corsConfiguration;
} //解决跨域问题
@Bean
public CorsFilter corsFilter(){
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**",corsConfiguration());
return new CorsFilter(source);
}
问题三:webService启动报错:Factory method 'endpoint' threw exception; nested exception is javax.xml.WebServiceException:org.apache.cxf.service.factory.ServiceConstructionException
记住!这个报错的原因之一 webService的入参和返回值可以void、String、不要写对象!
WebService:java配置类形式发布WebService接口及遇见的问题总结的更多相关文章
- spring 配置 Java配置类装配bean
https://www.cnblogs.com/chenbenbuyi/p/8457700.html 自动化装配的确有很大的便利性,但是却并不能适用在所有的应用场景,比如需要装配的组件类不是由自己的应 ...
- 我写了一个java实体类,implements了Serializable接口,然后我如何让serialversionUID自动生成
写了一个java实体类,implements了Serializable接口,让serialversionUID自动生成方法: 1.点击类旁边的警告符号: 2.选择Add generated seria ...
- day63-webservice 06.在web项目中发布以类的形式发布webservice
真正用的时候都是需要部署在WEB服务器里面. 不能写主函数来发布了,需要借助于我们WEB. 4.配置web.xml, <!DOCTYPE web-app PUBLIC "-//Sun ...
- java调用oracle数据库发布WebService
package com.hyan.service; import java.io.FileInputStream;import java.sql.Connection;import java.sql. ...
- 真正的轻量级WebService框架——使用JAX-WS(JWS)发布WebService
WebService历来都很受重视,特别是Java阵营,WebService框架和技术层出不穷.知名的XFile(新的如CXF).Axis1.Axis2等. 而Sun公司也不甘落后,从早期的JAX-R ...
- 真正的轻量级WebService框架——使用JAX-WS(JWS)发布WebService(转载)
WebService历来都很受重视,特别是Java阵营,WebService框架和技术层出不穷.知名的XFile(新的如CXF).Axis1.Axis2等. 而Sun公司也不甘落后,从早期的JAX-R ...
- java普通类、抽象类、接口、面向对象理解
1.面向对象编程: 面向对象编程的语言不止java一种,还有: simula 67(第一个面向对象语言,支持单继承和一定含义的多态和部分动态绑定): Smalltalk(第一个支持动态类型的语言,支持 ...
- SSM框架新特性关于用Java配置类完全代替XML
项目目录结构 从Spring3.0,@Configuration用于定义配置类,可替换xml配置文件,被注解的类内部包含有一个或多个被@Bean注解的方法, 这些方法将会被AnnotationConf ...
- java 调用 wsdl形式的webservice 示例
import java.rmi.RemoteException; import javax.xml.rpc.ParameterMode; import javax.xml.rpc.ServiceExc ...
随机推荐
- C++知识点案例 笔记-6
1.三种友元函数 -非模板友元函数 -约束模板友元函数 -非约束模板友元函数 2.非类型参数 3.模板特化 1.三种友元函数 =====三种友元函数===== --1---非模板友元函数 #inclu ...
- 047.Python前端html
一 HTTP协议 1.1 HTTP请求 URL: 协议/IP:端口/路径?GET参数 基于请求响应 请求协议格式 GET URL路径?a=1&b=2 HTTP # 请求首行 user-age ...
- python基础之流程控制(if判断和while、for循环)
程序执行有三种方式:顺序执行.选择执行.循环执行 一.if条件判断 1.语句 (1)简单的 if 语句 (2)if-else 语句 (3)if-elif-else 结构 (4)使用多个 elif 代码 ...
- python3 列表转换为字符串
join将列表转换为字符串 list1 = ["张三","李四","王五"] a1 = ','.join(list1) print(a1) ...
- python程序打包成exe(使用pyinstaller)
pyinstaller下载地址:https://github.com/pyinstaller/pyinstaller/ (这个文件能够自动安装依赖项,其他版本的貌似还要自己安装依赖项) 下载之后解压到 ...
- 第六章 XaaS和IT服务标准
从云计算(Cloud Computing)谈起 云计算是一种按使用量付费的模式,这种模式提供可用的.便捷的.按需的网络访问,进入可配置的计算资源共享池(资源包括网络,服务器,存储,应用软件,服务),这 ...
- flink反压的监控
反压在流式系统中是一种非常重要的机制,主要作用是当系统中下游算子的处理速度下降,导致数据处理速率低于数据接入的速率时,通过反向背压的方式让数据接入的速率下降,从而避免大量数据积压在flink系统中,最 ...
- Activiti中工作流的生命周期详细解析!一个BPMN流程示例带你认识项目中流程的生命周期
BPMN 2.0介绍 业务流程模型注解(BusinessProcess Modeling Notation - BPMN)是业务流程模型的一种标准图形注解.这个标准是由对象管理组(Object Man ...
- GO学习-(32) Go实现日志收集系统1
Go实现日志收集系统1 项目背景 每个系统都有日志,当系统出现问题时,需要通过日志解决问题 当系统机器比较少时,登陆到服务器上查看即可满足 当系统机器规模巨大,登陆到机器上查看几乎不现实 当然即使是机 ...
- 在gitlab网页上合并分支
在gitlab网页上合并分支 使用gitlab网页将代码合并分 下面将dev分支代码合并至master 1.点击request merge 2.源分支为当前分支,目标分支默认为master,确认无误, ...