首页 > 代码库 > maven工程中,spring mvc @ResponseBody的使用

maven工程中,spring mvc @ResponseBody的使用

1.配置web.xml文件(确保已经导入了spring中的jar包)

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns="http://java.sun.com/xml/ns/javaee"    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"    id="WebApp_ID" version="2.5">    <display-name>springmvc</display-name>    <servlet>        <servlet-name>spring</servlet-name>        <servlet-class>            org.springframework.web.servlet.DispatcherServlet</servlet-class>        <load-on-startup>1</load-on-startup>    </servlet>    <servlet-mapping>        <servlet-name>spring</servlet-name>        <url-pattern>/</url-pattern>    </servlet-mapping>    <filter>        <filter-name>encodingFilter</filter-name>        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>        <init-param>            <param-name>encoding</param-name>            <param-value>UTF-8</param-value>        </init-param>        <init-param>            <param-name>forceEncoding</param-name>            <param-value>true</param-value>        </init-param>    </filter>    <filter-mapping>        <filter-name>encodingFilter</filter-name>        <url-pattern>/*</url-pattern>    </filter-mapping></web-app>

2.注意到上面的橙红色代码:

<servlet-name>spring</servlet-name>
在默认情况下我们还需要一个 "xxx-servlet.xml" 文件,"xxx"就是<servlet-name>标签中的spring,所以我们需要的文件为(在WEB-INF文件夹下):spring-servlet.xml

3.spring-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:tx="http://www.springframework.org/schema/tx"    xmlns:context="http://www.springframework.org/schema/context"      xmlns:mvc="http://www.springframework.org/schema/mvc"      xsi:schemaLocation="http://www.springframework.org/schema/beans     http://www.springframework.org/schema/beans/spring-beans-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/mvc    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">    <!-- 自动扫描的包名,我的是com.lxw.controller -->    <context:component-scan base-package="com.lxw.controller" ></context:component-scan>    <!-- 默认的注解映射的支持 -->    <mvc:annotation-driven /></beans> 

4.在pom.xml中加入如下依赖:

<dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-core-asl</artifactId> <version>1.8.5</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.8.5</version> </dependency> 

 

5.Java代码:

 1 package com.lxw.controller; 2  3 import java.util.LinkedList; 4 import java.util.List; 5  6 import org.springframework.stereotype.Controller; 7 import org.springframework.web.bind.annotation.RequestMapping; 8 import org.springframework.web.bind.annotation.ResponseBody; 9 10 @Controller11 public class ResponseBodyAnnotation {12     @RequestMapping(value="http://www.mamicode.com/topics/json")13     @ResponseBody14     public List<Topic> getTextString(){15         System.out.println("Annotation response");16         LinkedList<Topic> topics = new LinkedList<Topic>();17         topics.add(new Topic("a"));18         topics.add(new Topic("b"));19         topics.add(new Topic("c"));20         topics.add(new Topic("d"));21         topics.add(new Topic("e"));22         return topics;23     }24     class Topic{25         private String id;26         public String getId() {27             return id;28         }29 30         public void setId(String id) {31             this.id = id;32         }33 34         public int getFlag() {35             return flag;36         }37 38         public void setFlag(int flag) {39             this.flag = flag;40         }41 42         private int flag = 0;43 44         public Topic(String id) {45             this.id = id;46         }47 48         @Override49         public boolean equals(Object obj) {50             return this.id.equals(((Topic) obj).getId());51         }52     }53 }
View Code

 

6.运行!然后在浏览器中输入:"http://localhost:port/projectname/topics/json" , port为端口号,projectname为项目名称.

7.运行过程中如果出现406错误,则可能是没导入json的jar包.http://pan.baidu.com/s/1gdqwNUb(该链接如果失效,请另想办法).

 

maven工程中,spring mvc @ResponseBody的使用