在 Spring Boot 中将整个 Protobuf 消息添加到模型中
我开始尝试使用 Spring Boot 以了解它如何在 MVC Web 应用程序中发挥作用。
从高层来看,我们的目标是拥有一个控制器,通过向外部 gRPC 服务器发出请求来处理 GET 请求,该服务器将返回 Order protobuf 消息。 Order
数据将添加到 Model
中,并通过带有 Thymeleaf
的模板提供服务。
我对整个 Spring 框架很陌生,所以这种方法可能是不正确的,但我正在做的是:
@Controller
public class OrderController {
@GetMapping("/order")
public String getOrder(@RequestParam(name = "order_number") String orderNumber, Model model) {
// Code for getting Order proto message from external server here
model.addAttribute("name", Order.getDate());
model.addAttribute("total", Order.getTotal());
model.addAttribute("number", Order.getNumber());
....
return "order";
}
}
然而,这看起来相当乏味(特别是对于较大的消息)。我是否缺少一些东西可以让我批量添加这些字段?
I'm starting to play around with Spring Boot to learn how it functions for a MVC web application.
At a high-level the goal is to have a controller that will handle GET
requests by issuing a request to an external gRPC server which will return a Order
protobuf message. The Order
data will be added to the Model
and served via a template with Thymeleaf
.
I am new to the Spring framework as a whole so the approach is likely incorrect but what I was doing is:
@Controller
public class OrderController {
@GetMapping("/order")
public String getOrder(@RequestParam(name = "order_number") String orderNumber, Model model) {
// Code for getting Order proto message from external server here
model.addAttribute("name", Order.getDate());
model.addAttribute("total", Order.getTotal());
model.addAttribute("number", Order.getNumber());
....
return "order";
}
}
However, this seems pretty tedious (especially for larger messages). Is there something I am missing that would allow me to add these fields en-masse?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
抱歉,我无法对问题发表评论(SO限制),因此在这里问 - 你不能将订单对象放入模型(而不是单个字段)中并在视图(Thymeleaf)中访问它的字段/方法吗?
在视图中的控制器中
,我不确定,因为我没有使用过 Thymeleaf,但应该类似于
Sorry, I cannot comment on question (SO restriction), hence asking here - cannot you just put your order object in model (rather than individual fields) and access it's fields/methods in view (Thymeleaf)?
In controller
in view, I am not sure as I have not used Thymeleaf but should be simillar to