Annotation @RequestMapping, RequestMapping trong Spring MVC

Related Articles

Spring MVC – Phần 3 : Annotation ( 1 ) – Annotation @ RequestMapping, RequestMapping trong Spring MVC

@RequestMapping  là một trong những annoation sử dụng nhiều nhất trong Spring MVC.

Annotation @RequestMapping được sử dụng để map request với class hoặc method xử lý request đó.

@RequestMapping có thể được áp dụng với controller class hoặc method trong controller class.

1.1 @RequestMapping với class:

@Controller
@RequestMapping("/index")
public class BaseController { public String index() { return "index"; }
}

Class BaseController sẽ đóng vai trò như là 1 class Servlet giải quyết và xử lý URL “ / index ”

1.2 @RequestMapping với method:

@Controller
public class HomeController { @RequestMapping("/method0") public String method0() { return "page0"; } @RequestMapping("/method1") public String method1() { return "page1"; }
}

Class HomeController sẽ giải quyết và xử lý 2 URL là “ / method1 ”, “ / method2 ” .

1.3 @RequestMapping với nhiều URI:

@RequestMapping(value = { "/", "/home" })
public String home() { return "home";
}

Method home()Sẽ xử lý cả 2 URL “/” và “/home”

1.4 @RequestMapping với HTTP Method:

@RequestMapping(value="/test", method = RequestMethod.GET)
public String doGet() { return "test1";
}
@RequestMapping(value="/test", method = RequestMethod.POST)
public String doPost() { return "test2";
}

Cùng là 1 URL “/test” nhưng request tới có method là GET sẽ được xử lý bởi method doGet() còn request có method là POST sẽ được xử lý bởi method doPost().

Nếu không chỉ rõ HTTP method thì mặc định controller sẽ giải quyết và xử lý tổng thể những HTTP method .

1.5 @RequestMapping với Header:

@RequestMapping(value = "/method0", headers = "name=kai")
public String method0() { return "page0";
}
@RequestMapping(value = "/method1", headers = { "name=kai", "id=1" })
public String method1() { return "page1";
}

Khi chỉ rõ headers trong @RequestMapping thì nó sẽ chỉ xử lý những request có header chứa các tham số đã chỉ rõ

Ví dụ như method0() sẽ chỉ xử lý request có header chứa cặp key-value là name-kai

1.6 @RequestMapping với Produces và Consumes:

@RequestMapping(value = "/method2", produces = { "application/json", "application/xml" }, consumes = "text/html")
public String method2() { return "page2";
}

consumes : chỉ đồng ý những request có content-type giống với giá trị khai báo bên trong consumes

produces : kiểu tài liệu trả về, cái này thường chỉ dùng với những REST-API .

Thư viện sử dụng

 4.0.0 stackjava.com SpringAnnotation1 0.0.1-SNAPSHOT war  5.0.2.RELEASE    org.springframework spring-webmvc ${spring.version}  

Controller :

package stackjava.com.springmvchello.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/index")
public class BaseController { public String index() { return "index"; }
}
package stackjava.com.springmvchello.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class HomeController { @RequestMapping(value = { "/", "/home" }) public String home() { return "home"; } @RequestMapping(value = "/test", method = RequestMethod.GET) public String doGet() { return "test1"; } @RequestMapping(value = "/test", method = RequestMethod.POST) public String doPost() { return "test2"; } @RequestMapping(value = "/method0", headers = "name=kai") public String method0() { return "page0"; } @RequestMapping(value = "/method1", headers = { "name=kai", "id=1" }) public String method1() { return "page1"; } @RequestMapping(value = "/method2", produces = { "application/json"}, consumes = "text/html") public String method2() { return "page2"; }
}

Các file jsp :

Spring MVC Annotation 

home.jsp

/home

/test (GET)

Spring MVC Annotation 

index.jsp

Spring MVC Annotation 

test1.jsp

Spring MVC Annotation 

test2.jsp

Spring MVC Annotation 

page0.jsp

Spring MVC Annotation 

page1.jsp

Spring MVC Annotation 

page2.jsp

Demo:

Spring MVC - Phần 3: Annotation(1) - Annotation @RequestMapping, RequestMapping trong Spring MVC

URL / test với method GET

spring annotation @requestmapping

URL / test với method POST

Annotation @Requestmapping

Các ví dụ với headers, consumes phải thêm giá trị vào header khi gửi nên mình sẽ dùng postman để demo :

Ví dụ với consumes = ” text / html ”

spring mvc consumes

Ví dụ với headers = ” name = kai ”

spring mvc proceduces

Okay, Done!

Download code ví dụ trên tại đây

References :

Spring MVC @RequestMapping Annotation Example with Controller, Methods, Headers, Params, @RequestParam, @PathVariable

More on this topic

Comments

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Advertismentspot_img

Popular stories