WebService:java配置类形式发布WebService接口及遇见的问题总结
2021-01-23 11:14
标签:pac ring http point code 包装 tor sse context 配置WebService前需要以下依赖jar包 编写配置类 问题一:配置webService后,Controller不能访问,失效 解决方法: 问题二:前端跨域问题,需要后端配置跨域 WebService:java配置类形式发布WebService接口及遇见的问题总结 标签:pac ring http point code 包装 tor sse context 原文地址:https://www.cnblogs.com/nhdlb/p/12884920.html#版本只供参考,具体看项目
dependency>
grouId>org.apache.cxfgrouId>
artifactId>cxf-rt-frontend-jaxwsartifactId>
version>3.1.6version>
dependency>
dependency>
grouId>org.apache.cxfgrouId>
artifactId>cxf-rt-transports-httpartifactId>
version>3.1.6version>
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;
}
}
//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);
}
上一篇:python`最简单的爬虫`实现
下一篇:Java语言运算符
文章标题:WebService:java配置类形式发布WebService接口及遇见的问题总结
文章链接:http://soscw.com/index.php/essay/45858.html