Article Directory
SpringMVC comment
①@RequestHeader、@CookieValue
@RequestMapping("/test")
public void test(@RequestHeader("host") String host,@CookieValue("JSESSIONID" String cookie)){}
@RequestHeader
It may be the Request header
value portion bound to the method parameters. @CookieValue
You can put Request header
in on cookie
the value of the binding.
②@RequestBody
The request body mapping entity class, need to specify http header
in content-type
asapplication/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
springboot
The default start banner
below shows
We can modify this banner
information, add a profile, and then specify spring-banner-location
the default value:classpath:banner.txt
Modified:
Exception Handling
A custom exception handling classes, use @RestControllerAdvice
or @ControllerAdvice
using a @ExceptionHandler
catch 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.xml
to add <packaging>war</packaging>
, and then maven install
packaged.
② The war
manner 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);
}
}