基于spring开发webservice接口(soap接口与restful接口)

导入maven依赖

    <!--webService配置-->
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-frontend-jaxrs</artifactId>
        <version>3.0.5</version>
    </dependency>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-transports-http-jetty</artifactId>
        <version>3.0.5</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.3.6</version>
    </dependency>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-frontend-jaxws</artifactId>
        <version>3.1.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-transports-http</artifactId>
        <version>3.1.1</version>
    </dependency>

1、soap接口

(1)spring配置application-webservice.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:jaxws="http://cxf.apache.org/jaxws"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://cxf.apache.org/jaxws
	   http://cxf.apache.org/schemas/jaxws.xsd"
       default-autowire="byName">

       <jaxws:endpoint id="wsServiceSoap" implementor="com.webservice.impl.WsServiceSoapImpl"
                    address="/wsServiceSoap">
       </jaxws:endpoint>
</beans>

(2)在web.xml里面配置webservice

 <!--webService在web.xml的配置-->
    <servlet>
        <servlet-name>CXFServlet</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>CXFServlet</servlet-name>
        <url-pattern>/ws/*</url-pattern>
    </servlet-mapping>

(3)编写WsServiceSoap接口

@WebService(targetNamespace = "com.webservice")
public interface WsServiceSoap {
    public String syncUser(@WebParam(name = "userId") String userId);
}

(4)编写WsServiceSoap接口实现类

@Component("wsServiceSoap")
@WebService(endpointInterface = "com.webservice.WsServiceSoap"
        , targetNamespace = "com.webservice")
public class WsServiceSoapImpl  implements WsServiceSoap {
		public String syncUser(@WebParam(name = "userId") String userId) {
			//业务代码
			System.out.println(userId);
			return "success";
		}
}

运行后在浏览器访问http:ip:port/ws/wsServiceSoap?wsdl即可看到接口信息,此时webservice的soap接口可以正常访问了,可以通过soapUI软件访问wsdl接口

2、restful接口

(1)spring配置application-cxf.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:jaxrs="http://cxf.apache.org/jaxrs"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://cxf.apache.org/jaxrs
       http://cxf.apache.org/schemas/jaxrs.xsd">

    <!--webService配置-->
    <jaxrs:server address="/synApi/syncUser">
        <jaxrs:serviceBeans>
            <bean class="com.webservice.impl.WsServiceImpl"></bean>
        </jaxrs:serviceBeans>
    </jaxrs:server>
</beans>

(2)在web.xml里面配置webservice

 <!--webService在web.xml的配置-->
    <servlet>
        <servlet-name>CXFServlet</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>CXFServlet</servlet-name>
        <url-pattern>/ws/*</url-pattern>
    </servlet-mapping>

(3)编写WsService接口

@WebService
public interface WsService {
    @WebMethod
    public String syncUser();
}

(4)编写WsService接口的实现类

@Component("wsServiceImpl")
@WebService(endpointInterface = "com.webservice.WsService")
public class WsServiceImpl  implements WsService {
		//这里通过这个对象接收数据,可根据实际情况接收数据
	    private WsUserTpm wsUserTpm;

	@Override
    @Consumes(MediaType.APPLICATION_JSON)		   //接收json的数据
    @Produces(MediaType.APPLICATION_JSON)          //返回json格式的数据
    @POST                                          //提交方式
    public String syncUser() {
    	System.out.println(wsUserTpm);
    	return "success";
    }
}

此时可以用postman工具访问接口http:ip:port/ws/synApi/syncUser进行测试

  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值