spring常用註解

參數綁定註解

@ModelAttribute

傳輸格式必須爲x-www-form-urlencoded,後臺纔會自動注入實體類
在這裏插入圖片描述

@RequestBody

傳輸格式爲json,會自動根據json的格式去匹配接收的實體.一般用來處理非Content-Type: application/x-www-form-urlencoded編碼格式的數據(當然x-www-form-urlencoded也是可以處理的),不能處理二進制流的形式。比如說from表單默認提交方式(multipart/form-data

@PathVariable

獲取URL中的參數

@Controller 
@RequestMapping("/owners/{ownerId}") 
public class RelativePathUriTemplateController { 
  //獲取ownerId和petId 
  @RequestMapping("/pets/{petId}") 
  public void findPet(@PathVariable String ownerId,@PathVariable String petId, Model model) {     
 
  } 
} 

@RequestParam

  • 常用來處理簡單類型的綁定,通過Request.getParameter() 獲取的String可直接轉換爲簡單類型的情況( String–> 簡單類型的轉換操作由ConversionService配置的轉換器來完成);因爲使用request.getParameter()方式獲取參數,所以可以處理get 方式中queryString的值,也可以處理post方式中 body data的值;

  • 用來處理Content-Type: 爲 application/x-www-form-urlencoded編碼的內容,提交方式GET、POST;

  • 該註解有兩個屬性: value、required; value用來指定要傳入值的id名稱,required用來指示參數是否必須綁定;

URL:xxx/get?username=xxx
請求方式:get

@Controller 
public class RelativePathUriTemplateController { 
  @RequestMapping("/get") 
  public void findPet(@RequestParam("username")string username, Model model) {     
   //xxx
  } 
} 

當前臺界面使用GET或POST方式提交數據時,數據編碼格式由請求頭的ContentType指定。分爲以下幾種情況:

  1. application/x-www-form-urlencoded,這種情況的數據@RequestParam、@ModelAttribute可以處理,@RequestBody也可以處理。
  2. multipart/form-data,@RequestBody不能處理這種格式的數據。(form表單裏面有文件上傳時,必須要指定enctype屬性值爲multipart/form-data,意思是以二進制流的形式傳輸文件。)
  3. application/json、application/xml等格式的數據,必須使用@RequestBody來處理。

Spring常用註解

  • @SpringBootApplication,springboot的核心註解,用於開啓自動配置,等效於@Configuraion、@ComponentScan和@EnableAutoConfiguration三個註解一起使用。
  • @RestController,表示是servlet控制器,是@Controller和ResponseBody的註解合集,支持restful風格的寫法,請求的返回的數據格式爲json,適合專門做後臺服務接口開發,在4.0版本前不支持該註解。
  • @RequestMapping:提供路由信息,負責URL到Controller中的具體函數的映射。
  • @ResponseBody: 將請求返回的結果爲json形式。
  • @Controller:用於定義控制器類。
  • @Service: 表示Service層。
  • @Repository: 表示數據關係映射層。
  • @ComponentScan: 將該類自動發現掃描組件。
  • @Bean: 用在方法上來生成一個bean示例,交給Spring容器託管。
  • @Component: 表示組件。
  • @Configuration: 相當於傳統的xml配置文件,如果有些第三方庫需要用到xml文件,建議仍然通過@Configuration類作爲項目的配置主類——可以使用@ImportResource註解加載xml配置文件。
  • @EnableAutoConfiguration: Spring Boot自動配置(auto-configuration):嘗試根據你添加的jar依賴自動配置你的Spring應用。
  • @ConfigurationProperties: 將配置文件的內容封裝成實體類對應的屬性。可以把@ConfigurationProperties直接定義在@Bean的註解上,此時bean實體類就不用@Component和@ConfigurationProperties註解了。
  • @Profiles: 提供了一種隔離應用程序配置的方式,並讓這些配置只能在特定的環境下生效。
  • @Autowired: 自動導入依賴的bean。
  • @Qualifier: 當有多個同一類型的Bean時,可以用@Qualifier(“name”)來指定。與@Autowired配合使用。@Qualifier限定描述符除了能根據名字進行注入。
  • @Resource: 默認byName,與@Autowired幹類似的事,找不到再根據類型找,使用@Resource(name=”name”,type=”type”)
  • @Import: 用來導入其他配置類。
  • @ImportResource:用來加載xml配置文件。
  • @RequestBody: 處理post、put等請求時數據解析。
  • @RequestParam: 獲取請求參數,用在方法的參數前。
  • @PathVariable: 路徑變量。
  • @Value: 獲取配置application文件的內容。
  • @ControllerAdvice:包含@Component。可以被掃描到。統一處理異常。
  • @ExceptionHandle(Exception.class):用在方法上面表示遇到這個異常就執行以下方法。
  • @ComponentScan:我們經常使用 @ComponentScan 註解搜索beans,並結合 @Autowired 構造器注入。
  • @Profiles :Spring Profiles提供了一種隔離應用程序配置的方式,並讓這些配置只能在特定的環境下生效。任何@Component或@Configuration都能被@Profile標記,從而限制加載它的時機。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章