在建立好基本的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
packagecom.zf.controller; importorg.springframework.web.bind.annotation.RequestMapping; importorg.springframework.web.bind.annotation.RestController; importcom.zf.po.Message; importcom.zf.po.ServiceType; @RestController @RequestMapping(value="/ajax") public classAjaxController { @RequestMapping(value="/json") publicServiceType jsonAction(){ ServiceType st=newServiceType(); st.setId(1); st.setName("ServiceType没有使用xml相关注解,无论是否使用.json后缀,均返回json格式"); st.setPriority(3); returnst; } @RequestMapping(value="/html") publicString htmlAction(){ return "直接返回字符串,则视图一般html"; } @RequestMapping(value="/xml") publicMessage xmlAction(){ Message st=newMessage(); st.setText("无需带后缀,返回XML结构"); st.setName("XML"); returnst; } @RequestMapping(value="/xml.json") publicMessage xmlJsonAction(){ Message st=newMessage(); st.setText("message使用了xml相关的注解,使用.json后缀访问,仍返回json数据"); st.setName("json"); returnst; } }
4、两个简单类
Message.java
packagecom.zf.po; importjavax.xml.bind.annotation.XmlElement; importjavax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name="root") public classMessage { String name; String text; @XmlElement publicString getName() { returnname; } public voidsetName(String name) { this.name =name; } @XmlElement publicString getText() { returntext; } public voidsetText(String text) { this.text =text; } }
ServiceType.java
packagecom.zf.po; importjava.io.Serializable; public class ServiceType implementsSerializable, Cloneable { private static final long serialVersionUID = 1L; private intid; privateString name; private intpriority; public intgetId() { returnid; } public void setId(intid) { this.id =id; } publicString getName() { returnname; } public voidsetName(String name) { this.name =name; } public intgetPriority() { returnpriority; } public void setPriority(intpriority) { 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