【发布时间】: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