SpringBoot注解

URL中的参数注解

1、获取url中的数据:@PathVariable 

URL = localhost:8080/api/{databaseKey}/file

为了获取URL中的databasekey,可以如下编写代码

@RestController
@RequestMapping("/api")
public class FileController {
    @RequestMapping("/{databaseKey}/file")
    public String getDatabaseKey(@PathVariable String databaseKey) {
        return databaseKey;
    }
}

备注:通过@PathVariable注解来获取URL中参数的前提条件是已经提前知道URL的格式是什么样子

2、获取请求参数的值:@RequetParam

URL = localhost:8080/api/file?fileKey=M201876146

为了获取fileKey的值,可以如下编写代码

@RestController
@RequestMapping("/api")
public class FileController {
    @RequestMapping("/file")
    public String getDatabaseKey(@RequestParam("fileKey") String fileKey) {
        return fileKey;
    }
}

常用注解

1、请求映射注解:@RequestMapping

参数value : 请求映射的地址

参数method : 将请求映射到一个特定的HTTP方法(GET/PUT/POST/DELETE)

参数consumesproduces用来缩小请求映射类型的范围,consumes指定处理请求的提交内容;produces指定返回的内容类型,例如

@RequestMapping(method = RequestMethod.POST, value="/{databaseKey}/files",consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)接受表单数据,返回JSON数据

未完待续。。。

原文地址:https://www.cnblogs.com/SChenqi/p/10511311.html