jersey和spring集成配置使用
2016-11-30 18:40
369 查看
jersey 是基于Java的一个轻量级RESTful风格的Web Services框架。
官网
当然必不可少的,也需要使用
Jar包详解:
jersey-client 是jersey提供的客户端包,封装了一些客户端操作的类
jersey-container-servlet 是jersey的核心,服务端必备包
jersey-media-moxy 是定义了jersry支持的常用的数据格式,json,xml都包括其中
jersey-media-multipart 是jersey的上传文件的支持
@ApplicationPath 注解该类,并且在参数中指定相对路径
继承
该类构造方法中设置jersey的配置,比如指定接口的包路径
如下:
@PathParam 接收链接中参数,如"/xxx/{name}/",@PathParm("name")
@QueryParam 接收链接中的普通参数,如"/xxx?name=ttt",@QueryParam("name")
@FormParm 接收post提交中的表单参数
@FormDataParm 上传文件接收文件参数
返回json需要使用@Produces注解,指定压缩方式:
原文链接:http://www.jianshu.com/p/15c32cb52da1
下面是使用案例:
其中有一个activemq的发送消息类:
activemq接受消息队列的类:
其他内容可以参考:http://blog.csdn.net/jbgtwang/article/details/43939037
官网
使用maven,在pom.xml中加入:
<!-- Jersey --> <dependency> <groupId>org.glassfish.jersey.core</groupId> <artifactId>jersey-client</artifactId> <version>${jersey.version}</version> </dependency> <dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-servlet</artifactId> <version>${jersey.version}</version> </dependency> <dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-moxy</artifactId> <version>${jersey.version}</version> </dependency> <dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-multipart</artifactId> <version>${jersey.version}</version> </dependency>
当然必不可少的,也需要使用
Java EE的支持:
<!-- JAVA EE --> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>7.0</version> <scope>provided</scope> </dependency>
Jar包详解:
jersey-client 是jersey提供的客户端包,封装了一些客户端操作的类
jersey-container-servlet 是jersey的核心,服务端必备包
jersey-media-moxy 是定义了jersry支持的常用的数据格式,json,xml都包括其中
jersey-media-multipart 是jersey的上传文件的支持
配置
jersey 的使用,必须要有一个全局的配置类,这个类需满足以下条件:@ApplicationPath 注解该类,并且在参数中指定相对路径
继承
org.glassfish.jersey.server.ResourceConfig类
该类构造方法中设置jersey的配置,比如指定接口的包路径
如下:
@ApplicationPath("/") public class RESTServiceConfig extends ResourceConfig { public RESTServiceConfig() { packages("web.rest"); register(MultiPartFeature.class); } }
GET
GET例子:
@GET @Path("/thing") public String get() { return "thing"; }
POST
POST例子:
@POST @Path("/add") public Boolean add(@FormParam("name") String name) { // TODO save return true; }
Param
jersey中有几种常用的接收参数的注解:@PathParam 接收链接中参数,如"/xxx/{name}/",@PathParm("name")
@QueryParam 接收链接中的普通参数,如"/xxx?name=ttt",@QueryParam("name")
@FormParm 接收post提交中的表单参数
@FormDataParm 上传文件接收文件参数
json
开发中,json已经常用到无处不在了,jersey对json的支持很好。接收json,需要使用@Consumes,注解指定解压方式:@Consumes(MediaType.APPLICATION_JSON)
返回json需要使用@Produces注解,指定压缩方式:
@Produces(MediaType.APPLICATION_JSON)
文件上传
示例:@POST @Path("import-excel") @Consumes(MediaType.MULTIPART_FORM_DATA) @Produces(MediaType.APPLICATION_JSON) public ImportResultBean importForExcel(@FormDataParam("file") String fileString, @FormDataParam("file") InputStream fis, @FormDataParam("file") FormDataContentDisposition fileDisposition) { // TODO return ; }
文件下载
文件下载需要将Response对象的压缩方式,指定为:
@Produces(MediaType.APPLICATION_OCTET_STREAM)
原文链接:http://www.jianshu.com/p/15c32cb52da1
下面是使用案例:
<!-- jersey-spring: 包含了jersey-servlet/jersey-server/jersey-core等,同时还包含了spring相关依赖。 --> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-core</artifactId> <version>${jersey.version}</version> </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-server</artifactId> <version>${jersey.version}</version> </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-json</artifactId> <version>${jersey.version}</version> </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-servlet</artifactId> <version>${jersey.version}</version> </dependency> <dependency> <groupId>com.sun.jersey.contribs</groupId> <artifactId>jersey-spring</artifactId> <version>${jersey.version}</version> <exclusions> <exclusion> <artifactId>spring-aop</artifactId> <groupId>org.springframework</groupId> </exclusion> <exclusion> <artifactId>spring-context</artifactId> <groupId>org.springframework</groupId> </exclusion> <exclusion> <artifactId>spring-beans</artifactId> <groupId>org.springframework</groupId> </exclusion> <exclusion> <artifactId>spring-web</artifactId> <groupId>org.springframework</groupId> </exclusion> <exclusion> <artifactId>spring-core</artifactId> <groupId>org.springframework</groupId> </exclusion> </exclusions> </dependency>web.xml文件配置:
<!-- restful webservices配置 --> <servlet> <servlet-name>jerseySpring</servlet-name> <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class> <init-param> <param-name>com.sun.jersey.config.property.packages</param-name> <param-value>com.innotek.webservice</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>jerseySpring</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping>实现类如下:
/** * Acestek.com.cn Inc. * Copyright (c) 2004-2016 All Rights Reserved. */ package com.innotek.webservice; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.RequestParam; import com.innotek.common.core.enums.ErrorCode; import com.innotek.core.support.mq.QueueSender; import com.innotek.model.parking.generator.ExtralBerthData; import com.innotek.model.parking.generator.RequestData; import com.innotek.model.parking.generator.ResponseData; import com.innotek.util.DataAnalysisUtil; /** * 基于http协议的webservice接口 * */ @Component @Path("/parkingData") public class ParkingDataService { private final Logger log = LogManager.getLogger(ParkingDataService.class); @Autowired private QueueSender queueSender; /** * 泊位状态接收接口 */ @Path("berthStatus") @POST @Produces(MediaType.TEXT_PLAIN) public String receiveBerthStatus(String message) { ResponseData response = null; try { RequestData requestData = DataAnalysisUtil.getRequest(message); //参数不正确 if (null == requestData) { response = new ResponseData(ErrorCode.PARAM_ERROR.code, ErrorCode.PARAM_ERROR.msg, "0"); return response.toString(); } //验证接口名称、厂家id、接入id if (!DataAnalysisUtil.verifyParam(requestData)) { response = new ResponseData(ErrorCode.PARAM_ERROR.code, ErrorCode.PARAM_ERROR.msg, "0"); return response.toString(); } //签名不正确 if (!DataAnalysisUtil.verifySign(requestData)) { response = new ResponseData(ErrorCode.SIGN_FAULT.code, ErrorCode.SIGN_FAULT.msg, "0"); return response.toString(); } //泊位信息数据 ExtralBerthData extralBerthData = DataAnalysisUtil.getExtralBerthData(requestData .getData()); //数据参数不正确 if (extralBerthData == null) { response = new ResponseData(ErrorCode.PARAM_ERROR.code, ErrorCode.PARAM_ERROR.msg, "0"); return response.toString(); } //将数据加入消息队列中 queueSender.send("Lily.parking.queue", extralBerthData); response = new ResponseData(ErrorCode.SUCCESS.code, ErrorCode.SUCCESS.msg, String.valueOf(extralBerthData.getSequence())); } catch (Exception e) { log.error("接口异常", e); response = new ResponseData(ErrorCode.UNKNOW_ERROR.code, ErrorCode.UNKNOW_ERROR.msg, "0"); } return response.toString(); } }
其中有一个activemq的发送消息类:
package com.innotek.core.support.mq; import java.io.Serializable; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.Session; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.jms.core.JmsTemplate; import org.springframework.jms.core.MessageCreator; import org.springframework.stereotype.Component; /** * 队列消息发送类 * @author ShenHuaJie * @version 2016年5月20日 下午3:19:19 */ @Component public class QueueSender { @Autowired @Qualifier("jmsQueueTemplate") private JmsTemplate jmsTemplate; /** * 发送一条消息到指定的队列(目标) * * @param queueName 队列名称 * @param message 消息内容 */ public void send(String queueName, final Serializable message) { jmsTemplate.send(queueName, new MessageCreator() { public Message createMessage(Session session) throws JMSException { return session.createObjectMessage(message); } }); } }
activemq接受消息队列的类:
package com.innotek.service.mq.queue; import java.sql.Timestamp; import java.util.Date; import javax.jms.Message; import javax.jms.MessageListener; import javax.jms.ObjectMessage; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.innotek.core.Constants; import com.innotek.core.util.DateUtil; import com.innotek.dao.parking.expand.BerthExpandMapper; import com.innotek.model.busi.generator.Berth; import com.innotek.model.parking.generator.ExtralBerthData; import com.innotek.provider.leave.AutoArriveService; import com.innotek.provider.leave.AutoLeaveServiceImpl; @Service public class QueueMessageListener implements MessageListener { private final Logger logger = LogManager.getLogger(); @Autowired private BerthExpandMapper berthExpandMapper; @Autowired private AutoLeaveServiceImpl autoLeaveServiceImpl; @Autowired private AutoArriveService autoArriveService; public void onMessage(Message message) { try { ExtralBerthData extralBerthData = (ExtralBerthData) ((ObjectMessage) message) .getObject(); Berth berth = berthExpandMapper.queryByBerthCood(extralBerthData.getBerthCode()); Date date = DateUtil.string2Date(extralBerthData.getSendTime(), "YYYY-MM-DD HH:mm:ss"); Timestamp sendTime = new Timestamp(date.getTime()); if (extralBerthData.getStatus() == Constants.PARK_YES) { //驶入接口 autoArriveService.autoParkRecord(extralBerthData.getCityCode(), berth, sendTime); } else if (extralBerthData.getStatus() == Constants.PARK_NO) { //驶离接口 autoLeaveServiceImpl.leave(extralBerthData.getCityCode(), berth, sendTime, null); } } catch (Exception e) { logger.error(e); } } }
其他内容可以参考:http://blog.csdn.net/jbgtwang/article/details/43939037
相关文章推荐
- spring 配置sqlserver数据源-使用集成身份登陆
- MQ原理、使用场景、IBM WebSphere MQ介绍及spring集成配置
- 使用Spring集成Hibernate配置,根据实体类自动创建表
- 非spring boot (即spring) 使用/集成 Spring cloud Config 分布式配置中心
- slf4j+log4j与spring集成及相关配置使用
- jersey spring shiro mybatis mysql集成配置
- 使用spring集成xfire开发webservice接口时的namespace和参数名称等配置
- spring-boot 集成mybatis配置使用
- Spring 集成 Quartz 的配置与使用
- 使用Maven配置Struts2 集成Spring
- 第三部分:shiro集成spring使用cas单点登录配置
- jersey的简单介绍及与spring-boot的集成使用
- spring与hibernate集成--关于配置文件的最优化使用
- springBoot系列教程05:fastjson的集成、配置及使用
- jersey的简单介绍及与spring-boot的集成使用
- Spring 与 ActiveMq的集成配置使用
- jersey和spring3集成使用
- Mybatis3.2.1使用例三:Mapper方式集成Spring、配置文件提供SQL
- shiro集成spring使用cas单点登录配置
- Spring.NET 1.3.2 集成 NHibernate 3.2 - 2 - 配置使用 Spring.NET 的网站