package com.example.demo.Controller;
import com.example.demo.Entity.User;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@RestController
@RequestMapping("/test")
public class TestController {
/**
* 请求:http://localhost:8080/test/hello/
* http方式:get 请求
* 返回contentType: text/plain
* 请求responseBody: "Hello page"
* @Description
* @author Administrator
* @return
*/
@RequestMapping("/")
public String index() {
return "Hello page";
}
/**
* 请求:http://localhost:8080/test/hello/testname
* 请求返回contentType:
* responseBody: "Hello testname!!!"
* @Description
* @author Administrator
* @param name
* @return
*/
@RequestMapping("/hello/{name}")
public String hello(@PathVariable String name){
return "hello " + name;
}
/**
* 请求:http://localhost:8080/test/hello4?id=123&name=abc
* http方式:get
* 请求返回contentType: text/plain
* 请求responseBody: ""id:123 name:abc""
* @Description
* @author Administrator
* @param id
* @param name
* @return
*/
@RequestMapping("/helloid")
public String helloId(int id,String name){
return "hello : " + id + " name : " + name;
}
/**
* 请求:http://localhost:8080/test//getDemo/testname
* http方式:get
* 请求返回contentType: application/json
* 请求responseBody: { "createTime": 1495640486000, "id": 123, "name": "good122223" } fastjson支持
* 返回对象则自动解析为json字符串 因为spring boot
* 默认使用json解析框架自动返回,返回头是Content-Type:application/json;charset=UTF-8
* @Description
* @author Administrator
* @param myName
* @return {"id":123,"name":"good","createTime":1492782569909}
*/
@RequestMapping("/helloObject")
public User helloId(String name){
User user = new User();
user.setId(123);
user.setName(name);
user.setTime(new Date());
return user;
}
/**
* 请求:http://localhost:8080/test/getMapping2?id=123&name=abc
* http方式:get
* 请求返回contentType: application/json
* 请求responseBody: { "id": 123, "name": "abc" }
* 请求boty设置为 application/x-www-form-urlencoded 也可以接受参数
* body : id=123&name=abc
* @RequestMapping(value="/getMapping" )
* 等于 @RequestMapping(value="/getMapping" , method={RequestMethod.GET,RequestMethod.POST})
* @Description
* @param user
* @return
*/
@RequestMapping(value = "/getMapping2", method = { RequestMethod.GET, RequestMethod.POST })
public User getMapping2(User user) {
return user;
}
/**
* @RequestBody String 直接获取请求体, 不封装
* 请求:http://localhost:8080/test/get2
* http方式:post
* {"createTime":1495640486000,"name":"good122223","id":123}
* 请求返回contentType: application/json 请求responseBody:
* {"createTime":1495640486000,"name":"good122223","id":123}
* @Description
* @param user
* @return
*/
@RequestMapping("/get3")
public User get3(@RequestBody String reqContent, @RequestBody User user) {
// {"name":"abc","id":123}
System.out.println("reqContent:" + reqContent);
System.out.println(user.toString());
return user;
}
/**
* 对json数据进行实体类封装 请求:http://localhost:8080/test/user
* http方式:post
* 请求requestBody:
* {"createTime":1495640486000,"name":"good122223","id":123}
* 请求返回contentType: application/json
* 请求responseBody: {"createTime":1495640486000,"name":"good122223","id":123}
* @Description
* @param user
* @return
*/
// {"id":2,"username":"user2","name":"李四","age":20,"balance":100.00}
// 发送格式没application/json
@PostMapping("/user")
public User postUser(@RequestBody User user) {
System.out.println(user);
return user;
}
/**
* 请求:http://localhost:8080/test/list-all
* http方式:get
* 请求返回contentType:
* 请求responseBody: [ { "createTime": 1495641815658, "id": 1, "name":
* "zhangsan" }, { "createTime": 1495641815658, "id": 1, "name": "zhangsan"
* }, { "createTime": 1495641815658, "id": 1, "name": "zhangsan" } ]
*
* @Description
* @param user
* @return
*/
@GetMapping("list-all")
public List<User> listAll() {
ArrayList<User> list = new ArrayList<User>();
User user = new User(1, "zhangsan", new Date());
User user2 = new User(1, "zhangsan", new Date());
User user3 = new User(1, "zhangsan", new Date());
list.add(user);
list.add(user2);
list.add(user3);
return list;
}
}