1、在 Spring Boot 中的 Controller 上使用 @RestController 注解
2、去掉方法上的 @ResponseBody
3、方法中,将要返回的值,添加到json中
4、方法中,返回格式更改为 JSONObject
import com.alibaba.fastjson.JSONObject;
@Controller
@RestController
public class Total
{
@GetMapping("/requstname")
public JSONObject test()
{
String name="zhangsan"
JSONObject jsonParam = new JSONObject();
jsonParam.put("person_name", name);
return jsonParam;
}
}
本文介绍如何在SpringBoot的Controller中利用@RestController注解,去除方法上的@ResponseBody,并将数据以JSONObject形式返回。示例代码展示了如何创建一个GET请求处理方法,返回包含姓名的JSONObject。

1302

被折叠的 条评论
为什么被折叠?



