对json的支持
package com.myy.ssm.controller;
import com.myy.ssm.model.Book;
import com.myy.ssm.service.BookService;
import com.myy.ssm.utils.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
* @author 熊贝贝
* @site www.myy.com
* @company
* @create 2019-11-23 14:28
* springmvc中五种返回值处理情况
* 转发三种
* 转发到安全目录web-inf下
* 转发到根目录
* 转发到requestMapping
* 重定向两种
* 根目录
* requestMapping
@Controller
public class HelloController {
@Autowired
private BookService bookService;
@RequestMapping("/say1")
public String say1(){
return "hello";
@RequestMapping("/say2")
public ModelAndView say2(){
ModelAndView mv = new ModelAndView();
mv.setViewName("/hello2");
mv.addObject("msg","换种方式继续");
return mv;
@RequestMapping("/req1")
public String req1() {
System.out.println("转发到安全目录web-inf下...");
return "a";
@RequestMapping("/req2")
public String req2() {
System.out.println("转发到根目录...");
return "forward:/b.jsp";
@RequestMapping("/req3")
public String req3() {
System.out.println("转发到requestMapping...");
return "forward:/req2";
@RequestMapping("/red1")
public String red1() {
System.out.println("red1...");
return "redirect:/b.jsp";
@RequestMapping("/red2")
public String red2() {
System.out.println("red2...");
return "redirect:/req2";
@ResponseBody
@RequestMapping("/json1")
public Map json1(HttpServletRequest req){
PageBean pageBean = new PageBean();
pageBean.setRequest(req);
List<Map> list = bookService.listPager(new Book(), pageBean);
Map map = new HashMap();
map.put("total",101);
map.put("data",list);
return map;
@ResponseBody
@RequestMapping("/json2")
public List<Map> json2(HttpServletRequest req){
PageBean pageBean = new PageBean();
pageBean.setRequest(req);
List<Map> list = bookService.listPager(new Book(), pageBean);
return list;
@ResponseBody
@RequestMapping("/json3")
public String json3(HttpServletRequest req){
return "springmvc string to json";
@ResponseBody 将结果进行json转换
分别运行显示:
json2:
json2:
json格式:
"bname": "斗破第13章",
"price": 13.0,
"bid": 13
}, {
"bname": "圣墟第16章",
"price": 16.0,
"bid": 16
}, {
"bname": "圣墟第18章",
"price": 18.0,
"bid": 18
}, {
"bname": "圣墟第19章",
"price": 19.0,
"bid": 19
}, {
"bname": "圣墟第20章",
"price": 20.0,
"bid": 20
}, {
"bname": "圣墟第21章",
"price": 21.0,
"bid": 21
}, {
"bname": "圣墟第22章",
"price": 22.0,
"bid": 22
}, {
"bname": "仙逆第23章",
"price": 23.0,
"bid": 23
}, {
"bname": "圣墟第24章",
"price": 24.0,
"bid": 24
}, {
"bname": "斗破",
"price": 123.0,
"bid": 25
json1:
json格式:
"total": 101,
"data": [{
"bname": "斗破第13章",
"price": 13.0,
"bid": 13
}, {
"bname": "圣墟第16章",
"price": 16.0,
"bid": 16
}, {
"bname": "圣墟第18章",
"price": 18.0,
"bid": 18
}, {
"bname": "圣墟第19章",
"price": 19.0,
"bid": 19
}, {
"bname": "圣墟第20章",
"price": 20.0,
"bid": 20
}, {
"bname": "圣墟第21章",
"price": 21.0,
"bid": 21
}, {
"bname": "圣墟第22章",
"price": 22.0,
"bid": 22
}, {
"bname": "仙逆第23章",
"price": 23.0,
"bid": 23
}, {
"bname": "圣墟第24章",
"price": 24.0,
"bid": 24
}, {
"bname": "斗破",
"price": 123.0,
"bid": 25
json在线解析:https://www.sojson.com/