spring传参之@RequestParam注解
@RequestParam注解有三个参数分别是: value、 required、 defaultValue
-
代码:
@RequestMapping(value="test1", method = RequestMethod.GET)
public String reqTest1(@RequestParam("name") String name){
return name;
}
通过@RequestParam注解生命接收用户传入的参数,这样当我们在浏览器输入http://localhost:8080/test1?name=123
-
不设置参数的key
代码如下:
@RequestMapping(value="/test2/{id}", method = RequestMethod.GET)
public Integer reqTest2(@PathVariable("id") Integer id){
return id;
}
静态资源映射
resources资源springboot默认只映射static、 template两个文件夹下的文件
如果我们要在resources下新建一个image资源,想要让spring boot找到它,只需在项目主类中配置一下就好
@SpringBootApplication
public class MyApplication extends WebMvcConfigurerAdapter{
public static void main(String[] args){
SpringApplication.run(MyApplication.class,args);
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry){
super.addResourceHandlers(registry);
//这种方式会在默认的基础上增加,不会影响默认的方式
registry.addResourceHandler("/image/**").addResourceLocations("classpath:/image/");
}
}