REST是 Roy Fielding 博士在 2000 年提出的。
REST(Representational State Transfer-表现层状态转化)是一种新的软件架构风格,它以资源(resource)为核心,使用 HTTP、 URI、XML 以及 HTML 等流行协议和标准来完成对资源的操作及显示。 这些操作包括获取、创建、修改和删除资源(CRUD),分别对应于 HTTP 协议的 GET、POST、PUT 和 DELETE 方法。
RESTful架构可以总结为以下三个内容:
(1)每一个URI代表一种资源;
(2)客户端和服务器之间,传递这种资源的某种表现层;
(3)客户端通过四个HTTP动词,对服务器端资源进行操作,实现”表现层状态转化”。
REST (风格的)服务(RESTful Service)是一种基于 HTTP 和 REST 准则的轻量级 Web 服务。
这类服务可以看作一系列资源(resource)的集合,服务的定义可以视为以下三个切面的组合 :
- 访问 Web Service 的 URI,如:http://example.com/resources。
- Web Service 所支持的数据 MIME 类型,如:JSON, XML, YAML 等。
- Web Service 使用 HTTP 协议支持的操作,如 GET, POST, PUT, DELETE。
REST风格的服务的性能,效率和易用性等方面均优于 SOAP 协议:
- 相比SOAP 和 XML-RPC, REST 服务更加简洁,
- 它可以完全通过 HTTP 协议实现,
- 支持多种消息格式,比如XML 、JSON
- 还可以利用缓存 Cache 来提高响应速度(第一次访问资源 缓存,第二次访问资源,返回304客户端调用本地)。
JAX-RS (JSR 311)是Java世界中的另一套Web Service规范,用于开发RESTful Web Service。它属于Java EE 6规范中的子规范,逐步取代了JAX-WS(大WebService的规范)的地位。
基于JAX-RS规范实现的RESTful API可达到:支持资源抽象、统一接口的 “CRUD式Web服务”。
CXF框架支持JAX-WS,也支持JAX-RS规范,都是远程调用。
pom.xml
<properties>
<cxf.version>3.1.9cxf.version>
<slf4j.version>1.7.21slf4j.version>
properties>
<dependencies>
<dependency>
<groupId>org.apache.cxfgroupId>
<artifactId>cxf-rt-frontend-jaxrsartifactId>
<version>${cxf.version}version>
dependency>
<dependency>
<groupId>org.apache.cxfgroupId>
<artifactId>cxf-rt-transports-http-jettyartifactId>
<version>${cxf.version}version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
<version>${slf4j.version}version>
dependency>
<dependency>
<groupId>org.apache.cxfgroupId>
<artifactId>cxf-rt-rs-clientartifactId>
<version>${cxf.version}version>
dependency>
<dependency>
<groupId>org.apache.cxfgroupId>
<artifactId>cxf-rt-rs-extension-providersartifactId>
<version>${cxf.version}version>
dependency>
<dependency>
<groupId>org.codehaus.jettisongroupId>
<artifactId>jettisonartifactId>
<version>1.3.8version>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
<version>3.1version>
<configuration>
<source>1.7source>
<target>1.7target>
configuration>
plugin>
plugins>
build>
@XmlRootElement 指定序列化(转换XML、JSON) 对象名字。
//指定序列化(转换XML、JSON) 对象名字
//实体类:资源对应的类
//@XmlRootElement//默认情况下,该对象在传输表现的时候,表现方式xml,要转成xml,根元素: name:默认类名小写
@XmlRootElement(name="users")//根元素:
//比如查询列表:123 ....
//如果是json:{users:[{"id":"2342",...},{...}}
public class User {
private Integer id;
private String username;
private String password;
private Integer age;
第一种 @Path 服务访问资源路径
如果访问saveUser方法 /userService/user
第二种 @Produces 生成(方法返回值) @Consumes 消费 (方法参数)
@Consumes 指定能够处理客户端传递过来数据格式
@Produces 指定能否生成哪种格式数据返回给客户端
第三种 @GET 查询 @PUT 修改 @POST 增加 @DELETE 删除
第四种:
@PathParam来自于URL的路径,@QueryParam来自于URL的查询参数
//SEI的接口:对外暴露
//加上注解path,类似于具体的服务的名字"/mobile"
@Path("/userService")//暴露出去访问资源的服务路径:http://127.0.0.1:8888//userService/users/
public interface UserService {
//定义CRUD方法
/**
* 说明:保存用户
* @param user
*/
@Path("/users")//访问的资源
@POST//新建资源动作
//配置表现:setContenttype(....)
//消费:接收客户端传过来的消息的格式
@Consumes({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
//没有产品
public void saveUser(User user);
/**
*
* 说明:修改用户
* @param user
*/
@Path("/users")
@PUT//更新操作
//可接收客户端传递过来的格式
@Consumes({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
public void updateUser(User user);
/**
*
* 说明:直接根据id删除
* @param id
*/
@Path("/users/{id}")
@DELETE//删除,uri:/users/1
@Consumes({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})//消费
public void deleteUser(@PathParam("id")Integer id);
/**
*
* 说明:查询所有数据列表
* @return
*/
@Path("/users")
@GET
//产品(生产):返回给客户端的格式
@Produces({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
public List findUserList();
/**
*
* 说明:根据id来查询用户
* @param id
* @return
*/
@Path("/users/{id}")
@GET
//消费者
@Consumes({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
//生产者:
@Produces({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
//127.0.0.1:8888/cxf_jaxrs/userService/users/1
//资源后面都是参数,参数,类似于命名占位符
public User findUserById(@PathParam("id")Integer id);
/**
*
* 说明:使用参数传参:/users?id=1
* @param id
* @return
*/
@Path("/usersparam")
@GET
//消费者
@Consumes({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
//生产者:
@Produces({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
public User findUserById2(@QueryParam("id")Integer id);
}
/**
* SEI实现
*/
public class UserServiceImpl implements UserService {
@Override
public void save(User user) {
//调用dao
System.out.println("----保存:"+user);
}
@Override
public void update(User user) {
//调用dao
System.out.println("-----更新对象:"+user);
}
@Override
public void delete(Integer id) {
//调用dao
System.out.println("-----删除的id:"+id);
}
@Override
public List findUserList() {
List userList = new ArrayList<>();
userList.add(new User(1,"jack","123",19));
userList.add(new User(2,"rose","1234",18));
return userList;
}
@Override
public User findUserById(Integer id) {
System.out.println("----根据id查询,id是:"+id);
return new User(1,"jack","123",19);
}
@Override
public User findUserById2(Integer id) {
System.out.println("----根据id参数查询,id是:"+id);
return new User(2,"rose","1234",18);
}
}
public class CxfJaxRsServer {
public static void main(String[] args) {
// 发布rest服务
//1.构建服务工厂对象
JAXRSServerFactoryBean jaxrsServiceFactoryBean = new JAXRSServerFactoryBean();
//2.在工厂上设置几个属性
//2.1服务地址
jaxrsServiceFactoryBean.setAddress("http://localhost:8888/cxf_jaxrs");
//2.2资源的类型
jaxrsServiceFactoryBean.setResourceClasses(User.class);
//2.3服务对象.自动反射接口
jaxrsServiceFactoryBean.setServiceBean(new UserServiceImpl());
//c创建并发布服务
jaxrsServiceFactoryBean.create();
System.out.println("rest服务发布了!");
//资源访问的方式:web地址+服务地址+资源名字
}
}
浏览器测试(只能是get方法测试)
查询所有:http://localhost:8888/cxf_jaxrs/userService/users
根据id查询(路径方式):http://localhost:8888/cxf_jaxrs/userService/users/1
根据id查询(参数方式):http://localhost:8888/cxf_jaxrs/userService/users?id=1
客户端编程有两种做法:
1)HttpClient工具需要自己对HTTP协议内容进行定制和解析。
2)WebClient工具类(CXF自带)(使用)。
主要的方法说明:
- create:调用服务资源路径,并建立连接
- type:客户端发送给服务器的数据(资源)格式,对应服务端的@consumes的数据类型
- accept:客户端接收服务器的数据(资源)格式,对应服务端的@Produces的数据类型
- get,post,put,delete四个方法,分别是要采用HTTP协议的那种方式访问服务器。
public class CxfRsClient {
public static void main(String[] args) {
//----方法说明
//create:调用服务资源路径,并建立连接
//type:客户端发送给服务器的数据(资源)格式,对应服务端的@consumes的数据类型
//accept:客户端接收服务器的数据(资源)格式,对应服务端的@Produces的数据类型
//get,post,put,delete四个方法,分别是要采用HTTP协议的那种方式访问服务器。
// 目标:调用服务端:crud
//WebClient:客户端工具类
//保存
//new对象
// User user = new User();
// user.setId(101);
// user.setUsername("xiaohong");
// user.setPassword("666");
// user.setAge(28);
// //访问的资源http://127.0.0.1:8888/cxf_jaxrs/userService/users
// //参数:访问的服务器上的资源
// WebClient.create("http://127.0.0.1:8888/cxf_jaxrs/userService/users")
// //消费者(对于服务端)
// .type(MediaType.APPLICATION_JSON)//设置表现形式,内容类型,怎么传数据,服务器可接收什么
// .post(user)//自动将对象转换成xml或json
// ;
//更新
// User user2 = new User();
// user2.setId(102);
// user2.setUsername("xiaohong2");
// user2.setPassword("6662");
// user2.setAge(28);
//资源uri
// WebClient.create("http://127.0.0.1:8888/cxf_jaxrs/userService/users")
// .type(MediaType.APPLICATION_JSON)
// .put(user2);
//删除
//资源uri
// WebClient.create("http://127.0.0.1:8888/cxf_jaxrs/userService/users/1")
//// .path("/"+id);//类似于Stringbuffer的.append("xxxx")
// .type(MediaType.APPLICATION_JSON)
// .delete();
//查询所有列表
//资源uri
// Collection extends User> userList = WebClient.create("http://127.0.0.1:8888/cxf_jaxrs/userService/users")
// //生产者(针对服务器来说,客户端能接收)
// .accept(MediaType.APPLICATION_JSON)
//// .get().getEntity()//获取内容
// .getCollection(User.class);//列表对象
// System.out.println(userList);
//根据id查询一个对象
//资源uri
// User user = WebClient.create("http://127.0.0.1:8888/cxf_jaxrs/userService/users/1")
// //消费者
// .type(MediaType.APPLICATION_JSON)
// //生产者
// .accept(MediaType.APPLICATION_JSON)
// .get(User.class);//获取一个对象
// System.out.println(user);
//有时候查询先判断有没有查询成功,还不想要结果。
int status = WebClient.create("http://127.0.0.1:8888/cxf_jaxrs/userService/users")
.accept(MediaType.APPLICATION_JSON)
.get()
.getStatus();
System.out.println(status);
System.out.println("客户端操作完成!");
}
技术架构:
Spring + Hibernate(spring和hibernate直接整合)+CXF+Oracle
引入Maven坐标:
Spring、Hibernate、数据库和连接池、日志、Servlet、JSP、junit、编译版本覆盖、tomcat端口覆盖8888:
<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.0modelVersion>
<groupId>cn.aric.projectgroupId>
<artifactId>crmartifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>warpackaging>
<name>crmname>
<description>物流的客户子系统description>
<properties>
<spring.version>3.2.12.RELEASEspring.version>
<hibernate.version>3.6.10.Finalhibernate.version>
<slf4j.version>1.7.5slf4j.version>
<c3p0.version>0.9.1.2c3p0.version>
<oracle.version>10.2.0.4.0oracle.version>
<servlet.version>2.5servlet.version>
<jsp.version>2.0jsp.version>
<junit.version>4.11junit.version>
<cxf.version>3.1.9cxf.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aspectsartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-ormartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.hibernategroupId>
<artifactId>hibernate-coreartifactId>
<version>${hibernate.version}version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
<version>${slf4j.version}version>
dependency>
<dependency>
<groupId>c3p0groupId>
<artifactId>c3p0artifactId>
<version>${c3p0.version}version>
dependency>
<dependency>
<groupId>com.oraclegroupId>
<artifactId>ojdbc14artifactId>
<version>${oracle.version}version>
<scope>runtimescope>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>servlet-apiartifactId>
<version>${servlet.version}version>
<scope>providedscope>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>jsp-apiartifactId>
<version>${jsp.version}version>
<scope>providedscope>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>${junit.version}version>
<scope>testscope>
dependency>
<dependency>
<groupId>javassistgroupId>
<artifactId>javassistartifactId>
<version>3.12.0.GAversion>
dependency>
<dependency>
<groupId>org.apache.cxfgroupId>
<artifactId>cxf-rt-frontend-jaxrsartifactId>
<version>${cxf.version}version>
dependency>
<dependency>
<groupId>org.apache.cxfgroupId>
<artifactId>cxf-rt-rs-extension-providersartifactId>
<version>${cxf.version}version>
dependency>
<dependency>
<groupId>org.codehaus.jettisongroupId>
<artifactId>jettisonartifactId>
<version>1.3.8version>
dependency>
<dependency>
<groupId>org.apache.cxfgroupId>
<artifactId>cxf-rt-rs-clientartifactId>
<version>${cxf.version}version>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojogroupId>
<artifactId>tomcat-maven-pluginartifactId>
<version>1.1version>
<configuration>
<port>8888port>
configuration>
plugin>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-resources-pluginartifactId>
<version>2.6version>
<configuration>
<encoding>UTF-8encoding>
configuration>
plugin>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
<version>3.1version>
<configuration>
<source>1.7source>
<target>1.7target>
configuration>
plugin>
plugins>
build>
project>
project>
配置Spring整合Hibernate(几个配置文件:applicationContext.xml、db.properties,web.xml,log4j.properties等)
web.xml
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name>crmdisplay-name>
<servlet>
<description>Apache CXF Endpointdescription>
<display-name>cxfdisplay-name>
<servlet-name>cxfservlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServletservlet-class>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>cxfservlet-name>
<url-pattern>/services/*url-pattern>
servlet-mapping>
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:applicationContext.xmlparam-value>
context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
<welcome-file-list>
<welcome-file>index.jspwelcome-file>
welcome-file-list>
web-app>
db.properties
jdbc.driver=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:xe
jdbc.user=scott
jdbc.password=tigger
log4j.properties
### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.err
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### direct messages to file mylog.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=d:/mylog.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### set log levels - for more verbose logging change 'info' to 'debug' ###
log4j.rootLogger=info, stdout
applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialectprop>
<prop key="hibernate.hbm2ddl.auto">updateprop>
<prop key="hibernate.show_sql">trueprop>
<prop key="hibernate.format_sql">true prop>
props>
property>
<property name="packagesToScan">
<list>
<value>cn.aric.crm.domainvalue>
list>
property>
bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<context:component-scan base-package="cn.aric.crm.service,cn.aric.crm.dao"/>
<import resource="applicationContext-cxf.xml"/>
beans>
@Entity
@Table(name="t_customer",schema="scott")
public class Customer {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)//auto代表自动
private Integer id;//OID属性
private String name;//客户名称
private String address;//住所
private String telephone;//联系电话
private String decidedZoneId;//定区编号(客户和定区关联的字段)
public Integer getId() {
return id;
}
public String getName() {
return name;
}
public String getAddress() {
return address;
}
public String getTelephone() {
return telephone;
}
public String getDecidedZoneId() {
return decidedZoneId;
}
public void setId(Integer id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setAddress(String address) {
this.address = address;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public void setDecidedZoneId(String decidedZoneId) {
this.decidedZoneId = decidedZoneId;
}
}
测试上面的配置,启动服务,自动建表:tomcat:run
<properties>
<cxf.version>3.1.9cxf.version>
properties>
<dependencies>
<dependency>
<groupId>org.apache.cxfgroupId>
<artifactId>cxf-rt-frontend-jaxrsartifactId>
<version>${cxf.version}version>
dependency>
<dependency>
<groupId>org.apache.cxfgroupId>
<artifactId>cxf-rt-rs-extension-providersartifactId>
<version>${cxf.version}version>
dependency>
<dependency>
<groupId>org.codehaus.jettisongroupId>
<artifactId>jettisonartifactId>
<version>1.3.8version>
dependency>
<dependency>
<groupId>org.apache.cxfgroupId>
<artifactId>cxf-rt-rs-clientartifactId>
<version>${cxf.version}version>
dependency>
dependencies>
@Entity
@Table(name="t_customer",schema="SCOTT")
@XmlRootElement(name="customer")
public class Customer {
@Id
//如果没有给主键策略,不管oid什么类型,都需要手动主键值
//但如果有主键策略,根据主键策略来走。
@GeneratedValue(strategy=GenerationType.AUTO)//auto代表自动
//--自动:根据OID的属性的类型来自动选择
//如果String类型,主键数据库不会自动生成,要么手动赋值(对于数据库来说手动主键),要么让hibernate的uuid赋值
//如果是Integer,Long,值自增长的,会自动创建一个序列,使用序列的值作为主键值(数据库提供的序列值--hibernate调用)。
private Integer id;//OID属性
private String name;//客户名称
private String address;//住所
private String telephone;//联系电话
private String decidedZoneId;//定区编号(客户和定区关联的字段)
@Path("/customerService")//具体服务的名字路径
@Produces({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
@Consumes({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
public interface CustomerService {
//提供三个暴露的方法
/**
*
* 说明:查询没有关联定区的客户的列表
* @return
*/
@Path("/customers")//某个方法的操作的资源
@GET
@Produces({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
public List findCustomerListNoDecidedZoneId();
/**
*
* 说明:查询关联某个定区的客户的列表
* @return
*/
@Path("/customers/{decidedZoneId}")//某个方法的操作的资源
@GET
@Produces({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
@Consumes({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
public List findCustomerListByDecidedZoneId(@PathParam("decidedZoneId")String decidedZoneId);
/**
*
* 说明:批量更新定区编号,通过客户编号
* @param decidedZoneId:DQ001
* @param customerIds:用逗号分割编号:2,3
*/
@Path("/customers/{decidedZoneId}/{customerIds}")//某个方法的操作的资源
@PUT
@Produces({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
@Consumes({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
public void updateDecidedZoneIdByIds(@PathParam("decidedZoneId")String decidedZoneId,@PathParam("customerIds")String customerIds);
}
//SEI:客户的业务实现类
//客户操作的业务层实现
Service("customeService")
@Transactional
public class CustomerServiceImpl implements CustomerService{
//注入dao
@Autowired
private GenericDAO customerDAO;
@Override
public List findCustomerListNoDecidedZoneId() {
//条件构建
DetachedCriteria criteria =DetachedCriteria.forClass(Customer.class)
.add(Restrictions.isNull("decidedZoneId"));
//查询
return customerDAO.findByCriteria(criteria);
}
@Override
public List findCustomerListByDecidedZoneId(String decidedZoneId) {
//条件构建
DetachedCriteria criteria =DetachedCriteria.forClass(Customer.class)
.add(Restrictions.eq("decidedZoneId", decidedZoneId));
//查询
return customerDAO.findByCriteria(criteria);
}
@Override
public void updateDecidedZoneIdByIds(String decidedZoneId, String customerIds) {
//快照更新
//====先去掉指定定区的所有的关联(update)
//查询出所有原来已经关的客户列表
DetachedCriteria criteria =DetachedCriteria.forClass(Customer.class)
.add(Restrictions.eq("decidedZoneId", decidedZoneId));
List customerList = customerDAO.findByCriteria(criteria);
//快照
for (Customer customer : customerList) {
customer.setDecidedZoneId(null);
}
//====关联需要关联指定定区的客户(update)
if(!StringUtils.isEmpty(customerIds)){
String[] customerArray = customerIds.split(",");
for (String customerId : customerArray) {
Customer customer = customerDAO.findById(Customer.class, Integer.parseInt(customerId));
customer.setDecidedZoneId(decidedZoneId);
}
}
//等待flush
}
}
略
<servlet>
<description>Apache CXF Endpointdescription>
<display-name>cxfdisplay-name>
<servlet-name>cxfservlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServletservlet-class>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>cxfservlet-name>
<url-pattern>/services/*url-pattern>
servlet-mapping>
cxf配置:applicationContext-cxf.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xmlns:jaxrsclient="http://cxf.apache.org/jaxrs-client"
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
http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd
http://cxf.apache.org/jaxrs-client
http://cxf.apache.org/schemas/jaxrs-client.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<jaxrs:server id="customerWebService" address="/CustomerWS" >
<jaxrs:serviceBeans>
<ref bean="customerService" />
jaxrs:serviceBeans>
<jaxrs:inInterceptors>
<ref bean="loggingInInterceptor"/>
jaxrs:inInterceptors>
<jaxrs:outInterceptors>
<ref bean="loggingOutInterceptor" />
jaxrs:outInterceptors>
jaxrs:server>
<bean id="loggingInInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor">bean>
<bean id="loggingOutInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor">bean>
beans>
测试的访问路径:
web上下文+cxf前端控制器+配置的服务路径+类上的具体的服务路径
web上下文+cxf前端控制器+配置的服务路径+类上的具体的服务路径+资源路径(方法上)
使用SoapUI—webservice的调试工具,Encoding改UTF-8。
引入Maven坐标(Pom.xml):
<cxf.version>3.1.9cxf.version>
<dependency>
<groupId>org.apache.cxfgroupId>
<artifactId>cxf-rt-rs-extension-providersartifactId>
<version>${cxf.version}version>
dependency>
<dependency>
<groupId>org.codehaus.jettisongroupId>
<artifactId>jettisonartifactId>
<version>1.3.8version>
dependency>
<dependency>
<groupId>org.apache.cxfgroupId>
<artifactId>cxf-rt-rs-clientartifactId>
<version>${cxf.version}version>
dependency>
//dto
@XmlRootElement(name="customer")
public class Customer {
private Integer id;//OID属性
private String name;//客户名称
private String residence;//住所
private String telephone;//联系电话
private String decidedZoneId;//定区编号(客户和定区关联的字段)
略
//列出没有关联定区的客户
@Action("decidedZone_listCustomerListNoDecidedZoneId")
public String listCustomerListNoDecidedZoneId(){
//直接调用webservice接口
//基本服务连接
WebClient webClient = WebClient.create("http://localhost:8888/crm/services");
Collection extends Customer> collection = webClient
.path("/crmService/customerService")//具体服务
.path("/customers")//资源路径
.accept(MediaType.APPLICATION_JSON)//客户端要接收的类型
.type(MediaType.APPLICATION_JSON)//发出去的数据类型,java对象会转换为该类型
.getCollection(Customer.class);
//压入栈顶
pushToValuestackRoot(collection);
//json数组
return JSON; //JSON在父类中配置常量
}
//列出已经关联定区的客户
@Action("decidedZone_listCustomerListHasDecidedZoneId")
public String listCustomerListHasDecidedZoneId(){
//直接调用webservice接口
//基本服务连接
WebClient webClient = WebClient.create("http://localhost:8888/crm/services");
Collection extends Customer> collection = webClient
.path("/crmService/customerService")//具体服务
.path("/customers")//资源路径
.path("/"+model.getId())
.accept(MediaType.APPLICATION_JSON)//客户端要接收的类型
.type(MediaType.APPLICATION_JSON)//发出去的数据类型,java对象会转换为该类型
.getCollection(Customer.class);
//压入栈顶
pushToValuestackRoot(collection);
//json数组
return JSON;