码迷,mamicode.com
首页 > 编程语言 > 详细

WebService:java配置类形式发布WebService接口及遇见的问题总结

时间:2020-05-13 21:52:50      阅读:85      评论:0      收藏:0      [点我收藏+]

标签:pac   ring   http   point   code   包装   tor   sse   context   

配置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接口及遇见的问题总结

标签:pac   ring   http   point   code   包装   tor   sse   context   

原文地址:https://www.cnblogs.com/nhdlb/p/12884920.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!