New Spring4 Feature - Enhancement of Web Development

New Spring4 Feature - Generic Qualified Dependent Injection

New Spring4 feature - other improvements to core containers

New Spring4 Feature - Enhancement of Web Development

New Spring4 feature - Integrating Bean Validation 1.1(JSR-349) to SpringMVC 

New Spring4 Feature - Groovy Bean Definition DSL

New Spring4 Feature - Better Java Generic Operating API 

New Spring4 Feature - Support for JSR310 Date API

New Spring4 features - annotations, scripts, tasks, MVC and other feature improvements 

 

Starting with Spring 4, Spring was developed with Servlet3, and if you are using the Spring MVC test framework, you need to specify a Servlet3-compatible jar package (because its Mock objects are Servlet3-based).Additionally, to facilitate Rest development, you specify on the controller through a new @RestController, which eliminates the need to add @ResponseBody to each @RequestMapping method.An AsyncRestTemplate was added to support asynchronous, non-blocking support for REST clients.

 

1,@RestController

@RestController
public class UserController {
    private UserService userService;
    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }
    @RequestMapping("/test")
      public User view() {
        User user = new User();
        user.setId(1L);
        user.setName("haha");
        return user;
    }

    @RequestMapping("/test2")
    public String view2() {
        return "{\"id\" : 1}";
    }
}

This is accomplished by adding @ResponseBody to @@RestController:

@org.springframework.stereotype.Controller
@org.springframework.web.bind.annotation.ResponseBody
public @interface RestController {
}

This way, when you develop the Rest server side, the spring-mvc configuration file requires very little code, perhaps just the following line:

<context:component-scan base-package="com.sishuok.spring4"/>
    <mvc:annotation-driven/>

  

2. mvc:annotation-driven configuration change

Unified style; change enableMatrixVariables to enable-matrix-variables attribute; change ignoreDefaultModelOnRedirect to ignore-default-model-on-redirect.

 

3. Provide AsyncRestTemplate for client non-blocking asynchronous support.

3.1, Server-side

Reference for server-side springmvc development https://github.com/zhangkaitao/servlet3-showcase In chapter3-springmvc

@RestController
public class UserController {
    private UserService userService;
    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }
    @RequestMapping("/api")
      public Callable<User> api() {
        System.out.println("=====hello");
        return new Callable<User>() {
            @Override
            public User call() throws Exception {
                Thread.sleep(10L * 1000); //Pause for two seconds
                User user = new User();
                user.setId(1L);
                user.setName("haha");
                return user;
            }
        };
    }
}

Very simple, the server pauses for 10 seconds before returning results (but the server is also non-blocking).Refer specifically to the code on my github.

 

3.2, Client

public static void main(String[] args) {
        AsyncRestTemplate template = new AsyncRestTemplate();
        //Return immediately after call (no blocking)
        ListenableFuture<ResponseEntity<User>> future = template.getForEntity("http://localhost:9080/spring4/api", User.class);
        //Set Asynchronous Callback
        future.addCallback(new ListenableFutureCallback<ResponseEntity<User>>() {
            @Override
            public void onSuccess(ResponseEntity<User> result) {
                System.out.println("======client get result : " + result.getBody());
            }

            @Override
            public void onFailure(Throwable t) {
                System.out.println("======client failure : " + t);
            }
        });
        System.out.println("==no wait");
    }

Future is used here to complete non-blocking, so we also need to give it a callback interface to get results; Future and allable are pairs, one consumer result, one producer.Templates are returned immediately after they are invoked, without blocking them; callbacks are invoked when there are results.

 

AsyncRestTemplate defaults to SimpleClientHttpRequestFactory, which is implemented through java.net.HttpURLConnection; we can also use apache's http components; use template.setAsyncRequestFactory (new HttpComponents AsyncClientHttpRequestFactory ()); and set it.

 

Also try not to register yourself during development, such as:

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">

Use as much as possible

<mvc:annotation-driven/>

It is designed well enough to configure the configuration we need with child elements.

  

And don't use older versions:

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">

Otherwise, you may get the following exception:

Write
Circular view path [login]: would dispatch back to the current handler URL [/spring4/login] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)

  

Reprinted at: https://my.oschina.net/qjx1208/blog/200938

Keywords: Spring REST Java github

Added by noober on Thu, 13 Jun 2019 21:47:03 +0300