WEB application development using the Spring Boot

Spring Boot suited to develop WEB applications, because it offers a lot of convenience, such as support for embedded HTTP server; providing a starter to manage dependencies, fast start, for example, spring-boot-starter-web start web application development, the use of spring -boot-starter-webflux started responsive web development.

Spring Web MVC Framework

In SpringMVC frame, a request processor @Controller or @RestController label, flag @RequestMapping request processing method using, for example:

@RestController
@RequestMapping(value="/users")
public class MyRestController {

    @RequestMapping(value="/{user}", method=RequestMethod.GET)
    public User getUser(@PathVariable Long user) {
        // ...
    }

    @RequestMapping(value="/{user}/customers", method=RequestMethod.GET)
    List<Customer> getUserCustomers(@PathVariable Long user) {
        // ...
    }

    @RequestMapping(value="/{user}", method=RequestMethod.DELETE)
    public User deleteUser(@PathVariable Long user) {
        // ...
    }

}

Spring MVC automatic configuration

Autoconfiguration Spring Boot is quite handy when developing Spring MVC WEB application.

By default, Spring Boot provides automatic configuration stuff:

  • 和 ContentNegotiatingViewResolver BeanNameViewResolver
  • Support static resources
  • Converter, GenericConverter, and Formatter
  • HttpMessageConverters
  • MessageCodesResolver
  • Static index.html
  • Support Custom Favicon
  • ConfigurableWebBindingInitializer

If the above stuff does not satisfy you, you can also:

  • If you want to automatically configure the basis of the above, the expansion of other functions, such as interceptors, formatters, view controllers, etc., you can mark a WebMvcConfigurer @Configuration to expand, but be careful not to label @EnableWebMvc.
  • If you want to automatically configure the basis of the above, to achieve RequestMappingHandlerMapping, RequestMappingHandlerAdapter, or ExceptionHandlerExceptionResolver, you can mark a WebMvcRegistrationsAdapter be achieved through @Configuration, but be careful not to label @EnableWebMvc.
  • If you want complete control, then you need to mark the front @EnableWebMVC configuration class.

HttpMessageConverters

HttpMessageConverters used to convert a response message, for example, the method returns Object Request directly converted into JSON or XML.

If you need a custom converter, you can do this:

import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.*;
import org.springframework.http.converter.*;

@Configuration(proxyBeanMethods = false)
public class MyConfiguration {

    @Bean
    public HttpMessageConverters customConverters() {
        HttpMessageConverter<?> additional = ...
        HttpMessageConverter<?> another = ...
        return new HttpMessageConverters(additional, another);
    }

}  

Custom JSON serializers and deserializers

If you are to use Jackson JSON serialization and de-serialization of data, you may want to customize JsonSerializer and JsonDeserializer, Spring Boot provides a @JsonComponent annotation can make a custom easier.

@JsonComponent can be used directly on JsonSerializer, JsonDeserializer KeyDeserializer implementation class and can be used on the exterior of categories, for example:

import java.io.*;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
import org.springframework.boot.jackson.*;

@JsonComponent
public class Example {

    public static class Serializer extends JsonSerializer<SomeObject> {
        // ...
    }

    public static class Deserializer extends JsonDeserializer<SomeObject> {
        // ...
    }

}

MessageCodesResolver

MessageCodesResover used to generate a binding error error code.

Static content  

By default, a static resource in / static, / public, / under the resources directory, Spring MVC using ResoucesHttpRequestHandler to deal with static resources, you can override WebMvcConfigurer of addResourceHandlers customize ResoucesHttpRequestHandler.

 

By default, static resources are mapped to / **, you can override by spring.mvc.static-path-pattern attributes, example:

spring.mvc.static-path-pattern=/resources/**

 

The same can customize the location of the static resource, by example spring.resources.static-locations:

spring.resources.static-locations=/icon/  

welcome page

The default welcome page is index.html, if index.html is not present, then the index to find the template file

Custom favicon

favicon.icon file directly under static resource directory.

Path matching and content negotiation

  

Guess you like

Origin www.cnblogs.com/stronger-brother/p/12120781.html