gpt4 book ai didi

How to read header value from feign Response(如何从伪装响应中读取标头值)

转载 作者:bug小助手 更新时间:2023-10-26 20:39:36 27 4
gpt4 key购买 nike



i am using spring boot to call a openfeign client and from the Response of that feign i need to extract some header values.how can i do that.
can anybody help please.
just help me wheather we can do that or not!

我正在使用Spring Boot调用一个OpenFeign客户端,从该伪装的响应中我需要提取一些头值。我该如何做到这一点。有谁能帮帮忙。帮帮我,不管我们能不能做到这一点!


更多回答
优秀答案推荐

Yes, you can do that. With Feign, we usually declare our interface with the method returning our class and Feign will automatically de-serialise the response from JSON into our POJO.

是的,你可以做到的。使用Feign时,我们通常使用返回类的方法声明接口,Feign会自动将来自JSON的响应反序列化到POJO中。


Here is the interface (operation):

下面是接口(操作):


@FeignClient(name = "library-book-service")
@RequestMapping("books")
public interface BookClient {

@GetMapping
public List<Book> getBooks(
@RequestParam("page") Optional<Integer> pageNum,
@RequestParam("size") Optional<Integer> pageSize,
@RequestParam("reader") Optional<Long> readerId);
}

And then you can use the feign client like this:

然后您可以像这样使用Feign客户端:


@Service
@RequiredArgsConstructor
public class BookService {
private final @NonNull BookClient bookClient;

public List<Book> retrieveBooks(
Optional<Integer> pageNum,
Optional<Integer> pageSize,
Optional<Long> readerId) {

return bookClient.getBooks(pageNum, pageSize, readerId);
}

However, in order to have access to the response headers, you need to declare your methods to return feign.Response.

但是,为了能够访问响应头,您需要声明您的方法以返回feign.Response。


import feign.Response;

@FeignClient(name = "library-book-service")
@RequestMapping("books")
public interface BookClient {

@GetMapping
public Response getBooks(
@RequestParam("page") Optional<Integer> pageNum,
@RequestParam("size") Optional<Integer> pageSize,
@RequestParam("reader") Optional<Long> readerId);
}

This way you can have access to the response body and headers:

这样,您就可以访问响应正文和标头:


@Service
@RequiredArgsConstructor
public class BookService {
private final @NonNull BookClient bookClient;
private final @NonNull ObjectMapper objectMapper;

public List<Book> retrieveBooks(
Optional<Integer> pageNum,
Optional<Integer> pageSize,
Optional<Long> readerId) {

var response = bookClient.getBooks(pageNum, pageSize, readerId);
if (response == null) {
return Collections.emptyList();
}

// retrieve body
var books = objectMapper.readValue(
new BufferedReader(new InputStreamReader(response.body().asInputStream(), StandardCharsets.UTF_8)),
new TypeReference<List<Book>>(){});

// retrieve headers
Map<String, Collection<String>> headers = response.headers();
// ... do whatever you need with the headers

return books;
}


You can use import feign.Response as a response like:

您可以使用导入feign.Response作为响应,如下所示:


@PostMapping("/test")
Response test(@RequestBody TestRequest testRequest);

then you can reach http header

然后您可以访问http标头。


response.headers().get(HEADER_NAME).toString();

if you want get body in this case you have to some json-string manipulation by use response.body() this page may help you for this

在这种情况下,如果您想要获取Body,则必须使用Response.Body()进行一些json字符串操作。此页面可能会为您提供帮助



Presumably you want to retrieve both the (deserialized) response body as well as the response headers. In that case, returning a ResponseEntity is ideal because it includes both.

假设您希望检索(反序列化的)响应正文和响应头。在这种情况下,返回ResponseEntity是理想的,因为它包括两者。


import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;

@FeignClient(name = "HttpStatusClient", url = "https://httpstat.us")
interface HttpStatusClient {
record HttpStatus(int code, String description){}

@GetMapping(path = "/200", produces = APPLICATION_JSON_VALUE)
ResponseEntity<HttpStatus> twoHundred();
}

更多回答

Issue with this is, it does not go through error decoder. It will bypass.

这个问题是,它不通过错误解码器。它会绕过。

I am using plain feign and not spring cloud one. Using Feign builder etc. If I do just Response as return type, it does work but it bypasses errordecoder. Do you know achieve same with plain Feign ?

我用的是素装,不是春云一号。使用Feign builder等。如果我只将Response作为返回类型,它确实有效,但它绕过了errordecoder。你知道用素朴的伪装也能达到同样的效果吗?

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com