spring 4 @RestController 小試

在創建好基本的spring框架後,能夠嘗試實現一下簡單的rest風格請求html

一、須要引入的包,部分pom.xml文件java

<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>

 

二、dispatcher-servlet.xml配置web

<?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>

三、建立controllerajax

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;
    }
}

 

四、兩個簡單類spring

Message.javajson

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.javaspring-mvc

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;
    }

}

五、啓動tomcat後,能夠嘗試訪問如下地址tomcat

http://localhost:8080/study/ajax/htmlmvc

http://localhost:8080/study/ajax/jsonapp

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

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

相關文章
相關標籤/搜索