SPRING - mosinn/DOCS-n-Snippets-n-Steps GitHub Wiki

Logging info detailed - AUTO-CONFIGURATION-REPORT

  • logging.level.org.springframework = debug
  • Look for "AUTO-CONFIGURATION-REPORT" in logs for ALL things spring has kept ready like request mappers, filters, interceptors,message converters etc

MVC REST quick

  • @RestController
  • @RequestBody
  • @ResponseBody
  • @GetMapping
  • @PostMapping
  • @PathVariable
  • @Service

Jackson write date as Human readable when in bean

  • spring.jackson.serialization.write-dates-as-timestamps=false

POST should return 201 not 200 ie 201 CREATED http status. Also as per rest good practice should return URI of created resource using:

  • inside Controller > URI uri = ServletUriComponentsBuilder.fromCurrentRequest.path("{id}").buildAndExpand(newUser.getId()).toUri()
  • return ResponseEntity.created(uri).build();

MVC - CUSTOM EXCEPTION STRUCTURE, instead of default structure from Spring ? @ResponseStatus on EXCEPTION class NOT ENOUGH?

VALIDATION

  • https://www.udemy.com/course/microservices-with-spring-boot-and-spring-cloud/learn/lecture/8005648#overview
  • Use @Valid near @RequestBody
  • Add @NotNull etc in RequestBody representing Bean, and add messages as well
  • We can change response structure in same way as above, and use ex.getBindingResult to change response details.
  • Here difference is we @Override handleMethodArgumentNotValid()
  • We get details to be set into custom exception pojo, using ex.getBindingResult()
  • Impl lib is hibernate-validator~jar
  • Maven changes not needed as already part of spring-boot-web-starter

HATEOS

  • https://www.udemy.com/course/microservices-with-spring-boot-and-spring-cloud/learn/lecture/8005652#overview
  • ControllerLinkBuilder.* static
  • Instead of normal response, we return wrapped response. Wrapper is [Resource]
  • After building real object from service, we decorate the wrapper code using above static import and inject that there and return.
  • Wrapper + wrapper.add(linkTo.withRel() where linkTo = Resource.linkTo(methodOn(this.getClass()).someLocalMappedMethod())
  • In summary, HATEOS spits out a wrapped initial response, with _links{} add-on, of OTHER MAPPED controller method's POSTMAN URL

AUTHENTICATION and AUTHORISATION

Load custom non application YML properties as ready made objects eg Maps with @PropertyResource and custom bean

@DependsOn , @Primary @Order and control bean dependency initialization order if that needs to be controlled

SPRING JWT method SECURITY

⚠️ **GitHub.com Fallback** ⚠️