假装客户端未收到响应性

发布于 2025-02-13 05:58:04 字数 1559 浏览 2 评论 0原文

我正在学习假装(春季2.7.0,带有云2021.0.3)和我的微服务。服务之间的沟通在起作用,但我无法假装实际上获得响应性 - 只有DTO或列表。

我有一个服务端点,该端点返回响应范围中的列表:

@RestController
@RequestMapping("/api")
@RequiredArgsConstructor

public class ApiController { 

  @GetMapping("/all")
  ResponseEntity getAll() {
        List<ApiKey> apiKeyList = apiService.getAll();
        if ( apiKeyList == null || apiKeyList.size() < 1) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).contentType(MediaType.APPLICATION_JSON)
                            .body(new ErrorResponse("No APIKeys were found "));
        } else {
            return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(apiKeyList);
        }

该列表包含多个:

@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ApiKey {

    @NotNull
    private String apiKey;
    private UUID uuid;

    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
    private LocalDate beginDate;

    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
    private LocalDate endDate;

实际上调用的假装客户端:

@FeignClient(name = "identity-service")
public interface IdentityServiceClient {

    @RequestMapping(method = RequestMethod.GET, value = "/api/all")
    public List<ApiKey> getAllApiKeys();
 // the above works, the below errors out:
    public ResponseEntity getAllApiKeys();

因此,只要它期望列表keyresponse keyresponse,feign客户端就可以正常工作 但是我想在响应范围内拥有它,以便我可以任何潜在的状态代码。

我如何让Feign客户端与响应性合作?

I am learning Feign (Spring 2.7.0 with cloud 2021.0.3) with my micro-services. The communication between services is working but I am unable to have Feign actually receive a ResponseEntity - only the DTO or List.

I have a service endpoint that returns a list in a ResponseEntity:

@RestController
@RequestMapping("/api")
@RequiredArgsConstructor

public class ApiController { 

  @GetMapping("/all")
  ResponseEntity getAll() {
        List<ApiKey> apiKeyList = apiService.getAll();
        if ( apiKeyList == null || apiKeyList.size() < 1) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).contentType(MediaType.APPLICATION_JSON)
                            .body(new ErrorResponse("No APIKeys were found "));
        } else {
            return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(apiKeyList);
        }

The List contains multiple:

@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ApiKey {

    @NotNull
    private String apiKey;
    private UUID uuid;

    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
    private LocalDate beginDate;

    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
    private LocalDate endDate;

The Feign client that actually makes the call is:

@FeignClient(name = "identity-service")
public interface IdentityServiceClient {

    @RequestMapping(method = RequestMethod.GET, value = "/api/all")
    public List<ApiKey> getAllApiKeys();
 // the above works, the below errors out:
    public ResponseEntity getAllApiKeys();

So the Feign client will work, as long as it expects List keyResponse
But I would like to have that in the ResponseEntity so that I can any potential status code.

How do I get feign client to work with ResponseEntity?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一曲爱恨情仇 2025-02-20 05:58:04

如果杰克逊在构建响应范围方面有问题,因为它没有no-args构造函数,请尝试使用响应式decoder

Feign.builder()      
.decoder(new ResponseEntityDecoder(jacksonDecoder))

If jackson has issues with constructing the ResponseEntity because it has no no-args constructor try to use the ResponseEntityDecoder

Feign.builder()      
.decoder(new ResponseEntityDecoder(jacksonDecoder))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文