idea模拟发送http请求

##1.准备User对象

package com.jt.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class User {
    private Integer id;
    private String username;
    private String password;
    private String address;
}

##2.准备控制层 UserController

package com.jt.controller;
import com.jt.entity.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/test")
public class UserController {
    @RequestMapping("t1")
    public String t1(@RequestParam("id") String id){
        System.out.println(id);
        return "success";
    }
    @RequestMapping("t2")
    public String t2(@RequestBody User user){
        System.out.println(user);
        return "success";
    }
}

##3.启动主启动项
##4.Tools中HTTP Client 的Test…
idea模拟发送http请求_第1张图片
##5.选择get或者post方式。http://localhost:8080 + /test/t1
idea模拟发送http请求_第2张图片
##6.执行出现如下图所示 success 表示成功
在这里插入图片描述
##7.网址上输入http://localhost:8080/test/t1?id=7 显示success
idea模拟发送http请求_第3张图片
##8.post请求
a.需要按 Convert request to the format
b.修改成 POST http://localhost:8080/test/t2
c.输出json串{“id”: 10,“username”: “小卢先生”}
d.点绿三角运行会出现success
idea模拟发送http请求_第4张图片

你可能感兴趣的:(intellij-idea,http,java)