无法使用JSON有效负载从另一个调用一个Spring REST控制器

时间:2016-10-20 18:06:29

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

如何从另一个调用一个弹簧REST控制器,通过所需的注射?

ProtectPanController.java(实际休息控制器正在开展工作)

@RestController
public class ProtectPanController {

    private ProtectPanService protectPanService;

    public ProtectPanController(ProtectPanService protectPanService) {
      this.protectPanService = protectPanService;
    }

    @PostMapping(value = "/pan/protect", consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE)
    public ResponseEntity<String> testPostMethod(@RequestBody ProtectPan protectPan) {
        ResponseEntity response = protectPanService.sendProtectPanRequest(protectPan);
        return response;
    }

}

工作(其他网址:http://localhost:9090/hosted-payments-webapp-1.0.0/pan/protect

有效载荷:

{
    "paymentAccountNumber": "4111111111111111",
    "tenderClass": "CreditCard"
}

响应:

{
    "token": "4111110PASeK1111"
}

Pan3dsLookupController.java(此控制器调用控制器上方)

@RestController
public class Pan3dsLookupController {

    @RequestMapping(value = {"/pan/3dslookup"})
    public String doProtectPanAndCmpiLookup(@RequestBody ProtectPanCmpiLookup protectPanCmpiLookup) {
        ProtectPan protectPan = protectPanCmpiLookup.getProtectPan();
        return "forward:/pan/protect";
    }

}

ProtectPanCmpiLookup.java(实际对象的包装)

public class ProtectPanCmpiLookup {

    private ProtectPan protectPan;

    public ProtectPan getProtectPan() {
        return protectPan;
    }

    public void setProtectPan(ProtectPan protectPan) {
        this.protectPan = protectPan;
    }

}

不工作!! (其他网址:http://localhost:9090/hosted-payments-webapp-1.0.0/pan/3dslookup

有效载荷:

{
    "protectPan": {
      "paymentAccountNumber": "4111111111111111",
      "tenderClass": "CreditCard"
    }
}

响应:

forward:/pan/protect

1 个答案:

答案 0 :(得分:0)

The reason why your code :

return "forward:/pan/protect";

works this way and give you that text:

forward:/pan/protect

is that you use RestController and response body of your method is String type:

@RequestMapping(value = {"/pan/3dslookup"})
public String <---

@RestController is a stereotype annotation that combines @ResponseBody and @Controller.

You have to return a ModelAndView object:

return new ModelAndView("forward:/pan/protect", modelName, modelObject)

Check this post for more info