springboot快速入门-4.常用取参数方法
POST
POST提交JSON
@PostMapping(value = "/agreeRefund")
public Result agreeRefund(@RequestBody Map map){
Object headId = map.get("headId");
if(CheckUtils.isEmpty(headId)){
return Result.error("参数错误");
}
return signService.agreeRefund(headId.toString());
}
POST提交JSON
JSON对象必须和实体类对象一致,这样可以自动转为实体类对象
@PostMapping("/saveAndSign")
public Result saveAndSign(@RequestBody NyjOrderHead nyjOrderHead){
if(CheckUtils.isEmpty(nyjOrderHead)){
return Result.error("数据错误");
}
return signService.saveAndSign(nyjOrderHead);
}
POST提交网址传参
@PostMapping("/test3")
public String test3(HttpServletRequest request){
log.info("id : "+request.getParameter("id"));
return "";
}
POST提交网址传参
@PostMapping("/test4")
public String test4(String id){
log.info("id : "+id);
return "";
}
GET
GET网址传参
@GetMapping("/test2")
public String test2(HttpServletRequest request){
log.info("id : "+request.getParameter("id"));
return "";
}
GET网址传参
@GetMapping("/test5")
public String test5(String id){
log.info("id : "+id);
return "";
}
GET网址传参
@GetMapping("/test6")
public String test6(@RequestParam(name = "id", required = true) String id){
log.info("id : "+id);
return "";
}
GET路由参数
@GetMapping("/test7/{id}")
public String test7(@PathVariable(name = "id", required = true) String id){
log.info("id : "+id);
return "";
}
特殊案例
POST上传文件(图片举例)
@RequestMapping("/uploadImg")
public JSONObject uploadImg(HttpServletRequest request, MultipartFile file){
JSONObject jsonObject = new JSONObject();
String mid = request.getParameter("mid");
String rootPath = System.getProperty("catalina.home");
rootPath = rootPath + "/img_data";
File f = new File(rootPath);
if(!f.exists()){
f.mkdir();
}
if (StringUtils.isBlank(mid)){
jsonObject.put("code",-100);
jsonObject.put("msg","mid is error");
return jsonObject;
}
String uuidStr = UUID.randomUUID().toString().replace("-","");
String fileName = uuidStr + ".png";
String filePath = rootPath + "/" + fileName;//本地绝对路径
log.info("[图片接口]上传的图片本地绝对路径为->" + filePath);
f = new File(filePath);
try {
file.transferTo(f);
} catch (IOException e) {
log.error("[图片接口]写文件到磁盘失败", e);
}
jsonObject.put("code",200);
jsonObject.put("path",fileName);
return jsonObject;
}
GET访问图片
@RequestMapping("/getImg/{filename}")
public void getImg(HttpServletResponse response, @PathVariable("filename") String filename){
if(StringUtils.isBlank(filename)){
//throw new AbpException("[图片接口]参数有误");
}
String rootPath = System.getProperty("catalina.home");
String filePath = rootPath + "/img_data/" + filename;
File imageFile = new File(filePath);
if (imageFile.exists()){
FileInputStream fis = null;
OutputStream os = null;
try {
fis = new FileInputStream(imageFile);
os = response.getOutputStream();
int count = 0;
byte[] buffer = new byte[1024 * 8];
while((count = fis.read(buffer)) != -1){
os.write(buffer,0,count);
os.flush();
}
log.info("[图片接口]输出完成");
} catch (Exception e) {
log.error("[图片接口]打开图片失败,可能图片ID错误", e);
//throw new AbpException("[图片接口]打开图片失败,可能图片ID错误");
}finally {
if(fis != null){
try {
fis.close();
} catch (IOException e) {
log.error("[图片接口]关闭输入流失败", e);
//throw new AbpException("[图片接口]关闭输入流失败");
}
}
if(os != null){
try {
os.close();
} catch (IOException e) {
log.error("[图片接口]关闭输出流失败", e);
//throw new AbpException("[图片接口]关闭输出流失败");
}
}
}
}else{
log.error("[图片接口]图片ID错误");
//throw new AbpException("[图片接口]图片ID错误");
}
}