zoukankan      html  css  js  c++  java
  • springboot2 with cxf3.3

    config类:

    package com.cxf.config;
    
    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.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
    import org.springframework.boot.web.server.WebServerFactoryCustomizer;
    import org.springframework.boot.web.servlet.ServletRegistrationBean;
    import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    import com.cxf.service.HelloService;
    import com.cxf.service.HelloServiceImpl;
    
    @Configuration
    public class CustomizationBean implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
    
    	@Override
    	public void customize(ConfigurableServletWebServerFactory server) {
    		server.setPort(9090);
    	}
    	/**
    	 * @return
    	 * 使用此方法和上面的功能一致,只不过可配置项增多,只要其中一个即可
    	 */
    	@Bean
    	public ConfigurableServletWebServerFactory webServerFactory() {
    		TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
    		factory.setPort(9090);
    		return factory;
    	}
    	@Bean
    	public ServletRegistrationBean servletRegistrationBean() {
    		return new ServletRegistrationBean(new CXFServlet(), "/services/*");
    	}
    	
    	@Bean(name = Bus.DEFAULT_BUS_ID)
        public SpringBus springBus()
        {
            return  new SpringBus();
        }
    
        @Bean
        public HelloService helloService()
        {
            return  new HelloServiceImpl();
        }
    
        @Bean
        public Endpoint endpoint() {
            EndpointImpl endpoint=new EndpointImpl(springBus(), helloService());//绑定要发布的服务
            endpoint.publish("/hello"); //显示要发布的名称
            return endpoint;
        }
        //本地输入http://localhost:9090/services/hello?wsdl就可以了
    
    }
    

      

    service类

    package com.cxf.service;
    
    import javax.jws.WebMethod;
    import javax.jws.WebParam;
    import javax.jws.WebService;
    
    
    @WebService
    public interface HelloService {
    	@WebMethod
    	String sayHello(@WebParam(name = "name") String name);
    
    }
    

    serviceImpl类:

    package com.cxf.service;
    
    import javax.jws.WebService;
    //备注说明:接口实现类名称前的注解targetNamespace是当前类实现接口所在包名称的反序(PS:加上反斜线),endpointInterface是当前需要实现接口的全称;
    @WebService(targetNamespace="http://service.cxf.com/",endpointInterface="com.cxf.service.HelloService",serviceName="HelloService")
    public class HelloServiceImpl implements HelloService{
    
    	@Override
    	public String sayHello(String name) {
    		System.out.println("your name is:"+name);
    		return "hello "+ name;
    	}
    
    }
    

      

    启动服务:

    package com;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class Starter {
    
    	public static void main(String[] args) {
    		SpringApplication.run(Starter.class, args);
    		System.out.println("启动成功.....");
    	}
    
    }
    

      

    客户端调用:

    package com.cxf.client;
    
    import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
    
    public class HelloClient {
    	public static void main(String args[]) throws Exception {
    		JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
    		org.apache.cxf.endpoint.Client client = dcf.createClient("http://localhost:9090/services/hello?wsdl");
    		Object[] objects = client.invoke("sayHello", "wangsong");
    		// 输出调用结果  *****hello wangsong
    		System.out.println("*****" + objects[0].toString());
    	}
    
    }
    

      

  • 相关阅读:
    spring boot cli 知识点
    OSX Homebrew 安装 Spring Boot CLI
    前端重定向,index.html文件被浏览器缓存,导致整个应用都是旧的
    单页面应用,接入cdn
    Spring Cloud 之 Hystrix 知识点:隔离、熔断、降级
    Spring Cloud 之 Ribbon 知识点:服务器负载均衡
    Spring Cloud 之 Feign 知识点:封装了 REST 调用
    spring cloud 学习资料
    Gradle 知识点
    Gradle 学习资料
  • 原文地址:https://www.cnblogs.com/JAYIT/p/12606348.html
Copyright © 2011-2022 走看看