【问题标题】:Spring MessageConverter HTTP 415 response?Spring MessageConverter HTTP 415响应?
【发布时间】: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


【解决方案1】:

IPNMessage 有一个期望请求的构造函数。所以最简单的解决方案是像这样改变你的方法:

public void processPaypalIPNRequest(HttpServletRequest request) {
  paypalDelegate.processPaypalIPNRequest(new IPNMessage(request));
}

如果您想要自己的消息转换器,那么理想情况下也可以使用new IPNMessage(request)

【讨论】:

  • 谢谢,但我也试过了。但是,因为我的 Controller 方法使用签名 processPaypalIPNRequest(IPNMessage request) 实现了我的接口,如果我尝试使用 HTTPServletRequest 它会破坏接口合同:( 抱歉没有分享这些细节。
  • 谢谢,我想通了。请参考我的回答
【解决方案2】:

问题与 MessageConverter 中的 supports() 方法有关,不评估类签名与 IPNMessage(它正在生成)相同。

@Override
protected boolean supports(Class<?> clazz) {
    return clazz == IPNMessage.class;
}

这导致 Spring 在到达控制器之前抛出更高级别的 HTTP415 错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-16
    • 2018-04-24
    • 2019-03-10
    • 1970-01-01
    相关资源
    最近更新 更多