springboot请求响应——响应

复制代码
package com.hxy.springboot.springbootdemo01.demo01;

import com.hxy.springboot.springbootdemo01.pojo.Result;
import com.hxy.springboot.springbootdemo01.pojo.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.DateFormat;
import java.time.LocalDateTime;
注:@RestController=@ResponseBody+@Controller,一方面用Controller标签标记该类为控制层,一方面用@ResponseBody表明需要返回响应。
@RestController
//请求标签
public class Class3 {
注:@RequestMapping表明响应路径
    @RequestMapping("/class3")
        注:@DateTimeFormat表明映射形式为"时间"
    public Result class3(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm")LocalDateTime localDateTime) {
        System.out.println(localDateTime);
        Result result = new Result();
        return result.success(1,"success", localDateTime);
    }

    @RequestMapping("/jasonparam")
         注:@RequestBody用于标记接收的内容为响应体,对应的在Postman中的url中需要使用jason格式进行发送
    public Result jasonparam(@RequestBody User user) {
        System.out.println(user.toString());
        Result result = new Result();
        return result.success(1,"success", user);
    }

    @RequestMapping("/jasonparam2")
        注:本响应接口与上述响应接口不同的地方在于没有使用@RequestBody标签,所以postman中也对应的是直接在Url中添加对应属性内容
    public Result jasonparam1( User user) {
        System.out.println(user.toString());
        Result result = new Result();
        return result.success(1,"success", user);
    }

    @RequestMapping("/path/{id}/{name}")
        注:@PathVariable表明响应的是一个路径格式,标签中需要与形参的名字相对应
    public Result pathtest(@PathVariable int id, @PathVariable String name) {
        System.out.println(id);
        System.out.println(name);
        Result result = new Result();
        return result.success(1,"success", id+name);
    }
}
相关推荐
大鸡腿同学7 小时前
【成长类】《只有偏执狂才能生存》读书笔记:程序员的偏执型成长地图
后端
0xDevNull7 小时前
MySQL数据冷热分离详解
后端·mysql
一定要AK7 小时前
Spring 入门核心笔记
java·笔记·spring
A__tao7 小时前
Elasticsearch Mapping 一键生成 Java 实体类(支持嵌套 + 自动过滤注释)
java·python·elasticsearch
AI袋鼠帝7 小时前
OpenClaw(龙虾)最强开源对手!Github 40K Star了,又一个爆火的Agent..
后端
KevinCyao7 小时前
java视频短信接口怎么调用?SpringBoot集成视频短信及回调处理Demo
java·spring boot·音视频
迷藏4947 小时前
**发散创新:基于Rust实现的开源合规权限管理框架设计与实践**在现代软件架构中,**权限控制(RBAC)** 已成为保障
java·开发语言·python·rust·开源
總鑽風8 小时前
搭建Spring Boot + ELK日志平台,实现可视化日志监控
spring boot·elk·macos
不吃香菜学java8 小时前
Redis简单应用
数据库·spring boot·tomcat·maven
wuxinyan1238 小时前
Java面试题47:一文深入了解Nginx
java·nginx·面试题