首页 > 技术文章 > spring 4 @RestController 小试

mybest 2015-03-25 17:27 原文

在建立好基本的spring框架后,可以尝试实现一下简单的rest风格请求

1、需要引入的包,部分pom.xml文件

<dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.13</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.5.1</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.5.1</version>
        </dependency>

 

2、dispatcher-servlet.xml配置

<?xml version="1.0" encoding="GBK"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                        http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
                        http://www.springframework.org/schema/context 
                        http://www.springframework.org/schema/context/spring-context-4.1.xsd
                        http://www.springframework.org/schema/tx
                        http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
                        http://www.springframework.org/schema/jee 
                        http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
                        http://www.springframework.org/schema/mvc
                        http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
                        http://www.springframework.org/schema/util  
                        http://www.springframework.org/schema/util/spring-util-4.1.xsd">
    <mvc:annotation-driven />
    <context:annotation-config />
    <context:component-scan base-package="com.zf" />
</beans>

3、创建controller

package com.zf.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zf.po.Message;
import com.zf.po.ServiceType;

@RestController
@RequestMapping(value="/ajax")
public class AjaxController {

    @RequestMapping(value="/json")
    public ServiceType jsonAction(){
        ServiceType st=new ServiceType();
        st.setId(1);
        st.setName("ServiceType没有使用xml相关注解,无论是否使用.json后缀,均返回json格式");
        st.setPriority(3);
        return st;
    }
    @RequestMapping(value="/html")
    public String htmlAction(){
        return "直接返回字符串,则视图一般html";
    }
    
    @RequestMapping(value="/xml")
    public Message xmlAction(){
        Message st=new Message();
        st.setText("无需带后缀,返回XML结构");
        st.setName("XML");
        return st;
    }
    @RequestMapping(value="/xml.json")
    public Message xmlJsonAction(){
        Message st=new Message();
        st.setText("message使用了xml相关的注解,使用.json后缀访问,仍返回json数据");
        st.setName("json");
        return st;
    }
}

 

4、两个简单类

Message.java

package com.zf.po;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="root")
public class Message {
    String name;
    String text;
    @XmlElement
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @XmlElement
    public String getText() {
        return text;
    }
    public void setText(String text) {
        this.text = text;
    }
}

ServiceType.java

package com.zf.po;

import java.io.Serializable;

public class ServiceType implements Serializable, Cloneable {

    private static final long serialVersionUID = 1L;
    private int id;
    private String name;
    private int priority;

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getPriority() {
        return priority;
    }

    public void setPriority(int priority) {
        this.priority = priority;
    }

}

5、启动tomcat后,可以尝试访问以下地址

http://localhost:8080/study/ajax/html

http://localhost:8080/study/ajax/json

http://localhost:8080/study/ajax/xml.json

http://localhost:8080/study/ajax/xml

推荐阅读