【问题标题】:Consume Realex JSON request -Spring MVC, Spring Boot使用 Realex JSON 请求 -Spring MVC、Spring Boot
【发布时间】:2017-03-16 08:06:29
【问题描述】:

我需要一些帮助来编写控制器以使用来自支付系统 (Realex) 的 JSON 响应。

到目前为止,我从 Realex 文档中获得了以下内容,但无法弄清楚如何编写控制器 - 我是 Spring Boot/Spring MVC 的新手。

RealexHpp realexHpp = new RealexHpp("Shared Secret");
HppResponse hppResponse = realexHpp.responseFromJson(responseJson);
// responseJson will be the Java variable containing the JSON response string
String result = hppResponse.getResult();
String message = hppResponse.getMessage();
String authCode = hppResponse.getAuthCode();

到目前为止,我已经写了:

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.realexpayments.hpp.sdk.RealexHpp;
import com.realexpayments.hpp.sdk.domain.HppResponse;

@Controller
public class PaymentController {

@RequestMapping(value = "/enrolment/confirmation", method = RequestMethod.GET,  consumes = "text/plain")
public String process(@RequestBody String responseJson, Model model) throws Exception {

    RealexHpp realexHpp = new RealexHpp("secret");
    HppResponse hppResponse = realexHpp.responseFromJson(responseJson);
    // responseJson will be the Java variable containing the JSON response string
    String result = hppResponse.getResult();
    String message = hppResponse.getMessage();
    String authCode = hppResponse.getAuthCode();

    System.out.println("Result: "+result);
    System.out.println("Message: "+message);
    System.out.println("AuthCode: "+authCode);

    model.addAttribute("result", result);
    model.addAttribute("message", message);
    model.addAttribute("authcode", authCode);

    return "enrolment/confirmation";

    }
}

【问题讨论】:

  • 您在编写控制器时遇到的具体问题是什么?您在视图端没有收到任何数据吗?是否有错误被抛出?它的行为是否与您预期的不一样?
  • @RequestMapping(value = "/enrolment/process", method = RequestMethod.POST) public String process(@RequestBody String responseJson, Model model) throws Exception { System.out.println("responseJson: "+responseJson); model.addAttribute("消息", responseJson);返回“注册/确认”; }
  • 我已经设法将数据作为字符串接收,但我希望能够将其绑定到“HppResponse hppResponse = realexHpp.responseFromJson(responseJson);”每次我这样做时都会收到一个空错误...我正在考虑通过服务层传递字符串,然后将响应保存在数据库中...似乎无法将其绑定到另一个对象,因为 JSON 标识符在大写... :-) 奇数
  • responseJson 变量包含什么?

标签: java json spring spring-mvc spring-boot


【解决方案1】:

看起来 Spring MVC 在这里不是正确的工具。

见:https://github.com/realexpayments/rxp-hpp-java

期待您使用他们的 SDK 来生成 repsonseJson:

HppRequest hppRequest = new HppRequest()
                    .addAmount(100)
                    .addCurrency("EUR")
                    .addMerchantId("merchantId");

RealexHpp realexHpp = new RealexHpp("mySecret"); 字符串 requestJson = realexHpp.requestToJson(hppRequest);

您可能希望使用 Spring MVC 来捕获请求的参数。

【讨论】:

    猜你喜欢
    • 2019-05-19
    • 2014-05-17
    • 1970-01-01
    • 2020-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-13
    • 2018-05-18
    相关资源
    最近更新 更多