使用spring-mvc注解:
spring-mvc
POST
form-url-encoded
@FeignClient
n6lpvg4x1#
将FormEncoder用于伪装:
您的伪装配置如下所示:
class CoreFeignConfiguration { @Autowired private ObjectFactory<HttpMessageConverters> messageConverters @Bean @Primary @Scope(SCOPE_PROTOTYPE) Encoder feignFormEncoder() { new FormEncoder(new SpringEncoder(this.messageConverters)) } }
然后,客户端可以像这样Map:
@FeignClient(name = 'client', url = 'localhost:9080', path ='/rest', configuration = CoreFeignConfiguration) interface CoreClient { @RequestMapping(value = '/business', method = POST, consumes = MediaType.APPLICATION_FORM_URLENCODED) @Headers('Content-Type: application/x-www-form-urlencoded') void activate(Map<String, ?> formParams) }
zf2sa74q2#
完整的Java代码和简化版本的kazuar解决方案,可与Sping Boot 配合使用:
import java.util.Map; import feign.codec.Encoder; import feign.form.spring.SpringFormEncoder; import org.springframework.beans.factory.ObjectFactory; import org.springframework.boot.autoconfigure.http.HttpMessageConverters; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.cloud.openfeign.support.SpringEncoder; import org.springframework.context.annotation.Bean; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import static org.springframework.http.MediaType.APPLICATION_FORM_URLENCODED_VALUE; @FeignClient(name = "srv", url = "http://s.com") public interface Client { @PostMapping(value = "/form", consumes = APPLICATION_FORM_URLENCODED_VALUE) void login(@RequestBody Map<String, ?> form); class Configuration { @Bean Encoder feignFormEncoder(ObjectFactory<HttpMessageConverters> converters) { return new SpringFormEncoder(new SpringEncoder(converters)); } } }
从属关系:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
u3r8eeie3#
为了补充accepted answer,还可以使用POJO代替Map<String, ?>,以便将表单参数传递给伪客户机:
Map<String, ?>
@FeignClient(configuration = CustomConfig.class) interface Client { @PostMapping( path = "/some/path", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE) void postComment(CommentFormDto formDto); ... } ... class CustomConfig { @Bean Encoder formEncoder() { return new feign.form.FormEncoder(); } } ... class CommentFormDto { private static String willNotBeSerialized; private final Integer alsoWillNotBeSerialized; @feign.form.FormProperty("author_id") private Long authorId; private String message; @feign.form.FormProperty("ids[]") private List<Long> ids; /* getters and setters omitted for brevity */ }
这将导致请求的主体如下所示:
author_id=42&message=somemessage&ids[]=1&ids[]=2
@FormProperty注解允许设置自定义字段名称;请注意,POJO的静态或最终字段沿着继承的字段将不会序列化为表单内容。
@FormProperty
bwitn5fc4#
对于POST中的URL格式编码数据,必须在Feign编码器中使用FormEncoder。包括应用的依赖项:玛文:
FormEncoder
<dependency> <groupId>io.github.openfeign.form</groupId> <artifactId>feign-form</artifactId> <version>3.8.0</version> </dependency>
将FormEncoder添加到您的Feign.Builder中,如下所示:
SomeFeign sample = Feign.builder() .encoder(new FormEncoder(new JacksonEncoder())) .target(SomeFeign.class, "http://sample.test.org");
在Feign界面中
@RequestLine("POST /submit/form") @Headers("Content-Type: application/x-www-form-urlencoded") void from (@Param("field1") String field1, @Param("field2") String field2);
更多信息请参考:https://github.com/OpenFeign/feign-form
xmq68pz95#
对于Feign.Builder,我的工作没有Jackson编码器,只有Feign FormEncoder:将FormEncoder添加到您的外观生成器:
SomeFeign sample = Feign.builder() .encoder(new FormEncoder()) <==difference here .target(SomeFeign.class, "http://sample.test.org");
我添加到pom.xml中的伪依赖项:
<dependency> <groupId>io.github.openfeign</groupId> <artifactId>feign-core</artifactId> <version>11.8</version> </dependency> <dependency> <groupId>io.github.openfeign</groupId> <artifactId>feign-jackson</artifactId> <version>11.8</version> </dependency> <dependency> <groupId>io.github.openfeign.form</groupId> <artifactId>feign-form</artifactId> <version>3.8.0</version> </dependency>
pom.xml中的父级为:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.6.2</version> <relativePath/> <!-- lookup parent from repository --> </parent>
Ramanan假装接口:
ymdaylpp6#
**在Kotlin进行测试:**以下内容对我有效:
1.创建假装配置:
@Configuration class FeignFormConfiguration { @Bean fun multipartFormEncoder(): Encoder { return SpringFormEncoder(SpringEncoder { HttpMessageConverters( RestTemplate().messageConverters ) }) } }
1.在你的伪装客户中:
@FeignClient( value = "client", url = "localhost:9091", configuration = [FeignFormConfiguration::class] ) interface CoreClient { @RequestMapping( method = [RequestMethod.POST], value = ["/"], consumes = [MediaType.APPLICATION_FORM_URLENCODED_VALUE], produces = ["application/json"] ) fun callback(@RequestBody form: Map<String, *>): AnyDTO? }
1.消耗伪装客户端:
// ----- other code -------- @Autowired private lateinit var coreClient: CoreClient fun methodName() { coreClient.callback(form) } // ----- other code --------
4smxwvx57#
这对我很有效
@FeignClient(name = "${feign.repository.name}", url = "${feign.repository.url}") public interface LoginRepository { @PostMapping(value = "${feign.repository.endpoint}", consumes = APPLICATION_FORM_URLENCODED_VALUE) LoginResponse signIn(@RequestBody Map<String, ?> form); }
格式为Map<String, Object> loginCredentials = new HashMap<>();
Map<String, Object> loginCredentials = new HashMap<>();
7条答案
按热度按时间n6lpvg4x1#
将FormEncoder用于伪装:
您的伪装配置如下所示:
然后,客户端可以像这样Map:
zf2sa74q2#
完整的Java代码和简化版本的kazuar解决方案,可与Sping Boot 配合使用:
从属关系:
u3r8eeie3#
为了补充accepted answer,还可以使用POJO代替
Map<String, ?>
,以便将表单参数传递给伪客户机:这将导致请求的主体如下所示:
@FormProperty
注解允许设置自定义字段名称;请注意,POJO的静态或最终字段沿着继承的字段将不会序列化为表单内容。bwitn5fc4#
对于POST中的URL格式编码数据,必须在Feign编码器中使用
FormEncoder
。包括应用的依赖项:
玛文:
将FormEncoder添加到您的Feign.Builder中,如下所示:
在Feign界面中
更多信息请参考:https://github.com/OpenFeign/feign-form
xmq68pz95#
对于Feign.Builder,我的工作没有Jackson编码器,只有Feign FormEncoder:
将FormEncoder添加到您的外观生成器:
我添加到pom.xml中的伪依赖项:
pom.xml中的父级为:
Ramanan假装接口:
ymdaylpp6#
**在Kotlin进行测试:**以下内容对我有效:
1.创建假装配置:
1.在你的伪装客户中:
1.消耗伪装客户端:
4smxwvx57#
这对我很有效
格式为
Map<String, Object> loginCredentials = new HashMap<>();