我想使用 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=TESTBUYERID01&address_name=John Smith&address_country=United States&address_country_code=US&address_zip=95131&address_state=CA&address_city=San Jose&address_street=123 any street&business=seller@paypalsandbox.com&receiver_email=seller@paypalsandbox.com&receiver_id=seller@paypalsandbox.com&residence_country=US&item_name1=something&item_number1=AK-1234&tax=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=cart&txn_id=297973429¬ify_version=2.1&custom=xyz123&invoice=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>
Controller 端点
@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 的进一步配置?
注意:我在消息转换器类中放置了断点,但它们没有到达,所以这个问题发生在更高的地方,但不确定在哪里。
最佳答案
IPNMessage
有一个需要请求的构造函数。因此,最简单的解决方案是像这样更改您的方法:
public void processPaypalIPNRequest(HttpServletRequest request) {
paypalDelegate.processPaypalIPNRequest(new IPNMessage(request));
}
如果您想要自己的消息转换器,那么理想情况下它也可以执行 new IPNMessage(request)
。
关于java - Spring MessageConverter HTTP 415响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36546354/