jeecg-boot 整合cxf实现SOAP WebService

jeecg-boot 整合cxf实现SOAP WebService

(一)引入依赖 Pom.xml

<!-- cxf webservice (暂时注释掉提高平台性能,需要此功能,可以放开注释)-->
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-frontend-jaxws</artifactId>
			<version>${
     cxf.version}</version>
		</dependency>
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-transports-http</artifactId>
			<version>${
     cxf.version}</version>
		</dependency>
		<!-- 服务端:用来生成JSONArray -->
		<dependency>
			<groupId>net.sf.json-lib</groupId>
			<artifactId>json-lib</artifactId>
			<version>${
     json-lib.version}</version>
			<classifier>jdk15</classifier>
		</dependency>
		<!-- 客户端:用来解析JSONArray -->
		<dependency>
			<groupId>com.google.code.gson</groupId>
			<artifactId>gson</artifactId>
		</dependency>

(二) 1.服务端代码 IMessageService

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

//声明一个访问接口
@WebService
public interface IMessageService {
     

    @WebMethod
    public String getMessary(@WebParam(name = "param") String param);
}

(二) 2.服务端代码 MessageServiceImpl

import net.sf.json.JSONArray;

import javax.jws.WebService;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

// name:暴露的服务名称;targetNamespace:命名空间,默认为本类包名倒写;endpointInterface:接口地址
@WebService(name="testCXF", targetNamespace="http://server.webservice.jeecg.org/",
        endpointInterface= "org.jeecg.webservice.server.IMessageService")
public class MessageServiceImpl implements IMessageService {
     

    @Override
    public String getMessary(String param) {
     
        List<Map<String, String>> list = new ArrayList<Map<String, String>>();
        Map<String, String> messaryMap1 = new HashMap<String, String>();
        messaryMap1.put("param", param);
        messaryMap1.put("name", "华闻");
        messaryMap1.put("sex", "男");
        messaryMap1.put("age", "18");
        Map<String, String> messaryMap2 = new HashMap<String, String>();
        messaryMap2.put("param", param);
        messaryMap2.put("name", "程勇");
        messaryMap2.put("sex", "女");
        messaryMap2.put("age", "18");
        list.add(messaryMap1);
        list.add(messaryMap2);
        // 返回JSON数组字符串
        return JSONArray.fromObject(list).toString();
    }
}


(三)服务发布


import org.jeecg.webservice.server.IMessageService;
import org.jeecg.webservice.server.MessageServiceImpl;
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.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.xml.ws.Endpoint;

/**
 * 服务发布
 */
@Configuration
public class CxfWebServiceConfig {
     
    /**
     *
     * */
    @Bean("cxfServletRegistration")
    public ServletRegistrationBean dispatcherServlet(){
     

        return new ServletRegistrationBean(new CXFServlet(),"/ws/*");
    }
    /**
     * 申明业务处理类 当然也可以直接 在实现类上标注 @Service
     */
    @Bean
    public IMessageService userService() {
     
        return new MessageServiceImpl();
    }

    /*
     * 非必要项
     */
    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
     
        SpringBus springBus = new SpringBus();
        return springBus;
    }

    /*
     * 发布endpoint  -1
     */
    @Bean
    public Endpoint endpoint( ) {
     
        EndpointImpl endpoint = new EndpointImpl(springBus(), userService());
        endpoint.publish("/m");//发布地址
        return endpoint;
    }


    /*
     * 发布endpoint  -2
     */
    @Bean
    public Endpoint endpoint2( ) {
     
        EndpointImpl endpoint = new EndpointImpl(springBus(), userService());
        endpoint.publish("/user2");//发布地址
        return endpoint;
    }

}


注意事项

jeecg-boot 整合cxf实现SOAP WebService_第1张图片
一.将文件放在 JeecgApplication 同级目录下,否则会扫描不到
二.在ShiroConfig.java 中 将拦截排除

	// cxf 服务排除 wsdl
	filterChainDefinitionMap.put("/ws/**", "anon");

你可能感兴趣的:(java,spring-boot,webservice,java,spring,boot,cxf,webservice)