SpringBoot+Mybatis-CXF WebService接口配置

项目结构图

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

1、pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <dependencies>
        <!--fastjson-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.46</version>
        </dependency>

		<!-- FastJson2 -->
		<dependency>
			<groupId>net.sf.json-lib</groupId>
			<artifactId>json-lib</artifactId>
			<version>2.2.3</version>
			<classifier>jdk15</classifier> <!-- 指定jdk版本 -->
		</dependency>

	    <!--webservice cxf-->
	    <dependency>
	        <groupId>org.apache.cxf</groupId>
	        <artifactId>cxf-rt-frontend-jaxws</artifactId>
	        <version>3.1.6</version>
	    </dependency>
	    <dependency>
	        <groupId>org.apache.cxf</groupId>
	        <artifactId>cxf-rt-transports-http</artifactId>
	        <version>3.1.6</version>
	    </dependency> 
  
    </dependencies>
</project>

2、CxfConfig.java

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.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.DispatcherServlet;
import com.styl.frame.core.service.ServiceDemo;
import com.styl.frame.core.service.UserService;
import com.styl.frame.core.service.impl.ServiceDemoImpl;
import com.styl.frame.core.service.impl.UserServiceImpl;

/**
 * @ClassName:CxfConfig
 * @Description:cxf发布webservice配置
 * @author 
 * @date:
 */
@Configuration
public class CxfConfig {

	
	@Bean("dispatcherServletCxf")
    public ServletRegistrationBean dispatcherServlet(){
        //return new ServletRegistrationBean(new CXFServlet(),"/webService/services/*");//发布服务名称	
        
        return new ServletRegistrationBean(new CXFServlet(),"/webService/*");//发布服务名称	
	}
	
    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }
    
    //------------------------接口配置------------(一)----------------------------------------------------------
    
    @Bean
    public ServiceDemo ServiceDemo() {
    	
        return new ServiceDemoImpl();
    }

    /** JAX-WS 
     * 站点服务(一)
     */
    @Bean
    public Endpoint endpoint_ServiceDemo() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), ServiceDemo());       
        endpoint.publish("/ServiceDemo");  
        //endpoint.getInInterceptors().add(new WsInterceptor()); //add webservice inteceptor   
        return endpoint;
    }
    
    //------------------------接口配置-----------(二)----------------------------------------------------------

	 @Bean
	 public UserService UserService() {
	  	
	      return new UserServiceImpl();
	 }

    /** JAX-WS 
     * 站点服务(二)
     */
    @Bean
    public Endpoint endpoint_UserService() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), UserService());       
        endpoint.publish("/UserService");       
        return endpoint;
    }

  //--------------------------------------------------------------------------------------------------------
  
}

3、接口及接口实现类

UserService.java

import javax.jws.WebParam;
import javax.jws.WebService;
/**
 * @ClassName:UserService
 * @Description:测试服务接口类
 *              include:两个测试方法
 * @author 
 * @date:
 */
@WebService
public interface UserService {
    public String getTs(@WebParam(name = "jsonStr") String jsonStr);
    public String getTsUser(@WebParam(name = "jsonStr") String jsonStr);
}

ServiceDemo.java

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

@WebService
public interface ServiceDemo {
	/*@WebMethod //标注该方法为webservice暴露的方法,用于向外公布,它修饰的方法是webservice方法,去掉也没影响的,类似一个注释信息。*/
	/*@WebResult(name="out") 可以省略不写*/
	public  String getValue(@WebParam(name = "param")String param);

	/*@WebMethod*/
	/*@WebResult(name="out")*/
	public  String getStatus();

    /*@WebMethod*/
    /*@WebResult(name="out")*/
    public String getUserName(@WebParam(name = "jsonStr") String jsonStr);
}

UserServiceImpl.java

import javax.jws.WebService;
import com.styl.frame.core.service.UserService;
import net.sf.json.JSONObject;
/**
 * @ClassName:UserServiceImpl
 * @Description:测试服务接口实现类
 * @author Maple
 * @date:2018年4月10日下午3:58:58
 */
@WebService(endpointInterface="com.styl.frame.core.service.UserService")//服务接口全路径, 指定做SEI(Service EndPoint Interface)服务端点接口
public class UserServiceImpl implements UserService {
	public String getTs(String jsonStr) {
		return null;
	}

	public String getTsUser(String jsonStr) {
		String  result  = "";
		JSONObject  jsr = JSONObject.fromObject(jsonStr);
		JSONObject  json =  new  JSONObject();
		try {
			String datas = jsr.getString("datas");
			json.put("datas",datas);
			json.put("msg","userServiceInterface");
			result  = json.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return  result;
	}
}

ServiceDemoImpl.java

import javax.jws.WebService;
import com.styl.frame.core.service.ServiceDemo;
import net.sf.json.JSONObject;


/*
@WebService(serviceName = "ServiceDemo", // 与接口中指定的name一致,可以省略
targetNamespace = "http://service.xxx.com/", // 与接口中的命名空间一致,一般是接口的包名倒,可以省略
endpointInterface = "com.xxx.service.ServiceDemo"// 接口地址
)
*/
/*
@WebService(serviceName="UserService",//对外发布的服务名
targetNamespace="http://service.core.frame.styl.com",//指定想要的名称空间,通常使用使用包名反转
endpointInterface="com.styl.frame.core.service.UserService")//服务接口全路径, 指定做SEI(Service EndPoint Interface)服务端点接口
*/
/*@WebService(serviceName = "ServiceDemo",targetNamespace = "http://service.core.frame.styl.com/",endpointInterface = "com.styl.frame.core.service.ServiceDemo")*/

@WebService(endpointInterface = "com.styl.frame.core.service.ServiceDemo")
public class ServiceDemoImpl implements ServiceDemo {
	public String getValue(String param) {
		return null;
	}
	public String getStatus() {
		return null;
	}

	public String getUserName(String jsonStr) {
		String  result = "";
		JSONObject  jsr =  JSONObject.fromObject(jsonStr);
		JSONObject  json = new  JSONObject();
		try {
			String data = jsr.getString("data");
			json.put("data",data);
			json.put("msg","测试数据!");
			result = json.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
}

参考文章:Spring Boot + cxf 整合WebService服务端和客户端

https://blog.csdn.net/qq_40760321/article/details/86621610?utm_medium=distribute.pc_relevant.none-task-blog-searchFromBaidu-8.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-searchFromBaidu-8.nonecase

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值