##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…
##5.选择get或者post方式。http://localhost:8080 + /test/t1
##6.执行出现如下图所示 success 表示成功
##7.网址上输入http://localhost:8080/test/t1?id=7 显示success
##8.post请求
a.需要按 Convert request to the format
b.修改成 POST http://localhost:8080/test/t2
c.输出json串{“id”: 10,“username”: “小卢先生”}
d.点绿三角运行会出现success
版权声明:本文为m0_69712602原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。