springboot2 with cxf3.3

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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());
	}

}

  

以上是关于springboot2 with cxf3.3的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot 2 和 Security With JWT 无法提供 Angular 构建的静态内容

Spring Boot 2.1.0 @Controller 不工作?

Spring Data + View with Union 返回重复行

Spring Boot with Security不提供我的静态内容

springboot2.0结合webuploader实现文件分片上传

springboot2.0处理任何异常返回通用数据格式