简括:
bos_fore :客户端--向服务器发送请求,提供两个参数:当前页码(page),每页记录条数(rows);
bos_management:服务器端--根据客户端提供的两个参数,查询结果并且返回给客户端(总记录数和当前页数据),客户端进行分 页显示;
bos_domain:提供数据的封装实体类;
(一)带参客户端
package cn.itcast.bos.web.action;
import javax.jws.WebService;
import javax.ws.rs.core.MediaType;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import cn.itcast.bos.domain.page.PageBean;
import cn.itcast.bos.domain.take_delivery.Promotion;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
@SuppressWarnings("all")
public class PromotionAction extends ActionSupport implements
ModelDriven<Promotion> {
private Promotion promotion = new Promotion();
@Override
public Promotion getModel() {
return promotion;
}
// 分页展示
private int page;
private int rows;
public void setPage(int page) {
this.page = page;
}
public void setRows(int rows) {
this.rows = rows;
}
@Action(value = "promotion_pageQuery", results = { @Result(name = "success", type = "json") })
public String pageQuery() {
// 基于WebService,获取bos_management的活动列表信息
PageBean<Promotion> pageBean = WebClient
.create("http://localhost:8080/bos_management/services/promotionService/pageQuery?page="
+ page + "&rows=" + rows)
.accept(MediaType.APPLICATION_JSON).get(PageBean.class);
ActionContext.getContext().getValueStack().push(pageBean);
return SUCCESS;
}
}
1.(1)客户端基于WebService,访问服务器端,访问地址是():""http://localhost:8080/bos_management/services/promotionService/pageQuery?page="
+ page + "&rows=" + rows"
其中向服务器端发送的两个参数page和rows进行地址拼接一并发送给服务器端;
(2)红色字体:
①若是返回一个对象,则用"get(返回对象.class)",并且是".accept"--->如红色字体
.accept(MediaType.APPLICATION_JSON).get(PageBean.class);
②若是向服务器传过去一个对象,则用"post(返回对象)",并且是".type"--->如:
.type(MediaType.APPLICATION_JSON).post(customer);
2.访问服务器端,服务器端的applicationContext-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="promotionService" address="/promotionService">
<jaxrs:serviceBeans>
<bean class="cn.itcast.bos.service.base.impl.PromotionServiceImpl"/>
</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>
注意:(1).访问服务器端的字符串拼接地址需要拼接上applicationContext-webService.xml配置文件里的"id";
(2).别忘了在applicationContext.xml引用applicationContext-webService.xml配置文件;
3.服务器端service层处理业务
public interface PromotionService {
@Path("/pageQuery")
@GET
@Produces({ "application/xml", "application/json" })
PageBean<Promotion> findPageData(@QueryParam("page") int page,
@QueryParam("rows") int rows);
void save(Promotion promotion);
}
(1)@Path标签可以加入客户端发送过来的参数
例:
// 验证激活码是否重复
@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);
// 将客户关联到定区上 , 将所有客户id 拼成字符串 1,2,3
@Path("/associationcustomerstofixedarea")
@PUT
public void associationCustomersToFixedArea(
@QueryParam("customerIdStr") String customerIdStr,
@QueryParam("fixedAreaId") String fixedAreaId);
(2)HTTP协议请求方式
@GET:查询
@POST:增加
@PUT:修改
@DELETE:删除
4.服务器端ServiceImpl层
package cn.itcast.bos.service.base.impl;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.itcast.bos.dao.base.PromotionRepository;
import cn.itcast.bos.domain.page.PageBean;
import cn.itcast.bos.domain.take_delivery.Promotion;
import cn.itcast.bos.service.base.PromotionService;
@Service
@Transactional
public class PromotionServiceImpl implements PromotionService{
@Override
public PageBean<Promotion> findPageData(int page, int rows) {
Pageable pageable = new PageRequest(page-1, rows);
Page<Promotion> pageData = promotionRepository.findAll(pageable);
//封装到PageBean对象当中
PageBean<Promotion> pageBean = new PageBean<Promotion>();
pageBean.setTotalCount(pageData.getTotalElements());
pageBean.setPageData(pageData.getContent());
return pageBean;
}
注意:别忘了加事务注解
5.服务器端PromotionRepository(dao)层
package cn.itcast.bos.dao.base;
import java.util.Date;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import cn.itcast.bos.domain.take_delivery.Promotion;
public interface PromotionRepository extends JpaRepository<Promotion, Integer>{
}
**
JPA的查询方法(Spring Data)
在JPA中有三种方式可以进行数据的查询(1,方法命名查询 2,@NamedQuery查询 3,@Query查询),
假设有一张表叫PERSON,字段:ID(INT),NAME(VARCHAR),AGE(INT),ADDRESS(VARCHAR).
实体类:id(integer),name(String),age(integer),address(String)---->定义JpaRepository<Promotion,Integer>里的Integer
(二)不带参客户端
在我的"使用actionMQ发送短信"博客文章里, --> 调用webService连接CRM保存客户信息 ;