SpringBoot常见请求参数

2023-12-26 06:06:50

package com.itheima.controller;

import com.itheima.controller.pojo.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.websocket.server.PathParam;
import java.lang.reflect.Array;
import java.net.http.HttpRequest;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@RestController
public class SimpleController {

    //    @RequestMapping("/simpleMethod")
//    public String simpleMethod(HttpServletRequest request){
//        String name = request.getParameter("name");
//        String ageStr = request.getParameter("age");
//        int age = Integer.parseInt(ageStr);
//        System.out.println(name+":"+age);
//        return "ok";
//    }
    @RequestMapping("/simpleMethod")
    public String simpleMethod(String name, @RequestParam(name = "age", required = false) Integer agestr) {

        System.out.println(name + ":" + agestr);
        return "ok";
    }

    @RequestMapping("/simpleUser")
    public String simpleUser(User user) {

        System.out.println(user);
        return "ok";
    }

    @RequestMapping("/jsonParam")
    public String jsonParam(@RequestBody User user) {

        System.out.println(user);
        return "ok";
    }

    @RequestMapping("/arrayParam")
    public String arrayParam(String[] hobby) {

        System.out.println(Arrays.toString(hobby));
        return "ok";
    }
    @RequestMapping("/listParam")
    public String listParam(  @RequestParam List<String> hobby) {

        System.out.println(hobby);
        return "ok";
    }
    @RequestMapping("/dateParam")
    public String dateParam(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime updatetime) {

        System.out.println(updatetime);
        return "ok";
    }
    @RequestMapping("/pathParam/{id}/{name}")
    public String pathParam(@PathVariable Integer id,@PathVariable String name) {

        System.out.println(id);
        System.out.println(name);
        return "ok";
    }

}

首先在idea中创建一个Springboot项目,然后在src/main/java创建的包下,创建一个contorller,在里面书写内容。

写好之后,先启动Springboot项目,然后在Aipfox里面创建接口,发送请求。

?

文章来源:https://blog.csdn.net/gang61/article/details/135207081
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。