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、不要寫對象!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM