与同事对接接口时,常常没有完整的定义传输格式,请求方式而经常导致口头交流,有时候又不能准确的区分之间的请求方式,以及接收的类型。故写此测试方法。
仅个人测试,也许存在错误,还望指正
ps:注释掉的是设想封装请求,但没有成功的(具体原因日后分析)
package online.controller;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import online.entity.UserDao;
import org.springframework.web.bind.annotation.*;
import javax.websocket.server.PathParam;
import java.util.ArrayList;
import java.util.Map;
/**
* Controller测试接收各个参数
* 各种请求对应各种接收方式
*/
@Slf4j
@RestController
@RequestMapping("ParamsController")
public class ParamsController {
/**
* restful
*/
@GetMapping("restful/{name}")
public Object restful(@PathVariable("name") String name) {
log.error("method: {}, name= {}", Thread.currentThread().getStackTrace()[1].getMethodName(), name);
return name;
}
@GetMapping("restful")
public Object restful1(@PathParam("name") String name) {
log.error("method: {}, name= {}", Thread.currentThread().getStackTrace()[1].getMethodName(), name);
return name;
}
/**
* 对象
* http://localhost/ParamsController/entry?id=123&name=xiaomimg
* Body中fromData
*/
@GetMapping("entry")
public Object entry(UserDao userDao) {
log.error("method: {}, name= {}", Thread.currentThread().getStackTrace()[1].getMethodName(), JSON.toJSONString(userDao));
return userDao;
}
/**
* 对象
* 基础类-post/get均可
* Body中 row
*/
@RequestMapping("/entryBody")
public Object entryBody(@RequestBody UserDao userDao) {
log.error("method: {}, name= {}", Thread.currentThread().getStackTrace()[1].getMethodName(), JSON.toJSONString(userDao));
return userDao;
}
/**
* List 失败,没有构造参数
* ArrayList 没有注入进去
*/
// @RequestMapping("/listTest")
// public Object listTest(ArrayList<String> name) {
// log.error("method: {}, name= {}", Thread.currentThread().getStackTrace()[1].getMethodName(), JSON.toJSONString(name));
// return name;
// }
/**
* List 失败,没有构造参数
* ArrayList 没有注入进去
*/
// @RequestMapping("/listUserTest")
// public Object listUserTest(@RequestBody ArrayList<UserDao> name) {
// log.error("method: {}, name= {}", Thread.currentThread().getStackTrace()[1].getMethodName(), JSON.toJSONString(name));
// return name;
// }
/**
* List 失败,没有构造参数
* ArrayList 没有注入进去
*/
@RequestMapping("/listBodyTest")
public Object listBodyTest(@RequestBody() ArrayList<String> name) {
log.error("method: {}, name= {}", Thread.currentThread().getStackTrace()[1].getMethodName(), JSON.toJSONString(name));
return name;
}
/**+
* Map
* mapTest?name=1&name1=2&name11=3
* body中FromData
*/
@RequestMapping("mapTest")
public Object mapTest(@RequestParam Map<String, Object> map) {
log.error("method: {}, name= {}", Thread.currentThread().getStackTrace()[1].getMethodName(), JSON.toJSONString(map));
return map;
}
/**
* Map
* body中raw
*/
@RequestMapping("mapBodyTest")
public Object mapBodyTest(@RequestBody Map<String, Object> map) {
log.error("method: {}, name= {}", Thread.currentThread().getStackTrace()[1].getMethodName(), JSON.toJSONString(map));
return map;
}
/**
* String[][不通]
* /StringTest?str=1&str=2&str=3
* Body-fromData
*/
// @RequestMapping("StringTest")
// public Object StringTest(String[] str) {
// log.error("method: {}, name= {}", Thread.currentThread().getStackTrace()[1].getMethodName(), JSON.toJSONString(str));
// return str;
// }
/**
* String[][不通]
* Body-raw
*/
// @RequestMapping("StringBodyTest")
// public Object StringBodyTest(@RequestBody String[] str) {
// log.error("method: {}, name= {}", Thread.currentThread().getStackTrace()[1].getMethodName(), JSON.toJSONString(str));
// return str;
// }
/**
* Integer...[不通]
*/
// @RequestMapping("integerBodyTest")
// public Object integerBodyTest(@RequestBody Integer... str) {
// log.error("method: {}, name= {}", Thread.currentThread().getStackTrace()[1].getMethodName(), JSON.toJSONString(str));
// return str;
// }
/**
* Integer...
* /integerTest?str=1&str=2&str=3
* Body-fromData
*/
@RequestMapping("integerTest")
public Object integerTest(Integer... str) {
log.error("method: {}, name= {}", Thread.currentThread().getStackTrace()[1].getMethodName(), JSON.toJSONString(str));
return str;
}
}