WebService的基本用法

CXF WebService开发:实现不同项目之间的数据通信

1.两种开发方式:WS和RS,但是每一种方式都能独立发布和与spring整合:一共有4种;

WS:只支持xml格式的数据,用的协议手机soap协议

RS:支持xml和json ,用的http协议

RS:访问同一路径的时候,根据请求的方式不同,来访问不同资源

@GET @PUT @POST @DELETE

好处:1.有利于代码的整洁,2.  支持多种数据格式    3.更容易做缓存 (第一次从数据库查。然后304)

现在用的最多的RS与spring整合的方式;这里只说这一种;

分为服务端和客户端

1.服务端(maven工程)

步骤1:导4个依赖 1cxf-rt-fronted-jaxrs  

                              2.cxf-rt-rs-client

                json依赖3.cxf-rt-rs-extension-providers

                             4.jettison

步骤2:写实体类:如果作为数据传输的实体要加上注解@XmlRootElement(name="Car")

步骤3:web.xml配置servlet:CXFServlet  url-patern:/service/*

步骤4:服务端还需要写applicationContext-*.xml 

            1.导名称空间jjaxrs

            2.配置服务端(接口)

步骤5:写接口,在接口方法上写注释

2.客户端

用WebClient发起请求:一些固定的API,网上一大推的百度一下就能找到

附上一个小demo

-------------------------------------------------服务端代码

1.web.xml

<!-- spring配置文件位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- spring核心监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
<servlet-name>CXFService</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFService</servlet-name>
<url-pattern>/services/*</url-pattern>

</servlet-mapping>

2.a'pplicationContext-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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:task="http://www.springframework.org/schema/task"
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://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/data/jpa 
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd ">

<jaxrs:server id="customerService" address="/customerService">
<jaxrs:serviceBeans>
<bean class="cn.itcast.crm.service.impl.CustomerServiceImpl" />
</jaxrs:serviceBeans>
<jaxrs:inInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
</jaxrs:inInterceptors>
<jaxrs:outInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
</jaxrs:outInterceptors>
</jaxrs:server>

</beans>

3.接口

package cn.itcast.crm.service;


import java.util.List;


import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;


import cn.itcast.crm.domain.Customer;


/**
 * 客户操作
 * 
 * @author itcast
 *
 */
public interface CustomerService {


// 查询所有未关联客户列表
@Path("/noassociationcustomers")
@GET
@Produces({ "application/xml", "application/json" })
public List<Customer> findNoAssociationCustomers();


// 已经关联到指定定区的客户列表
@Path("/associationfixedareacustomers/{fixedareaid}")
@GET
@Produces({ "application/xml", "application/json" })
public List<Customer> findHasAssociationFixedAreaCustomers(
@PathParam("fixedareaid") String fixedAreaId);


// 将客户关联到定区上 , 将所有客户id 拼成字符串 1,2,3
@Path("/associationcustomerstofixedarea")
@PUT
public void associationCustomersToFixedArea(
@QueryParam("customerIdStr") String customerIdStr,
@QueryParam("fixedAreaId") String fixedAreaId);

@Path("/customer")
@POST
public void saveCustomer(Customer customer);

@Path("/customer/telephone/{telephone}")
@GET
@Consumes({ "application/xml", "application/json" })
public Customer findByTelephone(@PathParam("telephone") String telephone);


@Path("/customer/updatetype/{telephone}")
@GET
public void updateType(@PathParam("telephone") String telephone);


@Path("/customer/login")
@GET
@Consumes({ "application/xml", "application/json" })
public Customer login(@QueryParam("telephone") String telephone,@QueryParam("password") String password);


//customer/findFixedAreaIdByAddress?address="order.getSendAddress()
@Path("/customer/findFixedAreaIdByAddress")
@GET
@Consumes({ "application/xml", "application/json" })
public String getFixedArea_Id(@QueryParam("address") String address);

}

4.实体类

@Entity
@Table(name = "T_CUSTOMER")
@XmlRootElement(name = "customer")

public class Customer {...}


-------------------------------客户端API

WebClient
.create("http://localhost:9002/crm_management/services"
+ "/customerService/customer")
.type(MediaType.APPLICATION_JSON).post(model);























评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值