[springframework] - Spring MVC Implements RESTful

This instructions is on targets for following requirements:

  • maven management
  • spring mvc framework
  • annotation configuration
  • JSON auto converting

1. POM and core dependencies

    <dependencyManagement><!-- <strong>to unify spring version</strong> -->
        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-framework-bom</artifactId>
                <version>4.1.7.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <!-- javaee -->
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>6.0</version>
            <scope>provided</scope>
        </dependency>
        <!-- jstl -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
            <scope>provided</scope>
        </dependency>
       
        <!-- springframework core -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
        </dependency>
       
        <!-- springframework web -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
        </dependency>
       
        <!-- <strong>JSON auto converting needs</strong> -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.6.4</version>
        </dependency>
       
        <!-- <strong>mock Log</strong> -->
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>${slf4j.version}</version>
        </dependency>    
    </dependencies>

2. web.xml

<pre name="code" class="html">    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><strong><!-- spring mvc request dispatcher --></strong>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/webContext.xml</param-value><!-- <strong>web level bean container</strong> -->
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>


 
 

3. webContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans     
                           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                           http://www.springframework.org/schema/mvc
                           http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
                           http://www.springframework.org/schema/context 
                           http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="com.gemalto.offerflex" />
    <mvc:annotation-driven />
    <context:annotation-config/>
    
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
    
    <!-- Configure to plugin JSON as request and response in method handler -->
    <bean  class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="jsonMessageConverter"/>
            </list>
        </property>
    </bean>
    
    <bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
</beans>

4. VO

JSON format message from request is converted into VO. And The VO will be converted into JSON format message into response if the VO is as a return of controller.

public class House {
 
    private String address;
    private int roomNum;

    public String getAddress() {
        return address;
    }

    public int getRoomNum() {
        return roomNum;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public void setRoomNum(int roomNum) {
        this.roomNum = roomNum;
    }
}


public class Person {
    
    private String id;
    private String name;
    private List<House> houses;
    
    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }
    
    public List<House> getHouses(){
        return houses;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }
    
    public void setHouses(List<House> houses){
        this.houses = houses;
    }
     
}

5. Controller

@RestController
@RequestMapping ("/rest")
public class TestRestController {
    
    private static final Logger LOGGER = LoggerFactory.getLogger(TestRestController.class);
    
    /**
     * http://localhost:7001/WebTest/rest/params.action?id=10&name=wang bo
     */
    @RequestMapping(value="/params", method=RequestMethod.GET)
    @ResponseStatus(value=HttpStatus.OK)
    public String params(@RequestParam String id, @RequestParam String name){
        
        LOGGER.info("params...");
        LOGGER.info("id  : " + id);
        LOGGER.info("name: " + name);
        
        return "Received!";
    }
    
    /**
     * http://localhost:7001/WebTest/rest/pathVariable/10/Tom.action
     */
    @RequestMapping(value="/pathVariable/{id}/{name}", method=RequestMethod.GET)
    @ResponseStatus(value=HttpStatus.OK)
    public String pathVariable(@PathVariable String id, @PathVariable String name){
        
        LOGGER.info("pathVariable...");
        LOGGER.info("id  : " + id);
        LOGGER.info("name: " + name);
        
        return "Received!";
    }
    
    /**
     * Json object, Person, does not implements Serializable interface and does not need any constructor.
     * 
     */
    @RequestMapping(value="/jsonObj", method=RequestMethod.POST)
    @ResponseStatus(value=HttpStatus.OK)
    public Person jsonObj(@RequestBody Person person){
        return dealWith(person);
    }
    
    private Person dealWith(Person person){
        LOGGER.info("jsonObj...");
        LOGGER.info("id  : " + person.getId());
        LOGGER.info("name: " + person.getName());
        
        if(person.getHouses() != null){
            for(House house : person.getHouses()){
                LOGGER.info("houses --- " + house.getAddress() + ", " + house.getRoomNum());
            }
        }else{
            LOGGER.info("houses --- null");
        }
        
        return person;
    }
    
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值