Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What skills should Spring Boot developers know?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "what skills Spring Boot developers should know". The explanation in this article is simple and clear and easy to learn and understand. Please follow the editor's train of thought to study and learn what skills Spring Boot developers should know.

1. Use @ ModelAttribute annotations

Usually, some developers choose to use Map when requesting parameters.

@ GetMappingpublic SomeDto getAll (@ RequestParam Map params)

Although there is nothing wrong with it, it lacks readability. If another developer wants to know which parameters are supported, the developer needs to read the code carefully and must struggle to find all the parameters. In addition, the Swagger 2.0 specification does not support Map.

The @ ModelAttribute annotation can be used to map request parameters to Java objects. The Java object can have all the request parameters expected by API. This allows you to use all javax validation on java objects.

@ GetMappingpublic SomeDto getAll (@ Valid @ ModelAttribute SomeObject params) 2. When using Feign Client, use @ Controlleradvice to handle all outstanding Feignexception

It is good to set up a global exception handler for all FeignException. In most cases, you want to send the same error response as the underlying service.

Sample treatment @ RequiredArgsConstructor@ControllerAdvicepublic class ExceptionControllerAdvice {private final ObjectMapper mapper;@ExceptionHandler (FeignException.class) public final ResponseEntity

< String >

HandleFeignException (FeignException fex) {log.error ("Exception from downstream service call -", fex); HttpStatus status = Optional.ofNullable (HttpStatus.resolve (fex.status () .orElse (HttpStatus.INTERNAL_SERVER_ERROR); String body = Strings.isNullOrEmpty (fex.contentUTF8 ())? Fex.getMessage (): fex.contentUTF (); return new ResponseEntity

< >

(body, getHeadersWithContentType (body), status);} private MultiValueMap

< String, String >

GetHeadersWithContentType (String body) {HttpHeaders headers = new HttpHeaders (); String contentType = isValidJSON (body)? "application/json": "text/plain"; headers.add (HttpHeaders.CONTENT_TYPE, contentType); return headers;} private boolean isValidJSON (String body) {try {if (Strings.isNullOrEmpty (body)) return false; mapper.readTree (body); return true;} catch (JacksonException e) {return false;}} 3. Avoid launching Spring application contexts for integration testing

For integration testing, many people will most likely test the class with the @ SpringBootTest annotation; the problem with this annotation is that it starts the entire application context. But sometimes you can avoid it, considering that you are only testing the service layer, and the only thing you need is a JPA connection.

In this case, you can use the @ DataJpaTest annotation to start the JPA component and repository bean. And load the service class itself with the @ Import annotation.

@ DataJpaTest (showSql = false) @ Import (TestService.class) public class ServiceTest {@ Autowired private TestService service; @ Test void testFindAll () {List values = service.findAll (); assertEquals (1, values.size ();}} 4. Avoid creating multiple property files for each Spring Profile

If you want to have only one application.yml file in your project, you can separate each profile configuration with three dashes.

Api.info: title: rest-serice description: some description version: 1.0client.url: http://dev.server---spring.config.activate.on-profile: integration-testclient.url: http://mock-server---spring.config.activate.on-profile: prodclient.url: http://real-server Thank you for your reading. These are the contents of "what skills Spring Boot developers should know". After studying this article, I believe that you have a deeper understanding of the skills that Spring Boot developers should know, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report