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);
    }
}
相关推荐
钮钴禄·爱因斯晨26 分钟前
Java 集合进阶:从 Collection 接口到迭代器的实战指南
java·开发语言
rannn_11127 分钟前
【MySQL学习|黑马笔记|Day1】数据库概述,SQL|通用语法、SQL分类、DDL
数据库·后端·学习·mysql
超浪的晨35 分钟前
JavaWeb 入门:HTML 基础与实战详解(Java 开发者视角)
java·开发语言·前端·后端·html·个人开发
程序员爱钓鱼38 分钟前
Go语言实战案例-计算字符串编辑距离
后端·google·go
程序员爱钓鱼39 分钟前
Go语言实战案例-判断二叉树是否对称
后端·google·go
XiaoLeisj41 分钟前
【智能协同云图库】智能协同云图库第七弹:基于 Jsoup 爬虫实现以图搜图、颜色搜图、批量操作
spring boot·爬虫·servlet·java-ee·mybatis·门面模式·jsoup
Victor3561 小时前
MySQL(165)MySQL如何处理并发控制?
后端
风象南1 小时前
用 4 张图解释 CAP 到底在纠结什么
java·分布式·后端
Victor3561 小时前
MySQL(166)MVCC在MySQL中的实现原理是什么?
后端
越来越无动于衷3 小时前
基于 JWT 的登录验证功能实现详解
java·数据库·spring boot·mysql·mybatis