02 - Difference between spring @Controller and @RestController annotation


Difference between spring @Controller and @RestController annotation


Can @Controller annotation be used for both Web MVC and REST applications?
If yes, how can we differentiate if it is Web MVC or REST application.



Answer:

- @Controller is used to mark classes as Spring MVC Controller.

- @RestController is a convenience annotation that does nothing more than adding the @Controller and @ResponseBody annotations

引用

see: http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/bind/annotation/RestController.html


org.springframework.web.bind.annotation
Annotation Type RestController


@Target(value=TYPE)
@Retention(value=RUNTIME)
@Documented
@Controller
@ResponseBody
public @interface RestController

A convenience annotation that is itself annotated with @Controller and @ResponseBody.
Types that carry this annotation are treated as controllers where @RequestMapping methods assume @ResponseBody semantics by default.


NOTE: @RestController is processed if an appropriate HandlerMapping-HandlerAdapter pair is configured such as the RequestMappingHandlerMapping-RequestMappingHandlerAdapter pair which are the default in the MVC Java config and the MVC namespace. In particular @RestController is not supported with the DefaultAnnotationHandlerMapping-AnnotationMethodHandlerAdapter pair both of which are also deprecated.

Since:
4.0
Author:
Rossen Stoyanchev, Sam Brannen



So the following two controller definitions should do the same:

@Controller
@ResponseBody
public class MyController { }


@RestController
public class MyRestController { }









-------------------------------------------------------------------

Spring 4.0 Restful 系列文章:


01 -Spring Framework: @RestController vs. @Controller
http://lixh1986.iteye.com/blog/2394351

02 - Difference between spring @Controller and @RestController annotation
http://lixh1986.iteye.com/blog/2394354

03 - SpringBoot: Building a RESTful Web Service
http://lixh1986.iteye.com/blog/2394363







- refere to :
difference-between-spring-controller-and-restcontroller-annotation
https://stackoverflow.com/questions/25242321









-

猜你喜欢

转载自lixh1986.iteye.com/blog/2394354
今日推荐