欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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;
    }

}