Spring学习(八)——Spring MVC之Restful API返回json数据

本文介绍如何在SpringMVC项目中实现前后端分离,通过引入Jackson依赖将Controller返回的对象转换为JSON格式,适用于现代Web应用开发。文章详细解释了使用@RestController和@ResponseBody注解的方法,展示了如何响应前端请求。

在上一篇博客的基础之上,对Spring MVC的使用有了一个基本的了解,而现在基本上都是用的前后端分离,因此需要对上一篇博客的demo进行一些修改,来满足我们的需要。

一、引入jackson依赖

jackson方便直接将Controller中的对象转为json对象返回给前端。

    <!--jackson-->
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-annotations</artifactId>
      <version>2.9.0</version>
    </dependency>

    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.9.0</version>
    </dependency>

二、在Controller类中添加注解

方式一:@RestController

可以在Controller类上直接添加@RestController注解,省去@Controller注解,因为@RestController相当于@ResponseBody@Controller一起的作用效果。在类上添加该注解后,下面的方法默认都将返回的对象转为json字符串。

package cool.gjh.controller;

import cool.gjh.entity.Student;
import cool.gjh.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Controller层
 * <p>
 *     Restful风格,返回json数据
 * </p>
 * @author ACE_GJH
 */
@RestController
@RequestMapping("/student")
public class StudentRestfulController {

    @Autowired
    private StudentService studentService;

    @RequestMapping(value = "/data", method = RequestMethod.GET)
    public Map<String, Object> showAll(){

        List<Student> students = studentService.getAllStudent();
        HashMap<String, Object> data = new HashMap<>(4*5/4, 0.8f);
        data.put("code", 200);
        data.put("msg", "OK");
        data.put("data", students);
        data.put("date", System.currentTimeMillis());

        return null;

    }

}

测试效果:
运行结果

方式二:@ResponseBody

在Controller类的方法上添加@ResponseBody注解,则该方法返回的对象将会以

package cool.gjh.controller;

import cool.gjh.entity.Student;
import cool.gjh.service.StudentService;
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.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Controller层
 * <p>
 *     Restful风格,返回json数据
 * </p>
 * @author ACE_GJH
 */
@Controller
@RequestMapping("/student")
public class StudentRestfulController {

    @Autowired
    private StudentService studentService;

    @ResponseBody
    @RequestMapping(value = "/data", method = RequestMethod.GET)
    public Map<String, Object> showAll(){

        List<Student> students = studentService.getAllStudent();
        HashMap<String, Object> data = new HashMap<>(4*5/4, 0.8f);
        data.put("code", 200);
        data.put("msg", "OK");
        data.put("data", students);
        data.put("date", System.currentTimeMillis());
        return data;

    }

}

测试效果:
测试效果2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

郭建華

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值