【发布时间】:2016-08-01 11:26:53
【问题描述】:
我想使用 Spring MVC 的 MessageConverter 来实例化一个名为 IPNMessage 的对象。 SDK link
Paypal IPN 消息采用text-plain 格式,我想将其反序列化为 IPNMessage 对象。
cmd=_notify-validate&payment_type=instant&payment_date=Fri Apr 08 2016 09:40:07 GMT+0100 (GMT Standard Time)&payment_status=Completed&address_status=confirmed&payer_status=verified&first_name=John&last_name=Smith&payer_email=buyer@paypalsandbox.com&payer_id=TESTBUY =美国及address_country_code = US&address_zip = 95131&ADDRESS_STATE = CA&ADDRESS_CITY =圣何塞&address_street = 123的任何街道和商业=卖家@ paypalsandbox.com&RECEIVER_EMAIL =卖家@ paypalsandbox.com&receiver_id =卖家@ paypalsandbox.com&residence_country = US&item_name1 =东西&item_number1 = AK-1234税额= 2.02&mc_currency = USD&mc_fee = 0.44 mc_gross = 12.34&mc_gross_1 = 12.34&mc_handling = 2.06&mc_handling1 = 1.67&mc_shipping = 3.02&mc_shipping1 = 1.02&txn_type =购物&txn_id = 297973429&notify_version = 2.1&定制= XYZ123&发票= ABC1234&的test_ipn = 1&verify_sign = AFcWxV21C7fd0v3bYYYRCpSSRl31A4TqrEbg4g7qEUK.b0lBrPhTpK8o
消息转换类:
public class PaypalIPNHttpMessageConverter extends AbstractHttpMessageConverter<IPNMessage> {
public PaypalIPNHttpMessageConverter() {
super(new MediaType("application", "text-plain"), MediaType.ALL);
}
@Override
protected boolean supports(Class<?> clazz) {
return false;
}
@Override
protected IPNMessage readInternal(Class<? extends IPNMessage> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
//Converts HTTPRequest into map<string,string> that IPNMessage can then parse
String requestString = IOUtils.toString(inputMessage.getBody(), "UTF-8");
Map<String, String[]> requestMap = new LinkedHashMap<>();
for (String keyValue : requestString.split("&")) { //each key value is delimited by &
String[] pairs = keyValue.split("=", 2); // = pairs a key to a value
requestMap.put(pairs[0], pairs[1].split(",")); // , splits multiple values for that key
}
return new IPNMessage(requestMap);
}
@Override
protected void writeInternal(IPNMessage ipnMessage, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
}
}
servlet.xml 中的配置
<bean class="com.kappa.PaymentController">
<property name="delegate" ref="paymentService"/>
<property name="paypalDelegate" ref="paypalIPNService"/>
<property name="messageConverter" ref="paypalIPNHttpMessageConverter"/>
</bean>
控制器端点
@Override
@Auditable
@RequestMapping(value = "/processPaypalIPNRequest.do", method = RequestMethod.POST)
@ResponseStatus(value = HttpStatus.OK)
public void processPaypalIPNRequest(@RequestBody IPNMessage ipnMessage) {
paypalDelegate.processPaypalIPNRequest(ipnMessage);
}
当我发出一个 POST 请求时,我收到一个 HTTP 415,说我的请求正文不受支持。
我是否缺少 Spring 的进一步配置?
注意:我在消息转换器类中放置了断点,但没有到达,所以这个问题发生在更高的位置,但不确定在哪里。
【问题讨论】:
-
您没有为自定义转换器指定支持的媒体类型。通过
setSupportedMediaTypes添加MediaType.APPLICATION_FORM_URLENCODED。 -
这哪里对不起?
-
更改 PaypalIPNHttpMessageConverter 上的超级调用。 PaypalIPN 发送
application/x-www-form-urlencoded请求,而不是text/plain。 -
谢谢,但我仍然收到 HTP 415 错误。我是否还需要在 context.xml 中指定媒体类型?
-
您可能需要注册您创建的自定义转换器。沿着these 行。
标签: java spring spring-mvc paypal