如何在基于Spring的REST API中将路径参数映射到gRPC对象?

时间:2019-02-08 16:24:23

标签: java spring rest grpc

我的REST API 必须 使用gRPC对象作为输入参数。

最简单的示例是:

GET http://localhost:8083/api/books?page.number=1&page.size=30

原始定义为:

message PagedMessage {
    Page page = 1;
}

message Page {
    int32 number = 1;
    int32 size = 2;
}

控制器为:

@RequestMapping(value = "/api/books")
public class ObjectOps {

   @Autowired
   private BooksService booksService;

   @GetMapping(value = "/")
   @ResponseBody
   BooksList listBooks(@RequestParam PagedMessage request) {
      return booksService.getBooks(request);
   }
}

在应用程序中,我有this bean

@Bean
ProtobufJsonFormatHttpMessageConverter protobufJsonFormatHttpMessageConverter() {
      return new ProtobufJsonFormatHttpMessageConverter();
}

它对我有用的唯一方法是将分页信息作为GET正文传递:

{ 
   "page" : {
      "number": 1,
      "size": 30
   }
}

但是最好从请求路径参数中填充列表方法方法的对象。

1 个答案:

答案 0 :(得分:0)

我认为您只需删除@RequestParam批注,Spring就会填充该对象。 该答案引用了以下内容:https://stackoverflow.com/a/16942352/8075423