猿问

使用对象作为参数从另一个控制器调用控制器并检索响应

我有两个休息控制器。两者都是后映射端点。


在第一个控制器 (Controller1) 使我需要的所有东西都成为一个对象之后,我想调用/重定向第二个控制器以便继续,然后从它那里得到响应。


@RestController

public class Controller1{


    @PostMapping("/endpoint1")

    public ReponseEntity<?> doWhatController1HasToDo(@RequestBody Object request){

       //some processing


       //here i would like to call second controller

    }

}

@RestController

public class Controller2{


    @PostMapping("/endpoint2")

    public ReponseEntity<?> doWhatController2HasToDo(@RequestBody Object request){

       //some processing


       return new ResponseEntity<>(JSON, HttpStatus.OK);

    }

}


我尝试过使用 RestTemplate,但总是出现 405 错误。我在某处读过,这是因为多部分


private ResponseEntity forwardUsingRestTemplate(HttpServletRequest httpServletRequest, Object object) throws MalformedURLException {

        HttpHeaders headers = new HttpHeaders();

        headers.setContentType(MediaType.APPLICATION_JSON);

        HttpEntity req = new HttpEntity(object, headers);

        RestTemplate template = new RestTemplate();

        ResponseEntity<TdmResponse> response = template.exchange(getBaseUrl(httpServletRequest) + "/endpoint2", HttpMethod.POST, req, TdmResponse.class);

}


问题是,我如何调用第二个端点?


隔江千里
浏览 131回答 3
3回答

慕慕森

为什么需要调用另一个端点?这些控制器是否位于单独的应用程序中?如果不是,那么在这两个控制器之上使用服务会更有效:public class Service {&nbsp;public Object processController1(Object object) {&nbsp;//some processing&nbsp;return processController2(result of some processing);&nbsp;}&nbsp;public Object processController2(Object object) {&nbsp;// processing&nbsp;}}然后在你的控制器中使用这两种方法:public class Controller1{&nbsp; &nbsp; private Service service;&nbsp; &nbsp; public ReponseEntity<?> doWhatController1HasToDo(@RequestBody Object request){&nbsp; &nbsp; &nbsp; &nbsp;return new ResponseEntity<>(service.processController1(request), OK);&nbsp; &nbsp; }}public class Controller2{&nbsp; &nbsp; private Service service;&nbsp; &nbsp; public ReponseEntity<?> doWhatController1HasToDo(@RequestBody Object request){&nbsp; &nbsp; &nbsp; &nbsp;return new ResponseEntity<>(service.processController2(resultProcess1), OK);&nbsp; &nbsp; }}如果有 2 个不同的应用程序,那么问题可能出在您的 CSRF 设置上。如果您在第二个应用程序中启用了 CSRF,那么它将拒绝您的呼叫,因为您没有使用 RestTemplate 传递 CSRF 令牌。稍后编辑:您可以使用外观模式在控制器和服务之间添加另一层抽象:public class Facade{&nbsp; &nbsp; private Service1 service1;&nbsp; &nbsp; private Service2 service2;&nbsp; &nbsp; public ReponseEntity<?> doWhatController1HasToDo(@RequestBody Object request){&nbsp; &nbsp; &nbsp; &nbsp;Object resultService1 = service1.process(request);&nbsp; &nbsp; &nbsp; &nbsp;Object resultService2 = service2.process(resultService1);&nbsp; &nbsp; &nbsp; &nbsp;return new ResponseEntity<>(resultService2, OK);&nbsp; &nbsp; }&nbsp; &nbsp; public ReponseEntity<?> doWhatController2HasToDo(@RequestBody Object request){&nbsp; &nbsp; &nbsp; &nbsp;Object resultService2 = service2.process(request);&nbsp; &nbsp; &nbsp; &nbsp;return new ResponseEntity<>(resultService2, OK);&nbsp; &nbsp; }}

Cats萌萌

您还可以将第二个控制器注入第一个控制器中,然后直接调用该方法。如果您不想动态更改端点,则此方法有效。

浮云间

您的控制器使用@RestController进行注释,这意味着从控制器方法返回的任何内容都将以 json 或 xml 的形式解释。在您的情况下,如果您从Controller1 的doWhatController1HasToDo返回任何内容,它会将其处理为 json 或 xml。您应该通过以下方式做到这一点。它可能对你有帮助。@Controllerpublic class Controller1{@PostMapping("/endpoint1")public String doWhatController1HasToDo(@RequestBody Object request){&nbsp; &nbsp;//some processing&nbsp; &nbsp;return "redirect:/doWhatController2HasToDo";} }我假设,您的两个控制器都在同一个文件夹中(即 Controller1 和 Controller2)。这将调用 Controller2 的方法doWhatController2HasToDo(@RequestBody Object request),您可以对该方法执行任何操作,例如@RestControllerpublic class Controller2{@PostMapping("/endpoint2")public ReponseEntity<?> doWhatController2HasToDo(@RequestBody Object request){&nbsp; &nbsp;//some processing&nbsp; &nbsp;return new ResponseEntity<>(JSON, HttpStatus.OK);}}
随时随地看视频慕课网APP

相关分类

Java
我要回答