1

我有用java(Spring Framework)编写的发票服务,其中有两个控制器-

  1. InvoicePageController - 使用 @Controller 注释和其中的处理程序 generateInvoice 进行注释,该处理程序在服务中返回视图“发票”一个 html 模板
  2. InvoiceController - 使用 @RestController 注释和一个处理程序 getInvoiceUrl 进行注释,该处理程序应该返回上传到 s3 的发票的 url

InvoiceController 将在 Request 中获取一些数据,并且我通过对同一服务 InvoicePageController 类进行 Rest Call 并获取呈现的视图作为响应并存储在字符串变量中来传递给 InvoicePageController 的数据

现在我得到响应的字符串,我正在上传到 S3 并获取它的 URL 并在响应中发送它

问题 - 我正在进行网络调用以获取呈现的发票模板,如果我直接调用 InvoicePageController 的方法,那么我将得到简单的“发票”字符串而不是呈现的视图,所以我想知道的是有一种方法可以通过网络调用来获得渲染视图

下面是类

InvoicePageController.java

@Controller
@RequestMapping(path = "/invoice-page")
public class InvoicePageController {

  @PostMapping
  public String getInvoicePage(@RequestBody InvoiceRequest request, Model model) {

    model.addAttribute("labDetails", request.getLabDetails());
    model.addAttribute("order", request.getOrder());
    model.addAttribute("orderItems", request.getOrderItems());
    return "invoice";
  }
}

发票控制器.java

@RestController
@RequestMapping(path = "/invoice")
public class InvoiceController {
  
  @Autowired RestHelper restHelper;
  @Autowired S3Service s3Service;

  @PostMapping
  public ResponseEntity<String> getInvoiceUrl(@RequestBody InvoiceRequest request) {
    // Call to a helper method with the payload to get the rendered invoice in the string variable
    String invoicePage = restHelper.getInvoicePage(request);
    String invoiceUrl = s3Service.uploadToS3AndGetUrl(invoicePage);
    return new ResponseEntity<>(invoiceUrl, HttpStatus.OK);
  }
}

笔记 -

  1. 我正在使用 thymeleaf 进行视图渲染
  2. 在进行网络调用时,呈现的视图正确地以字符串形式出现

查询 - 有没有办法直接获取渲染视图而不是进行网络调用

4

0 回答 0