Spring Hot(2)Jersey Integration Service

本文详细介绍了如何使用Spring框架和Jersey实现RESTful服务,包括依赖配置、注解使用、方法映射及消息转换等功能,并通过示例展示了如何创建、获取和保存产品数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring Hot(2)Jersey Integration Service

Actually jersey is the implementation of standard API jsr311.

Here is the dependency needed to run all the REST related things. The function jsr311 provide us is the HTTP method mapping, JSONMessagerConverter, and all these things are based on Spring Framework.

<dependency org="commons-logging"name="commons-logging"rev="1.1.1"/>

<dependency org="commons-httpclient"name="commons-httpclient"rev="3.1"/><dependency org="commons-codec"name="commons-codec"rev="1.4"/>

<dependency org="commons-pool"name="commons-pool"rev="1.5"/>

<dependency org="commons-configuration"name="commons-configuration"rev="1.5"/><dependency org="commons-lang"name="commons-lang"rev="2.4"/>

<dependency org="commons-collections"name="commons-collections"rev="3.2"/><dependency org="commons-beanutils"name="commons-beanutils"rev="1.8.3"/>

<!-- spring jar -->

<dependency org="org/springframework"name="spring-context"rev="3.1.2.RELEASE"/><dependency org="org/springframework"name="spring-web"     rev="3.1.2.RELEASE"/><dependency org="org/springframework"name="spring-webmvc"rev="3.1.2.RELEASE"/><dependency org="org/springframework"name="spring-beans"rev="3.1.2.RELEASE"/><dependency org="org/springframework"name="spring-core"rev="3.1.2.RELEASE"/><dependency org="org/springframework"name="spring-asm"rev="3.1.2.RELEASE"/><dependency org="org/springframework"name="spring-expression"rev="3.1.2.RELEASE"/><dependency org="org/springframework"name="spring-aop"rev="3.1.2.RELEASE"/>

<!--  jersey -->

<dependency org="com/sun/jersey"      name="jersey-server"rev="1.18"/><dependency org="com/sun/jersey"   name="jersey-servlet"rev="1.18"/><dependency org="com/sun/jersey/contribs"name="jersey-multipart"rev="1.18"/><dependency org="com/sun/jersey"  name="jersey-json"     rev="1.18"/><dependency org="com/sun/jersey"name="jersey-client"rev="1.18"/><dependency org="com/sun/jersey/contribs"name="jersey-spring"rev="1.18"/><dependency org="com/sun/jersey"name="jersey-core"rev="1.18"/><dependency org="javax/ws/rs"name="jsr311-api"rev="1.1.1"/>

<!-- jackson -->

<dependency org="org/codehaus/jackson"name="jackson-core-lgpl"rev="1.9.10"/><dependency org="org/codehaus/jackson"name="jackson-mapper-lgpl"rev="1.9.10"/><dependency org="org/codehaus/jackson"name="jackson-jaxrs"rev="1.9.10"/><dependency org="org/codehaus/jackson"name="jackson-xc"rev="1.9.10"/>

<!-- other -->

<dependency org="log4j"name="log4j"rev="1.2.15"/>

<dependency org="asm"name="asm"rev="3.3"/>


And for the annotation of Object, there is one thing we need to pay attention to. 

//XmlTransient means that we will ignore in generated XML

 

@XmlTransient

@JsonIgnore

public Long getId() {

 returnid;

}

 

@XmlElement

@JsonProperty 

public void setId(Long id) {

 this.id = id;

}

We can configure ignore on the get Method, then these properties will not show in the JSON response. But we do need to parse these properties from request to our object, so we do need the @XmlElement, @JsonProperty on set Method.

Since we put a lot of annotation there in the Model, we need to scan them, here is the Spring Configuration does that.

 

<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.0.xsd             http://www.springframework.org/schema/tx            http://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd">

 

<context:component-scan base-package="com.sillycat.easyspringrest"/></beans>

Here the Service Class, based on the document, it is quite easy. Jersey implementation handle the mapping, message Converter(JacksonMapping configured in Spring). The only thing we need to do is implement our logic. My demo only show one mock service to demo the REST service.
package com.sillycat.easyspringrest.service;

import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import com.sillycat.easyspringrest.model.Product;

@Component
@Scope("prototype")
@Path("/product")
public class ProductJerseyService {

     private static final Logger logger = Logger.getLogger(ProductJerseyService.class);
    
     @GET
     @Path("products")
     @Produces(MediaType.APPLICATION_JSON)
     public List<Product> listProducts(){
          logger.debug("hitting the products REST API");
          List<Product> items = new ArrayList<Product>();
         
          Product item1 = new Product();
          item1.setDesn("desn1");
          item1.setId(Long.valueOf(1));
          item1.setProductLink("http://github.com/luohuazju");
          item1.setProductName("My Github Linke");
         
          Product item2 = new Product();
          item2.setDesn("desn2");
          item2.setId(Long.valueOf(2));
          item2.setProductLink("http://sillycat.iteye.com");
          item2.setProductName("My Technical Blog");
         
          items.add(item1);
          items.add(item2);
          return items;
     }
    
     @GET
     @Path("products/{id}")
     @Produces(MediaType.APPLICATION_JSON)
     public Product getProduct(@PathParam("id") Long id){
          logger.debug("hitting the get product REST API with id = " + id);
          Product item1 = new Product();
          item1.setDesn("desn1");
          item1.setId(Long.valueOf(1));
          item1.setProductLink("http://github.com/luohuazju");
          item1.setProductName("My Github Linke");
         
          return item1;
     }
    
     @POST
     @Path("products")
     @Produces(MediaType.APPLICATION_JSON)
     public Product saveProduct(Product item){
          logger.debug("hitting the save product REST API");
          logger.debug("id=" + item.getId());
          logger.debug("desn=" + item.getDesn());
          logger.debug("link=" + item.getProductLink());
          logger.debug("name=" + item.getProductName());
          return item;
     }
        
}

All these demo are in project easyspringrest.

References:
http://sillycat.iteye.com/admin/blogs/2067537


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值