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

springboot2 with cxf3.3

时间:2020-03-31 17:17:23      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:service   static   over   ext   client   本地   server   cal   new t   

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());
	}

}

  

springboot2 with cxf3.3

标签:service   static   over   ext   client   本地   server   cal   new t   

原文地址:https://www.cnblogs.com/JAYIT/p/12606348.html

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