`
sillycat
  • 浏览: 2542167 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Spring3 and REST Integration(I)

阅读更多
Spring3 and REST Integration(I)

1. REST support in Spring MVC
@Controller

@RequestMapping
@RequestMapping(method=RequestMethod.GET, value="/emps",
headers="Accept=application/xml, application/json")

@PathVariable
@RequestMapping(method=RequestMethod.GET, value="/emp/{id}")
public ModelAndView getEmployee(@PathVariable String id) { … }

@RequestParam
            inject a URL parameter into the method

@RequestHeader
            inject a certain HTTP header into the method

@RequestBody
            inject an HTTP request body into the method

HttpEntity<T>
  inject into the method automatically if you provide it as a parameter

ResponseEntity<T>
            return the HTTP response with your custom status or headers

2.HttpMessageConverter
HTTP request and responses are text based, a browser and server communicate by exchanging raw texts.
This is handled by HttpMessageConverter.
StringHttpMessageConverter

FormHttpMessageConverter

MarshallingHttpMessageConverter

MappingJacksonHttpMessageConverter
      read/write JSON data using Jackson's ObjectMapping. It converts data of media type application/json

AtomFeedHttpMessageConverter
      read/write ATOM feed using ROME's Feed API.  application/atom+xml

RssChannelHttpMessageConverter
      read/write RSS feed using ROME's feed API. application/rss+xml

3. I decide to use groovy script as my Controller layer
I will use my open-source project easygroovyplugin to make spring groovy controller with with spring together.

First of All, web.xml file, nothing special there:
<context-param>
<param-name>contextConfigLocation</param-name>
    <param-value>classpath:main-context.xml</param-value>
</context-param>
<servlet>
<servlet-name>restproxy</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
    <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener> 
<servlet-mapping>
<servlet-name>restproxy</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>

and my servlet configuration file /WebContent/WEB-INF/restproxy-servlet.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
</beans>

Nothing there, just a empty file.

Add my controller layer spring configuration file to main-context.xml:
<import resource="classpath:controller-easyrestproxy-context.xml" />

And the controller layer spring configuration file is controller-easyrestproxy-context.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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:groovy="http://www.sillycat.com/schema/groovy"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/lang
http://www.springframework.org/schema/lang/spring-lang-3.0.xsd
http://www.sillycat.com/schema/groovy
http://www.sillycat.com/schema/groovy/groovy.xsd
">
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean id="handlerAdapter" class="com.sillycat.easygroovyplugin.servlet.proxy.ProxyAwareAnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonConverter" />
</list>
</property>
</bean>
<lang:defaults refresh-check-delay="5" />
<!--  file://D:/work/easyrestproxy/groovy/*.groovy -->
<groovy:scan source-pattern="/groovy/*.groovy" />
<bean id="jsonConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes" value="application/json" />
</bean>
<!-- Client -->
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
<property name="messageConverters">
<list>
<ref bean="jsonConverter" />
</list>
</property>
</bean>
</beans>

And this is the properties file tell the project where to find the groovy file, easygroovy.properties:
###############################################
# groovy configuration
###############################################
groovy.file.path=file://e:/work/easyrestproxy

And my jars are all configured in ivy.xml:
<dependencies>
<!-- commons -->
<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-dbcp" name="commons-dbcp" rev="1.2.2" />
<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"/>
<!-- spring jar -->
<dependency org="org/springframework" name="spring-web" rev="3.0.5.RELEASE"/>
<dependency org="org/springframework" name="spring-webmvc" rev="3.0.5.RELEASE"/>
<!-- mysql -->
<dependency org="com/mysql/jdbc" name="mysql" rev="5.1.10" />
<!--  jackson -->
<dependency org="org/codehaus/jackson" name="jackson-core-asl" rev="1.6.3" />
<dependency org="org/codehaus/jackson" name="jackson-mapper-asl" rev="1.6.3" />
<!--  groovy -->
<dependency org="com/sillycat" name="easygroovyplugin" rev="1.3" />
</dependencies>

Some jars are copy to JBOSS_HOME/server/default/lib directory, so they are not listed here.

Now,  all the configration is ready. The groovy controller will work with spring well.

4. My groovy Controller will be as follow, it will have the CRUD sample :
package com.sillycat.easyrestproxy.controller;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestBody;

import org.springframework.beans.factory.annotation.Autowired;
import com.sillycat.easyjpa.manager.PersonManager;
import com.sillycat.easyjpa.model.Person;
import com.sillycat.easyrestproxy.model.PersonList;

@Controller
public class PersonController {
   
    @Autowired
    PersonManager personManager;
   
    @RequestMapping(method = RequestMethod.GET, value = "/person/{id}", headers = "Accept=application/json")
    public @ResponseBody Person get(@PathVariable("id") String id) {
        Person person = personManager.get(Integer.valueOf(id));
        return person;
    }
   
    @RequestMapping(method = RequestMethod.GET, value = "/persons", headers = "Accept=application/json")
    public @ResponseBody PersonList getAll() {
        List<Person> list = personManager.getAll();
        PersonList persons = null;
        if(list != null && !list.isEmpty()){
            persons = new PersonList(list);
        }
        return persons;
    }
   
    @RequestMapping(method=RequestMethod.POST, value="/person")
    public @ResponseBody Person add(@RequestBody Person p) {
        personManager.save(p);
        return p;
    }
       
    @RequestMapping(method=RequestMethod.PUT, value="/person/{id}")
    public @ResponseBody Person update(
        @RequestBody Person p, @PathVariable("id") String id) {
        personManager.save(p);
        return p;
    }
       
    @RequestMapping(method=RequestMethod.DELETE, value="/person/{id}")
    public @ResponseBody void remove(@PathVariable("id") String id) {
        personManager.delete(Integer.parseInt(id));
    }
}

PersonList is only a POJO to hold the List of Person  PersonList.java:
package com.sillycat.easyrestproxy.model;
import java.util.List;
import com.sillycat.easyjpa.model.Person;
public class PersonList
{
    private int          amount;
    private List<Person> persons;
    public PersonList()
    {
    }
    public PersonList(List<Person> persons)
    {
        this.persons = persons;
        this.amount = persons.size();
    }
    public int getAmount()
    {
        return amount;
    }
    public void setAmount(int amount)
    {
        this.amount = amount;
    }
    public List<Person> getPersons()
    {
        return persons;
    }
    public void setPersons(List<Person> persons)
    {
        this.persons = persons;
    }
}

references:
http://www.ibm.com/developerworks/web/library/wa-spring3webserv/index.html
http://www.ibm.com/developerworks/web/library/wa-restful/

分享到:
评论

相关推荐

    pro_spring_integration.pdf (英文原版非扫描)

    ### Spring Integration核心知识点详解 #### 一、Spring Integration概述 **Spring Integration** 是Spring家族中的一个强大组件,它提供了一套全面的企业级集成解决方案。本书《Pro Spring Integration》旨在为...

    Spring攻略(第三版)源代码

    3. Spring Annotation Driven Core Tasks 4. Spring @MVC 5. Spring REST 6. Spring Deployment to the Cloud 7. Spring Social 8. Spring Security 9. Spring Mobile 10. Spring with other Web Frameworks 11. ...

    Spring Recipes: A Problem-Solution Approach, Second Edition

    * Spring web: Spring MVC, Spring Web Flow 2, Spring Roo, other dynamic scripting, integration with popular Grails Framework (and Groovy), REST/web services, and more. This book guides you step by ...

    activiti-api-spring-integration-example.zip

    在实际应用中,我们可能还会涉及到与数据库的交互,例如存储流程实例相关的数据,以及与外部系统的集成,例如通过Spring Integration发送消息或者调用REST服务。这个"activityexample"项目是一个很好的起点,帮助...

    Spring Data:Spring Data Modern Data Access for Enterprise Java

    Part I. Background 1. The Spring Data Project ...13. Creating Big Data Pipelines with Spring Batch and Spring Integration Part VI. Data Grids 14. GemFire: A Distributed Data Grid Bibliography Index

    spring-int-rest:这是使用Spring Integration for REST API的Spring引导应用程序

    3. **Message Channels**:在Spring Integration中,消息通过通道进行传输。这些通道可以是同步的,也可以是异步的,允许我们在不同组件之间传递数据。 4. **Service Activator**:这是处理消息的主要组件,它可以...

    Spring.Essentials.178398

    Build lightning-fast web applications and REST APIs using Spring MVC and its asynchronous processing capabilities with the view technologies of your choice Explore simplified but powerful data access ...

    Pro Spring Integration

    - **SOAP与REST**:介绍Spring Integration如何支持SOAP和RESTful Web服务。 - **安全措施**:讨论在处理敏感数据时的安全措施。 #### 十五、扩展Spring Integration(Chapter 15: Extending Spring Integration) ...

    跟我学spring3

    2. **改进的MVC框架**:Spring3的MVC框架增加了RESTful支持,使得创建REST服务变得更加容易。 3. **SpEL(Spring Expression Language)**:Spring3引入了强大的表达式语言,用于运行时查询和操作对象图。 4. **...

    Spring3.X企业应用开发实战pdf扫描版+源码光盘

    3. **数据访问集成(Data Access Integration, DAI)**:Spring3.X对各种ORM框架如Hibernate、JPA等进行了集成,简化了数据访问层的编写,同时提供了JdbcTemplate和SqlSessionTemplate等工具类,方便操作数据库。...

    building restful web services with spring 5 2e

    APIs provide the fabric through which systems interact, and REST has become synonymous with APIs.The depth, breadth, and ease of use of Spring makes it one of the most attractive frameworks in the ...

    spring 最新框架jar

    and the rest of the Spring [family of projects][]. Browse the repositories under the [Spring organization][] on GitHub for a full list. ## Code of Conduct This project adheres to the Contributor ...

    mastering-spring-cloud2018

    will cover such topics like creating REST API using Spring MVC annotations, providing API documentation using Swagger2, and exposing health checks and metrics using Spring Boot Actuator endpoints. ...

    spring recipe 英文版

    Spring REST 随着 RESTful 架构的流行,Spring 也提供了丰富的支持来构建 RESTful 服务。这一章节将介绍如何利用 Spring 来实现 RESTful API。 #### 6. Spring Social 对于需要社交功能的应用程序,Spring ...

    Spring 实践(Spring in Practice).pdf版本

    web flow, SOAP web services (REST web services are part of the core), enterprise integration, batch processing, mobile, various flavors of social (Facebook, LinkedIn, Twitter, GitHub, and so on), ...

    spring-boot-integration-data-mysql

    demoSpringBootIntegration REST API 构建Restful CRUD API(仅实现) 要求 Java-1.8 Maven的-4 MySQL的-5 弹簧靴-2 ..../mvnw清洁包&& java -jar target / spring-integration-0.0.1-SNAPSHOT.jar

Global site tag (gtag.js) - Google Analytics