Spring Boot开发人员应该知道的技巧有哪些

发布时间:2022-02-28 16:54:52 作者:iii
来源:亿速云 阅读:173

这篇文章主要讲解了“Spring Boot开发人员应该知道的技巧有哪些”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Spring Boot开发人员应该知道的技巧有哪些”吧!

1.使用@ModelAttribute注解

通常有部分的开发人员在请求参数会选择使用 Map<String, String>

@GetMapping

public SomeDto getAll(@RequestParam Map<String, String> params)

虽然它没有任何问题,但它缺少可读性。如果另一个开发人员想知道支持哪些参数,那么开发人员需要仔细阅读代码并且必须费力地找到所有参数。 此外,Swagger 2.0 规范不支持 Map<String, String>

@ModelAttribute注解可用于将请求参数映射到 Java 对象。Java 对象可以具有 API 期望的所有请求参数。这样你就可以在 java 对象上使用所有 javax 验证。

@GetMapping
public SomeDto getAll(@Valid @ModelAttribute SomeObject params)

2.使用Feign Client时,使用@Controlleradvice处理所有未处理的Feignexception

为所有 FeignException 设置一个全局异常处理程序是很好的。大多数情况下,都希望发送与底层服务发送的相同的错误响应。

样品处理
@RequiredArgsConstructor
@ControllerAdvice
public 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. 避免为集成测试而启动 Spring 应用程序上下文

对于集成测试,很多人很可能会使用 @SpringBootTest 注释测试类;此注释的问题在于它将启动整个应用程序上下文。但有时候你也可以避免它,考虑到你只是在测试服务层,而唯一需要的是 JPA 连接。

在这种情况下,可以使用@DataJpaTest注释来启动 JPA 组件和存储库 bean。并使用@Import 注释加载服务类本身。

@DataJpaTest(showSql = false)
@Import(TestService.class)
public class ServiceTest {
    @Autowired
    private TestService service;
    
    @Test
    void testFindAll() {
        List<String> values = service.findAll();
        assertEquals(1, values.size());
    }
}

4. 避免为每个 Spring Profile 创建多个属性文件

如果你希望你的项目中只有一个配置 (application.yml) 文件,那么你可以使用三个破折号分隔每个配置文件配置。

api.info:
  title: rest-serice
  description: some description
  version: 1.0client.url: http://dev.server
---
spring.config.activate.on-profile: integration-test
client.url: http://mock-server
---
spring.config.activate.on-profile: prod
client.url: http://real-server

感谢各位的阅读,以上就是“Spring Boot开发人员应该知道的技巧有哪些”的内容了,经过本文的学习后,相信大家对Spring Boot开发人员应该知道的技巧有哪些这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

推荐阅读:
  1. 为什么说 Java 程序员到了必须掌握 Spring Boot 的时候?
  2. Spring Boot 最流行的 16 条实践解读!

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

springboot

上一篇:input怎么限制输入长度记录

下一篇:怎么使用Pipenv

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》