webclient post java_java – Spring WebFlux,如何调试我的WebClient POST交换?

我无法理解在构建WebClient请求时我做错了什么.我想了解实际的HTTP请求是什么样的. (例如,将原始请求转储到控制台)

POST /rest/json/send HTTP/1.1

Host: emailapi.dynect.net

Cache-Control: no-cache

Postman-Token: 93e70432-2566-7627-6e08-e2bcf8d1ffcd

Content-Type: application/x-www-form-urlencoded

apikey=ABC123XYZ&from=example%40example.com&to=customer1%40domain.com&to=customer2%40domain.com&to=customer3%40domain.com&subject=New+Sale+Coming+Friday&bodytext=You+will+love+this+sale.

我正在使用Spring5的反应工具来构建API.我有一个实用工具类,它将使用Dyn的电子邮件api发送电子邮件.我想使用新的WebClient类来完成这个(org.springframework.web.reactive.function.client.WebClient)

curl --request POST "https://emailapi.dynect.net/rest/json/send" --data "apikey=ABC123XYZ&from=example@example.com&to=customer1@domain.com&to=customer2@domain.com&to=customer3@domain.com&subject=New Sale Coming Friday&bodytext=You will love this sale."

当我使用真实值在curl中进行调用时,电子邮件正确发送,因此我觉得我正在生成错误的请求.

我的发送命令

public Mono send( DynEmailOptions options )

{

WebClient webClient = WebClient.create();

HttpHeaders headers = new HttpHeaders();

// this line causes unsupported content type exception :(

// headers.setContentType( MediaType.APPLICATION_FORM_URLENCODED );

Mono result = webClient.post()

.uri( "https://emailapi.dynect.net/rest/json/send" )

.headers( headers )

.accept( MediaType.APPLICATION_JSON )

.body( BodyInserters.fromObject( options ) )

.exchange()

.flatMap( clientResponse -> clientResponse.bodyToMono( String.class ) );

return result;

}

我的DynEmailOptions类

import java.util.Collections;

import java.util.Set;

public class DynEmailOptions

{

public String getApikey()

{

return apiKey_;

}

public Set getTo()

{

return Collections.unmodifiableSet( to_ );

}

public String getFrom()

{

return from_;

}

public String getSubject()

{

return subject_;

}

public String getBodytext()

{

return bodytext_;

}

protected DynEmailOptions(

String apiKey,

Set to,

String from,

String subject,

String bodytext

)

{

apiKey_ = apiKey;

to_ = to;

from_ = from;

subject_ = subject;

bodytext_ = bodytext;

}

private Set to_;

private String from_;

private String subject_;

private String bodytext_;

private String apiKey_;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值