我正在使用netflix-feign和jackson来创建Mailgun的包装器。问题是API要求POST请求使用"Content-Type: application/x-www-form-urlencoded"
这是一个示例代码:
@RequestLine("POST /messages")
@Headers("Content-Type: application/x-www-form-urlencoded")
ResponseMessage sendMessage(Message message);JSON对象包含必要的属性,并具有Message注释:@JsonProperty(value = "from") private String from;
问题是要发送的对象是一个JSON对象:
{ "from" : "test@test.mailgun.org", "to" : "atestaccount@gmail.com", "subject" : "A test email", "text" : "Hello this is the text of a test email.", "html" : "<html><body><h1>Hello this is the html of a test email.</h1></body></html>" }
但是,这不是有效的x-www-form-urlencoded内容类型。
有没有办法自动将对象序列化为正确的内容类型?
我认为我可以使用@Body注释,但为了使用它,我必须将不同的属性传递给sendMessage方法。
发布于 2016-10-28 17:40:45
传递映射了xml内容的字符串
您只需在您的方法中发送一个字符串,该字符串之前已被解析为xml:
@RequestLine("POST /messages")
@Headers("Content-Type: application/x-www-form-urlencoded")
ResponseMessage sendMessage(String message);然后使用映射器(例如Jackson),您可以将消息映射到xml:
ObjectMapper xmlMapper = new XmlMapper();
String xml = xmlMapper.writeValueAsString(message);因此,然后使用此xml调用该方法:
sendMessage(xml);使用编码器
另外,我认为可以根据需要配置编码器和解码器。在本例中,要使用XML,可以使用JaxBEncoder和JaxBDecoder
api = Feign.builder()
.encoder(new JAXBEncoder())
.decoder(new JAXBDecoder())
.target(Api.class, "https://apihost");https://stackoverflow.com/questions/39410839
复制相似问题