SpringBoot2.x basis

SpringMVC comment

①@RequestHeader、@CookieValue

RequestHeader

@RequestMapping("/test")
public void test(@RequestHeader("host") String host,@CookieValue("JSESSIONID" String cookie)){}

@RequestHeaderIt may be the Request headervalue portion bound to the method parameters. @CookieValueYou can put Request headerin on cookiethe value of the binding.

②@RequestBody

The request body mapping entity class, need to specify http headerin content-typeasapplication/json charset=utf-8

③restful request

scenes to be used
@GetMapping Inquire
@PostMapping New
@PutMappiing modify
@DeleteMapping delete

④SpringBoot annotation mapping configuration file

Scan profiles Note:@PropertySource({"classpath:resource.properties"})

Mapping attributes:@Value("${test.name}")

json framework common comment

The specified field does not return:@JsonIgnore

Specified date format:@JsonFormat(pattern="yyyy-MM-dd hh:mm:ss",locale="zh",timezone="GMT+8")

Empty fields are not returned:@JsonInclude(Include.NON_NUll)

Alias:@JsonProperty

SpringBoot annotation mapping configuration file

Scan profiles Note:@PropertySource({"classpath:resource.properties"})

Mapping attributes:@Value("${test.name}")

Personalized banner

springbootThe default start bannerbelow shows
The default banner

We can modify this bannerinformation, add a profile, and then specify spring-banner-locationthe default value:classpath:banner.txt

FIG position banner

Modified:
Personalized banner

Exception Handling

A custom exception handling classes, use @RestControllerAdviceor @ControllerAdviceusing a @ExceptionHandlercatch exceptions, exception handling gives it two ways, one is to return json data, and the second is to return an error page.

@RestControllerAdvice
public class CustomExtController {

    //捕获全局异常,返回json数据
    @ExceptionHandler(value = Exception.class)
    Object handleException(Exception e, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 100);
        map.put("msg", e.getMessage());
        map.put("url", request.getRequestURL());
        return map;
    }

    //捕获特定异常,返回错误页面
    @ExceptionHandler(value = MyException.class)
    Object handleMyException(Exception e) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("error.html");
        modelAndView.addObject("msg",e.getMessage());
        return modelAndView;
    }
}

SpringBoot project packaged into war package

① In pom.xmlto add <packaging>war</packaging>, and then maven installpackaged.

② The warmanner start packet, the master boot class needs to be changed, as follows:

@SpringBootApplication
public class SpringbootApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(SpringbootApplication.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(SpringbootApplication.class, args);
    }

}

Published 81 original articles · won praise 124 · views 380 000 +

Guess you like

Origin blog.csdn.net/qq_38697437/article/details/104169574