【springBoot】springBoot返回json的一个问题
首先看下面的代码
- @Controller
- @RequestMapping("/users")
- public class UserController {
- @RequestMapping(method=RequestMethod.GET)
- public HttpResponse getList(HttpServletRequest req,HttpServletResponse rep){
- String id = req.getSession().getId();
- return new HttpResponse(id);
- }
- }
在通过ajax访问的时候会出现
- javax.servlet.ServletException: Circular view path [users]: would dispatch back to the current handler URL [/users] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)
这个异常,它的意思是没有指定视图结果,让你检查一下你的视图配置,在springmvc中我们是使用viewResolver,通过在controller中return的前缀来决定跳转到相应的视图
那么在springBoot怎么解决这个问题?
两个方案:
1、添加@ResponseBody
@Controller
@RequestMapping("/users")
public class UserController {
@RequestMapping(method=RequestMethod.GET)
@ResponseBody
public HttpResponse getList(HttpServletRequest req,HttpServletResponse rep){
String id = req.getSession().getId();
return new HttpResponse(id);
}
}
2、将@Controller换成@RestController// 标记为:restful
- @RestController
- @RequestMapping("/users")
- public class UserController {
- @RequestMapping(method=RequestMethod.GET)
- public HttpResponse getList(HttpServletRequest req,HttpServletResponse rep){
- String id = req.getSession().getId();
- return new HttpResponse(id);
- }
- }
Controller源码类
- org.springframework.stereotype.Controller
RestController源码类
- org.springframework.web.bind.annotation.RestController
两者区别在于
--------------------------------
ok
【springBoot】springBoot返回json的一个问题的更多相关文章
- 2.SpringBoot之返回json数据
一.创建一个springBoot个项目 操作详情参考:1.SpringBoo之Helloword 快速搭建一个web项目 二.编写实体类 /** * Created by CR7 on 2017-8- ...
- SpringBoot之返回json数据
一.创建一个springBoot个项目 二.编写实体类 /** * 返回Json数据实体类 */ public class User { private int id; private String ...
- 上手spring boot项目(四)之springboot如何返回json数据
在springboot整合thymeleaf中,经常会在HTML页面中接收来自服务器的json数据,然后处理json数据并在页面上渲染.那么如何在服务器中返回json类型的数据呢? 1.使用@Resp ...
- SpringBoot 02_返回json数据
在SpringBoot 01_HelloWorld的基础上来返回json的数据,现在前后端分离的情况下多数都是通过Json来进行交互,下面就来利用SpringBoot返回Json格式的数据. 1:新建 ...
- 第3章 springboot接口返回json 3-1 SpringBoot构造并返回一个json对象
数据的使用主要还是以JSON为主,我们不会去使用XML. 这个时候我们先不使用@RestController,我们使用之前SpringMVC的那种方式,就是@Controller. @Respons ...
- springboot统一返回json数据格式并配置系统异常拦截
本文链接:https://blog.csdn.net/syystx/article/details/82870217通常进行前后端分离开发时我们需要定义统一的json数据交互格式并对系统未处理异常进行 ...
- 第3章 springboot接口返回json 3-2 Jackson的基本演绎法
@JsonIgnore private String password; @JsonFormat(pattern="yyyy-MM-dd hh:mm:ss a",locale=&q ...
- SpringBoot 03_利用FastJson返回Json数据
自上一节:SpringBoot 02_返回json数据,可以返回json数据之后,由于有些人习惯于不同的Json框架,比如fastjson,这里介绍一下如何在SpringBoot中集成fastjson ...
- springboot 404返回自定义json(只进入过滤器)
今天在公司没事干,记一次springboot遇到的一些坑,在百度上也没有搜到类似的问题和答案(或者说 答案不是我想要的) 当我们在SpringBoot遇到了404或者500的错误的时候,你们会怎么办? ...
随机推荐
- Prepared Java infrastructure for distributed scenarios
code is sited on: https://github.com/zhoujiagen/javaiospike progress 2015/5/27 Nio/Nio2 examples, us ...
- sql语句 优化技巧
1.应尽量避免在 where 子句中使用!=或<>操作符,否则将引擎放弃使用索引而进行全表扫描. 2.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉 ...
- 350. Intersection of Two Arrays II
Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...
- tyvj 1067 dp 两次LIS(nlogn)
P1067 合唱队形 时间: 1000ms / 空间: 131072KiB / Java类名: Main 背景 NOIP2004 提高组 第三道 描述 N位同学站成一排,音乐老师要请其中的(N ...
- spark新能优化之数据本地化
数据本地化的背景: 数据本地化对于Spark Job性能有着巨大的影响.如果数据以及要计算它的代码是在一起的,那么性能当然会非常高.但是,如果数据和计算它的代码是分开的,那么其中之一必须到另外一方的机 ...
- scala言语基础学习十二
- URAL 1072 Routing(最短路)
Routing Time limit: 1.0 secondMemory limit: 64 MB There is a TCP/IP net of several computers. It mea ...
- CURL学习和应用
使用PHP的cURL库可以简单和有效地去抓网页.你只需要运行一个脚本,然后分析一下你所抓取的网页,然后就可以以程序的方式得到你想要的数据了.无论是你想从从一个链接上取部分数据,或是取一个XML文件并把 ...
- console下纯字符实现的俄罗斯方块
忙里偷闲,消遣一下,先上一张寒酸的效果图: 废话不多说,直接上代码,win7 64 code blocks编译通过. 吐槽一下cb的watch功能实在不够友好,不过免费的也不能要求太高. [按键说明] ...
- Android项目——传感器的使用
public class MainActivity extends Activity { // 定义 方向传感器 和 重力传感器 private TextView tvOrientation, tvA ...