配置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接口及遇见的问题总结的更多相关文章

  1. spring 配置 Java配置类装配bean

    https://www.cnblogs.com/chenbenbuyi/p/8457700.html 自动化装配的确有很大的便利性,但是却并不能适用在所有的应用场景,比如需要装配的组件类不是由自己的应 ...

  2. 我写了一个java实体类,implements了Serializable接口,然后我如何让serialversionUID自动生成

    写了一个java实体类,implements了Serializable接口,让serialversionUID自动生成方法: 1.点击类旁边的警告符号: 2.选择Add generated seria ...

  3. day63-webservice 06.在web项目中发布以类的形式发布webservice

    真正用的时候都是需要部署在WEB服务器里面. 不能写主函数来发布了,需要借助于我们WEB. 4.配置web.xml, <!DOCTYPE web-app PUBLIC "-//Sun ...

  4. java调用oracle数据库发布WebService

    package com.hyan.service; import java.io.FileInputStream;import java.sql.Connection;import java.sql. ...

  5. 真正的轻量级WebService框架——使用JAX-WS(JWS)发布WebService

    WebService历来都很受重视,特别是Java阵营,WebService框架和技术层出不穷.知名的XFile(新的如CXF).Axis1.Axis2等. 而Sun公司也不甘落后,从早期的JAX-R ...

  6. 真正的轻量级WebService框架——使用JAX-WS(JWS)发布WebService(转载)

    WebService历来都很受重视,特别是Java阵营,WebService框架和技术层出不穷.知名的XFile(新的如CXF).Axis1.Axis2等. 而Sun公司也不甘落后,从早期的JAX-R ...

  7. java普通类、抽象类、接口、面向对象理解

    1.面向对象编程: 面向对象编程的语言不止java一种,还有: simula 67(第一个面向对象语言,支持单继承和一定含义的多态和部分动态绑定): Smalltalk(第一个支持动态类型的语言,支持 ...

  8. SSM框架新特性关于用Java配置类完全代替XML

    项目目录结构 从Spring3.0,@Configuration用于定义配置类,可替换xml配置文件,被注解的类内部包含有一个或多个被@Bean注解的方法, 这些方法将会被AnnotationConf ...

  9. java 调用 wsdl形式的webservice 示例

    import java.rmi.RemoteException; import javax.xml.rpc.ParameterMode; import javax.xml.rpc.ServiceExc ...

随机推荐

  1. Scala 中为什么不建议用 return 关键字

    在scala中使用 return 的话,编译的时候会提示the latest statement is method is automatically returned, use of th retu ...

  2. html-新闻滚动条

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. node.js学习(3)模块

    1.创建文件 count.js 2 调用 3 改造 4 调用 5 再改造 6 在再改造

  4. nvGraph-NVIDIA图形库

    nvGraph-NVIDIA图形库 数据分析是高性能计算的不断增长的应用.许多高级数据分析问题可以称为图形问题.反过来,当今许多常见的图形问题也可以称为稀疏线性代数.这是nvGraph的动机,它利用G ...

  5. Nucleus-SE迁移:未实现的设施和兼容性

    Nucleus-SE迁移:未实现的设施和兼容性 Nucleus SE migration: Unimplemented facilities and compatibility Nucleus SE的 ...

  6. Django(55)GenericAPIView源码分析

    源码分析 GenericAPIView继承自APIView,也就是在APIView基础上再做了一层封装,源码如下: class GenericAPIView(views.APIView): query ...

  7. SpringCloud Alibaba实战(4:基本开发框架搭建)

    在上一节,我们已经完成了项目的整体技术架构设计和具体的数据库设计,接下来,我们搭建整体的开发框架. 开发工具选用Idea. 开发工具只是为了提高效率,如果不习惯Idea的话,STS使用起来也是OK的. ...

  8. python_selenium 框架代码的优化方向

  9. 9, java数据结构和算法: 直接插入排序, 希尔排序, 简单选择排序, 堆排序, 冒泡排序,快速排序, 归并排序, 基数排序的分析和代码实现

    内部排序: 就是使用内存空间来排序 外部排序: 就是数据量很大,需要借助外部存储(文件)来排序. 直接上代码: package com.lvcai; public class Sort { publi ...

  10. JMeter定时器设置延迟与同步

    JMeter定时器一般用来设置延迟与同步.它的作用域和优先级如下: 定时器的优先级高于Sampler. 在同一作用域(比如控制器下)有多个定时器存在,每个定时器都会执行. 在某一Sampler节点下的 ...