**1.@RestController = @Controller + @ResponseBody **
@controller主要负责处理前端控制器(DispatcherServlet )发过来的请求
@ResponseBody
@RestController注解里面包含了@Controller注解和@ResponseBody注解,@ResponseBody 注解是将返回的数据结构转换为 JSON 格式,所以说可以这么理解:@RestController = @Controller + @ResponseBody ,省了很多事,我们使用 @RestController 之后就不需要再使用 @Controller 了。
2.@RequestMapping
@RequestMapping 是一个用来处理请求地址映射的注解,它可以用于类上,也可以用于方法上。用于类上的注解会将一个特定请求或者请求模式映射到一个控制器之上,表示类中的所有响应请求的方法都是以该地址作为父路径;方法的级别上注解表示进一步指定到处理方法的映射关系。
该注解有6个属性,一般在项目中比较常用的有三个属性:value、method 和 produces。
value 属性:指定请求的实际地址,value 可以省略不写;
method 属性:指定请求的类型,主要有 GET、PUT、POST、DELETE,默认为 GET。
produces 属性:指定返回内容类型,如 produces = “application/json; charset=UTF-8”。
@RestController
@RequestMapping('/wms/rf/crashLog')
class RfCrashLogController extends BaseController {
@Autowired
MongoService mongoSvc
@RequestMapping(value = 'log', method = RequestMethod.POST)
ResponseMessage log(HttpServletRequest request, @RequestBody Map<String, Object> map) {
}
3.@PathVariable
@PathVariable 注解主要用来获取 URL 参数,Spring Boot 支持 Restfull 风格的 URL,比如一个 GET 请求携带一个参数 id,我们将 id 作为参数接收,可以使用 @PathVariable 注解。如下:
@GetMapping("/user/{id}")
public String Test(@PathVariable Integer id){
return String.valueof(id)
}
如果占位符 与接收的变量名称不一致必须指定 或者多个参数的时候
@GetMapping("/user/{idd}/{name}")
public String Test(@PathVariable(value ="idd" ) Integer id ,@PathVariable(value ="name") String name ){
return String.valueof(id)+name
}
运行项目,在浏览器中请求:localhost:8080/test/user/2/zhangsan, 可以看到控制台输出如下信息:
获取到的id为:2
获取到的name为:zhangsan
4.@RequestParam
@PathValiable 是从 URL 模板中获取参数值, 即这种风格的 URL:浏览器localhost:8080/test/user/2/zhangsan 请求中 :localhost:8080/test/user/{id}/{name}
@RequestParam 是从 Request 里获取参数值,即这种风格的 URL:
localhost:8080/test/user?id=1
@GetMapping("/user")
public String Test(@RequestParam Integer id){
return String.valueof(id)
}
@RequestParam 注解的value 属性是比较常用的,其作用和@PathVariable注解的value属性是一样的
required 属性:true 表示该参数必须要传,否则就会报 404 错误,false 表示可有可无。
defaultValue 属性:默认值,表示请求中没有同名参数时的默认值。
5.@RequestBody
比如前端通过 JSON 提交传来两个参数 username 和 password,此时我们需要在后端封装一个实体来接收。在传递的参数比较多的情况下,使用 @RequestBody 接收会非常方便
@PostMapping("/user")
public UserTest(@RequestBody User user){
return user
}
获取配置文件参数
ftp:
ftplp: 10.2.23.89
ftpPort: 21
ftpUser: uftp
ftpPwd: 12345678
ftpRemotePath: /home
package com.dbright.dataprediction.entity;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@PropertySource("classpath:ftpconfig.yml")
@ConfigurationProperties(prefix = "ftp")
public class FtpProperties {
@Value("${ftplp}")
public String ftplp;
@Value("${ftpPort}")
public String ftpPort;
@Value("${ftpUser}")
public String ftpUser;
@Value("${ftpPwd}")
public String ftpPwd;
@Value("${ftpRemotePath}")
public String ftpRemotePath;
}
读取自定义文件:须加注解
@PropertySource(value = {"classpath:config.yml","classpath:config.properties"})
读取application文件不需要加注解
// 中文
@Value("${message_zh}")
private String message_zh;