spring controller 使用

本文介绍如何使用IDEA作为HTTP请求工具,并通过Spring框架实现RESTful Web服务。包括GET和POST请求处理,以及如何直接返回JSON格式的数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、idea IDE可以作为http请求的工具【尤其是post】
路径菜单栏 tools –>Test Restful Web Service
2、spring可以将输入和输出换成json 给 request和response传递出去,
所以没必要将接口的参数写成request

package com.alisa;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/my")
public class MyController {

    // 可以运行
    @RequestMapping(value = "/student", method = { RequestMethod.GET })
    public Student testReturnStudent() {
        Student student = new Student();
        student.setId(12);
        student.setName("alisa");
        return student;
    }

    // 可以运行
    @RequestMapping(value = "/studentPost", method = { RequestMethod.POST })
    public Student testReturnPostStudent() {
        Student student = new Student();
        student.setId(12);
        student.setName("alisa");
        return student;
    }

    // 可以运行
    @RequestMapping(value = "/params", method = { RequestMethod.GET })
    public Student testParamsStudent(Student student) {
        return student;
    }

    // 可以运行
    @RequestMapping(value = "/paramsPost", method = { RequestMethod.POST })
    public Student testParamsPostStudent(Student student) {
        return student;
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值