在传统的开发过程中,我们的控制CONTROLL层通常需要转向一个JSP视图;但随着WEB2.0相关技术的崛起,我们很多时候只需要返回数据即可,而不是一个JSP页面。

SPRING MVC3的@ResponseBody使Controller直接返回数据,而不是直接指向具体的视图;同时通过

MessageConverter和produces(如produces="text/plain;charset=UTF-8")可以返回各种格式的数据(XML,json,RSS,TEXT,字节流等),本章只介绍最简单的使用;

见代码:

@ResponseBody可以直接返回结果,

而ResponseEntity 可以定义返回的HttpHeaders和HttpStatus,见文章最后的两张图

  1. @RequestMapping(value="/response", method=RequestMethod.GET)
  2. public class ResponseController {
  3. //http://127.0.0.1:8010/response/annotation
  4. @RequestMapping("/annotation")
  5. public @ResponseBody String responseBody() {
  6. return "The String ResponseBody";
  7. }
  8. @RequestMapping("/charset/accept")
  9. public @ResponseBody String responseAcceptHeaderCharset() {
  10. return "\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01 (\"Hello world!\" in Japanese)";
  11. }
  12. //http://127.0.0.1:8010/response/charset/produce
  13. @RequestMapping(value="/charset/produce", produces="text/plain;charset=UTF-8")
  14. public @ResponseBody String responseProducesConditionCharset() {
  15. return "\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01 (\"Hello world!\" in Japanese)";
  16. }
  17. //http://127.0.0.1:8010/response/entity/status
  18. @RequestMapping("/entity/status")
  19. public ResponseEntity<String> responseEntityStatusCode() {
  20. return new ResponseEntity<String>("The String ResponseBody with custom status code (403 Forbidden)",
  21. HttpStatus.FORBIDDEN);
  22. }
  23. //http://127.0.0.1:8010/response/entity/headers
  24. @RequestMapping("/entity/headers")
  25. public ResponseEntity<String> responseEntityCustomHeaders() {
  26. HttpHeaders headers = new HttpHeaders();
  27. headers.setContentType(MediaType.TEXT_PLAIN);
  28. return new ResponseEntity<String>("The String ResponseBody with custom header Content-Type=text/plain",
  29. headers, HttpStatus.OK);
  30. }
  31. }


 

 

[转发]SPRING MVC3.2案例讲解--SPRING MVC3的@ResponseBody和ResponseEntity的更多相关文章

  1. 从一个简单案例上手Spring MVC,同时分析Spring MVC面试问题

    很多公司都会用Spring MVC,而且初级程序员在面试时,一定会被问到这方面的问题,所以这里我们来通过一个简单的案例来分析Spring MVC,事实上,我们在培训中就用这个举例,很多零基础的程序员能 ...

  2. spring原理案例-基本项目搭建 02 spring jar包详解 spring jar包的用途

    Spring4 Jar包详解 SpringJava Spring AOP: Spring的面向切面编程,提供AOP(面向切面编程)的实现 Spring Aspects: Spring提供的对Aspec ...

  3. 案例学编程系列:案例认识 Spring IOC

    本文spring libs 地址:https://github.com/yizhiamumu/springlibs Spring 能帮我们做什么 ①.Spring 能帮我们根据配置文件创建及组装对象之 ...

  4. Spring和SpringMvc详细讲解

    转载自:https://www.cnblogs.com/doudouxiaoye/p/5693399.html 1. 为什么使用Spring ? 1). 方便解耦,简化开发 通过Spring提供的Io ...

  5. Java基础-SSM之Spring和Mybatis以及Spring MVC整合案例

    Java基础-SSM之Spring和Mybatis以及Spring MVC整合案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 能看到这篇文章的小伙伴,详细你已经有一定的Java ...

  6. 一个缓存使用案例:Spring Cache VS Caffeine 原生 API

    最近在学习本地缓存发现,在 Spring 技术栈的开发中,既可以使用 Spring Cache 的注解形式操作缓存,也可用各种缓存方案的原生 API.那么是否 Spring 官方提供的就是最合适的方案 ...

  7. [案例一] Spring中的事件驱动模型(机制)

    事件驱动模型是观察者模式的另一种形态,观察者相当于监听器,被观察者相当于事件源 事件源产生事件,监听器监听事件 以用户注册时候,要发送邮件和发送短信举例说明 定义一个事件 /** * spring会自 ...

  8. spring3 jsp页面使用<form:form modelAttribute="xxxx" action="xxxx">报错,附连接数据库的spring MVC annotation 案例

    在写一个使用spring3 的form标签的例子时,一直报错,错误信息为:java.lang.IllegalStateException: Neither BindingResult nor plai ...

  9. Spring Boot 入门案例与配置说明

    一.Spring Boot简介 官网地址:http://spring.io/projects/spring-boot Spring Boot可以轻松创建可以运行的独立的,生产级的基于Spring的应用 ...

随机推荐

  1. log4cplus基本用法

    说起日志系统,不得不提大名鼎鼎的Log4j.特别是使用Java的人们,能够说是无人不知无人不晓无人不用. Log4j以其简单的使用方式(引入一个jar包.一行代码就可以调用).灵活(可通过配置文件任意 ...

  2. 数论 - SGU 107 987654321 problem

    987654321 problem Problem's Link Mean: 略 analyse: 这道题目是道简单题. 不过的确要好好想一下: 通过简单的搜索可以知道,在N<9时答案一定为0, ...

  3. before伪类的超有用应用技巧——水平菜单竖线分隔符

    方法一.li前面加before伪类 <!doctype html> <html dir="ltr" lang="zh-CN"> < ...

  4. node 下好用的工具

    1. supervisor Node Supervisor is used to restart programs when they crash. Node Supervisor 是用来当程序崩溃时 ...

  5. windows环境通过cmd命令到ftp上下载文件到linux服务器

    转自:http://jingyan.baidu.com/article/6525d4b1300912ac7d2e941b.html

  6. Activity 5秒 Broadcast 10秒 Service 20秒

    第一:什么会引发ANR? 在Android里,应用程序的响应性是由Activity Manager和WindowManager系统服务监视的 .当它监测到以下情况中的一个时,Android就会针对特定 ...

  7. poj_3159 最短路

    题目大意 有N个孩子(N<=3000)分糖果.有M个关系(M<=150,000).每个关系形如:A B C 表示第B个学生比第A个学生多分到的糖果数目,不能超过C.求第N个学生最多比第1个 ...

  8. c++ 利用容器vector动态的定义二维数组

    #include <iostream> #include <vector> using namespace std; int main() { int row, column; ...

  9. 1455: 罗马游戏[左偏树or可并堆]

    1455: 罗马游戏 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1861  Solved: 798[Submit][Status][Discuss] ...

  10. MySQL中关于SQL注入的相关需要的基础知识

    零.绪论: 文章部分整理来源于公司同事,特此鸣谢!!! 一.关于注入点在KEY上的注入: 我们来看一个查询,你的第一个字段是过滤器(filter)第二个字段是查询的关键字,例如查询ip == 1.2. ...