WebService:java配置类形式发布WebService接口及遇见的问题总结
Posted nhdlb
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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:java配置类形式发布WebService接口及遇见的问题总结的主要内容,如果未能解决你的问题,请参考以下文章
java 在连接SAP webservice的时候 给了用户名和密码。传递的参数是xml形式的。这些都怎么设置呢。