目录
@SuppressWarnings 取消警告
1、java.lang.SuppressWarnings 注解主要用在取消一些编译器产生的警告对代码左侧行列的遮挡,比如这会挡住断点调试时打的断点。
2、通过源码可知 @SuppressWarnings 其注解目标为类、字段、构造函数、方法、方法参数、方法内的局部变量。
3、@SuppressWarnings("value1","values2"...),其中的 value 取值如下:
关键字 | 用途 |
---|---|
all | to suppress all warnings (抑制所有警告) |
boxing | to suppress warnings relative to boxing/unboxing operations (抑制装箱、拆箱操作时候的警告) |
cast | to suppress warnings relative to cast operations (抑制映射相关的警告) |
dep-ann | to suppress warnings relative to deprecated annotation (抑制启用注释的警告) |
deprecation | to suppress warnings relative to deprecation (抑制过期方法警告) |
fallthrough | to suppress warnings relative to missing breaks in switch statements (抑制确在switch中缺失breaks的警告) |
finally | to suppress warnings relative to finally block that don’t return (抑制finally模块没有返回的警告) |
hiding | to suppress warnings relative to locals that hide variable(抑制相对于隐藏变量的局部变量的警告) |
incomplete-switch | to suppress warnings relative to missing entries in a switch statement (enum case)(忽略没有完整的switch语句) |
nls | to suppress warnings relative to non-nls string literals( 忽略非nls格式的字符) |
null | to suppress warnings relative to null analysis( 忽略对null的操作) |
rawtypes | to suppress warnings relative to un-specific types when using generics on class params( 使用generics时忽略没有指定相应的类型) |
restriction | to suppress warnings relative to usage of discouraged or forbidden references( 抑制禁止使用劝阻或禁止引用的警告) |
serial | to suppress warnings relative to missing serialVersionUID field for a serializable class( 忽略在serializable类中没有声明serialVersionUID变量) |
static-access | to suppress warnings relative to incorrect static access( 抑制不正确的静态访问方式警告) |
synthetic-access | to suppress warnings relative to unoptimized access from inner classes( 抑制子类没有按最优方法访问内部类的警告) |
unchecked | to suppress warnings relative to unchecked operations( 抑制没有进行类型检查操作的警告) |
unqualified-field-access | to suppress warnings relative to field access unqualified( 抑制没有权限访问的域的警告) |
unused | to suppress warnings relative to unused code( 抑制没被使用过的代码的警告) |
@SuppressWarnings({"unused", "unchecked"})
public void show() {
Date inco_date = new Date();// 到账日期
String inco_date_str = DateFormatUtils.format(inco_date, "yyyy-MM-dd HH:mm:ss");
}
@RequestParam 请求参数
1、org.springframework.web.bind.annotation.RequestParam 注解将浏览器请求参数绑定至控制层方法参数上。
2、@RequestParam 三个常用属性:
(1)value:请求参数名(必须配置)
扫描二维码关注公众号,回复: 8562617 查看本文章(2)required:是否必需,默认为 true,即请求中必须包含该参数,如果没有包含,将会抛出异常(可选配置)
(3)defaultValue:当没有传入参数时,则使用此默认值。如果设置了该值,required 将自动设为 false,无论是否配置了required,配置了什么值,都是 false(可选配置)
/**
* required 属性默认为 true,此时页面必须传入 uid 参数,否则报 400 错误
*
* @param uid value 属性的值与参数名称要相同
* @return
*/
@GetMapping("get1")
public String get1(@RequestParam(value = "uid") Integer uid) {
logger.info("get = >" + uid);
return "get = >" + uid;
}
/**
* required = false:此时页面可以不传入 uid 参数时,后台 uid 为 null
*
* @param uid :value 属性的值与参数名称要相同
* @return
*/
@GetMapping("get2")
public String get2(@RequestParam(value = "uid", required = false) Integer uid) {
logger.info("get = >" + uid);
return "get = >" + uid;
}
/**
* 此时页面没有传入 uid 参数时,将使用默认值 9527
*
* @param uid :value 属性的值与参数名称要相同
* @return
*/
@GetMapping("get3")
public String get3(@RequestParam(value = "uid", defaultValue = "9527") Integer uid) {
logger.info("get = >" + uid);
return "get = >" + uid;
}
/**
* 平时这种方法写的最多,当页面没有传入 uid 参数时,后台 uid 此时默认为 null
*
* @param uid
* @return
*/
@GetMapping("get4")
public String get4(Integer uid) {
logger.info("get = >" + uid);
return "get = >" + uid;
}
@PathVariable 路径变量
1、带占位符的 URL 是 Spring3.0 新增的功能,通过 @PathVariable 可以将 URL 中占位符参数绑定到控制器处理方法的入参中。
2、URL 中的 {xxx} 占位符可以通过 @PathVariable("xxx") 绑定到操作方法的入参中。
3、如有 url 地址: http://localhost:8080/find/1/china/2?id=100&name=zhangSan。则 @RequestParam 可以用来获取 "?" 后面的 id、name 参数,而 @PathVariable 则是获取 url 中的参数,如里面的 "1"、"china"、"2"
4、 @PathVariable 有两个常用属性 value 与 required,value 用于指定 url 中的占位符名称,required 表示占位符参数是否必须。
5、required 注意 事项:
如果 required = false,而用户没有传这个参数,那么它会去找这个参数去掉之后的替代 url,如果发现有替代的url,就可以处理这个请求,如果没有找到,则页面报错 404
带占位符的 url 优先级高于其它没有占位符的 url 地址。
如果 required = true,则带占位符的 url 必须匹配,即使还有其它的 url 匹配未传入参数的地址,同样会报错 404
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.logging.Logger;
@RestController
public class WmxController {
/**
* @PathVariable : 其中的 value 属性值对应 url 中的占位符名称,只有 value 时,可以省略 value 不写,甚至直接括弧不写
* <p>
* http://localhost:8080/wmx/movie/1/2/3 :正确
* http://localhost:8080/wmx/movie/1/2/3/:正确
* http://localhost:8080/wmx/movie/1/2/3/4:报错 404
* http://localhost:8080/wmx/movie/1/2/ :报错 404,注意 1/2/ 与 1/2/3 是不同的 url
* http://localhost:8080/wmx/movie/1/2 :报错 404
*/
@GetMapping("/wmx/movie/{type}/{region}/{order}")
public String findMovie1(@PathVariable(value = "type") Integer type,//三个 type 对应
@PathVariable("region") String region,//省略 value
@PathVariable Integer order) {//直接省略括号,最简形式
Logger logger = Logger.getAnonymousLogger();
logger.info("type=" + type + ", region=" + region + ", order=" + order);
return "type=" + type + ", region=" + region + ", order=" + order;
}
/**
* post 请求与 get 请求一样
* http://localhost:8080/wmx/movie2/1/china/2 :正确
* http://localhost:8080/wmx/movie2/1/china/:报错 404,与上面是两个不同的 url
* http://localhost:8080/wmx/movie2/china/2 :报错 404
*
* @return
*/
@PostMapping("/wmx/movie2/{type}/china/{order}")
public String findMovie2(@PathVariable Integer type, @PathVariable Integer order) {
Logger logger = Logger.getAnonymousLogger();
logger.info("type=" + type + ", order=" + order);
return "type=" + type + ", order=" + order;
}
/**
* "/wmx/movie3/{id}"
* 首先明白 :/wmx/movie3/ 与 /wmx/movie3 是相同的 url ,但与 /wmx/movie3/20 是不同的 url
* required 属性:表示 url 中的参数是否可以为 null,默认为 true
* 如下所示 当没有设置 required = false 时,则此方法的路径必须满足 "/wmx/movie3/{id}" 的格式,否则就是 404 错误
* 设置 required = false 后,则 "/wmx/movie3/{id}" 中的参数 id 可以为 null,此时就会去匹配没有 id 时的格式,如 "/wmx/movie3"
* XxxMapping 可以设置多个 url 地址,当然其中的 "/wmx/movie3" 也可以在其它方法中,并不一定要在同一个方法中
*
* @param id
* @return
*/
@GetMapping(value = {"/wmx/movie3", "/wmx/movie3/{id}"})
public String findMovie3(@PathVariable(required = false) Integer id) {
Logger logger = Logger.getAnonymousLogger();
logger.info("id=" + id);
return "id=" + id;
}
}