Restcontroller in Java

Source: Internet
Author: User

Spring4 new features--web development enhancements

Spring4 new features-generic constrained dependency Injection

Spring4 new features-other improvements to the core container

Spring4 new features--web development enhancements

Starting with Spring4, Spring is developed with Servlet3, and if you use the Spring MVC test framework, you need to specify SERVLET3 compatible jar packages (because their mock objects are Servlet3 based). In addition, to facilitate rest development, the new @restcontroller is specified on the controller so that there is no need to add on each @requestmapping method @ResponseBody了。而且添加了一个 AsyncRestTemplate to support asynchronous nonblocking support for rest clients.

1. @RestController

View Copy to Clipboard printing

  1. @RestController

  2. Public class Usercontroller {

  3. Private UserService UserService;

  4. @Autowired

  5. Public Usercontroller (UserService userservice) {

  6. this. UserService = UserService;

  7. }

  8. @RequestMapping ("/test")

  9. Public User view () {

  10. User user = new user ();

  11. User.setid (1L);

  12. User.setname ("haha");

  13. return user;

  14. }

  15. @RequestMapping ("/test2")

  16. Public String View2 () {

  17. return "{\" id\ ": 1}";

  18. }

  19. }

The implementation is to include @responsebody in the @ @RestController:

View Copy to Clipboard printing

    1. @org. Springframework.stereotype.Controller

    2. @org. springframework.web.bind.annotation.ResponseBody

    3. Public @interface Restcontroller {

    4. }

So when you develop the rest server side, the SPRING-MVC configuration file requires very little code, possibly just the following line:

View Copy to Clipboard printing

    1. <context:component-scan base-package ="Com.sishuok.spring4"/>

    2. <mvc:annotation-driven/>

2. Mvc:annotation-driven Configuration Changes

Unified style; Change Enablematrixvariables to Enable-matrix-variables property ; Change Ignoredefaultmodelonredirect to Ignore-default-model-on-redirect.

3, provide asyncresttemplate for client non-blocking asynchronous support.

3.1. Server-side

For server-side SPRINGMVC development, refer to the CHAPTER3-SPRINGMVC in Https://github.com/zhangkaitao/servlet3-showcase

View Copy to Clipboard printing

  1. @RestController

  2. Public class Usercontroller {

  3. Private UserService UserService;

  4. @Autowired

  5. Public Usercontroller (UserService userservice) {

  6. this. UserService = UserService;

  7. }

  8. @RequestMapping ("/api")

  9. Public Callable<user> API () {

  10. System.out.println ("=====hello");

  11. return New Callable<user> () {

  12. @Override

  13. Public User call () throws Exception {

  14. Thread.Sleep (10L * ); //Pause for two seconds

  15. User user = new user ();

  16. User.setid (1L);

  17. User.setname ("haha");

  18. return user;

  19. }

  20. };

  21. }

  22. }

Very simple, the server side pauses for 10 seconds and returns the result (but the server is also non-blocking). Refer to the code on my github for details.

3.2, the Client

View Copy to Clipboard printing

  1. Public Static void Main (string[] args) {

  2. Asyncresttemplate template = new asyncresttemplate ();

  3. //return immediately after call (no blocking)

  4. listenablefuture<responseentity<user>> future = template.getforentity ("http://localhost:9080/ Spring4/api ", User. class);

  5. //Set asynchronous callbacks

  6. Future.addcallback (new listenablefuturecallback<responseentity<user>> () {

  7. @Override

  8. Public void onsuccess (responseentity<user> result) {

  9. System.out.println ("======client get Result:" + result.getbody ());

  10. }

  11. @Override

  12. Public void onfailure (Throwable t) {

  13. System.out.println ("======client failure:" + t);

  14. }

  15. });

  16. System.out.println ("==no Wait");

  17. }

The future is used here to do non-blocking, so we also need to give it a callback interface to get the results; the future and the callable are a pair, a consumption result, a result. When the template is finished, it returns immediately, does not block, and invokes its callback when there is a result.

Asyncresttemplate uses Simpleclienthttprequestfactory by default, which is achieved through java.net.HttpURLConnection, and we can also use Apache's HTTP Use Template.setasyncrequestfactory (new Httpcomponentsasyncclienthttprequestfactory ());


This article is from "Ghost" blog, please make sure to keep this source http://caizi.blog.51cto.com/5234706/1557325

Restcontroller in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.