spring mvc返回值处理
程序员文章站
2022-07-15 13:27:31
...
<%--
Created by IntelliJ IDEA.
User: 黄国栋
Date: 2020/6/26
Time: 18:27
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>JSON格式数据参数绑定</title>
</head>
<body>
<h1><a href="responseBody/returnString">测试String类型返回值</a></h1><br>
<h1><a href="responseBody/returnPojo">测试POJO类型返回值</a></h1><br>
<h1><a href="restController/returnString">测试String类型返回值</a></h1><br>
<h1><a href="restController/returnPojo">测试POJO类型返回值</a></h1><br>
</body>
</html>
package com.hgd.ssm.controller;
import com.hgd.ssm.po.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* @auther 黄国栋
* @data 2020-06-26 20:10
* @since
*/
@RequestMapping("responseBody")
@Controller
public class MyResponseBodyController {
@RequestMapping(value = "returnString",produces = "text/plain;charset=utf-8")
@ResponseBody
public String returnString(){
return "查询失败....";
}
@RequestMapping("returnPojo")
@ResponseBody
public User returnPojo(){
User user=new User();
user.setId(1);
user.setUsername("黄国栋");
user.setSex("男");
return user;
}
}
package com.hgd.ssm.controller;
import com.hgd.ssm.po.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
/**
* @auther 黄国栋
* @data 2020-06-26 20:29
* @since
*/
@RequestMapping("restController")
@RestController
public class MyRestController {
@RequestMapping(value = "returnString",produces = "text/plain;charset=utf-8")
@ResponseBody
public String returnString(){
return "查询失败....";
}
@RequestMapping("returnPojo")
@ResponseBody
public User returnPojo(){
User user=new User();
user.setId(1);
user.setUsername("黄国栋");
user.setSex("男");
return user;
}
}
推荐阅读
-
Spring MVC源码(三) ----- @RequestBody和@ResponseBody原理解析
-
python多任务及返回值的处理方法
-
你知道@RequestMapping的name属性有什么用吗?【享学Spring MVC】
-
MVC异常处理详解
-
[Spring Boot]使用自定义注解统一请求返回值
-
spring AOP定义AfterThrowing增加处理实例分析
-
品Spring:对@Autowired和@Value注解的处理方法
-
关于Spring MVC在Controller层中注入request的坑详解
-
干货分享:ASP.NET CORE(C#)与Spring Boot MVC(JAVA)异曲同工的编程方式总结
-
基于Maven 的 Spring MVC