英文:
Openfeign How to use @Headers annotation with different produces and consumes types?
问题
我想在我的项目中使用Feign客户端,但某些端点会生成 text/plain
,同时消耗 application/json
。目前我有类似这样的代码。该方法生成 plain/text
,将会消耗 application/json
。有没有办法解决这个问题?请注意,这不是一个Spring Boot应用程序,只是一个Java/Maven项目。
@Headers("Content-Type: text/plain")
@RequestLine(value = "POST /containers/{id}/services")
String startService(@Param("id") String id, String serviceType);
在Jaxrs中,我们使用以下头部来指定此方法或类使用的媒体类型。
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.TEXT_PLAIN)
我希望能够做同样的事情,但在Feign客户端中没有Produces或Consumes头部。只有@Headers,您可以在其中指定内容类型。我想知道这个注解的 Content-Type
是用于消耗和生成的,还是仅用于消耗,或者是否可以为生成和消耗分别指定不同的类型?
英文:
I want to use Feign client in my project but some endpoints produces text/plain
while consuming application/json
. Right now I have something like this. Method is producing plain/text
and will consume application/json
. Is there a way to fix it? Note that this is not a Spring boot application. It's just a Java/Maven project.
@Headers("Content-Type: text/plain")
@RequestLine(value = "POST /containers/{id}/services")
String startService(@Param("id") String id, String serviceType);
In Jaxrs we use below headers to specify the mime types that this method or class uses.
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.TEXT_PLAIN)
I want to be able to do same thing but there is no Produces or consumes headers in feign client. There is only @Headers there you can specify content type. I wonder if this annotation's Content-Type
is used for both consuming and producing, or just for consuming, or if you can specify different types for both producing and consuming?
答案1
得分: 2
@Headers content should look like this:
@Headers({
"Content-Type: application/json",
"Accept: text/plain",
})
With `Accept` header in place, you tell feign client to produce JSON (in your case it is serviceType passed into the method) and consume text/plain.
英文:
@Headers content should look like this:
@Headers({
"Content-Type: application/json",
"Accept: text/plain",
})
With Accept
header in place, you tell feign client to produce JSON
(in your case it is serviceType
passed into the method) and consume text/plain
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论