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/");
}
}