我正在将在Jetty
上的Spring
中运行的遗留单体应用程序现代化并转换为Spring Boot
应用程序。
在遗留代码中,我有一个端点,它看起来像:
@POST
@Produces({MediaType.APPLICATION_JSON})
@Consumes({MediaType.APPLICATION_JSON})
@Path("/zoo/feedCats")
@WebMethod
public Response giveFood( @Context UriInfo uri, HungryCats hungryCats){...}
Spring Boot
中新端点的初稿如下:
@PostMapping("/zoo/feedCats")
public Response giveFood( UriInfo uri , @RequestBody HungryCats hungryCats) {...}
我不确定什么应该是遗留代码中@Context UriInfo uri
的正确替代品。
我在2016年发现了这篇帖子,想知道是否还有其他东西可以使用?
所以我这样解决了它:
创建
class MyUriInfo implements UriInfo{..}
在控制器中,我将签名更改为:
@PostMapping("/zoo/feedCats")
public Response giveFood(@RequestBody HungryCats hungryCats,
HttpServletRequest request,
@RequestParam Map<String, String> allParams)
并创建了一个翻译器方法,它从request
和allParams
中获取我需要的内容,并将它们注入MyUriInfo
:
public UriInfo requestToUriInfo(HttpServletRequest request, Map<String, String>
allParams) {
...
retrun new MyUriInfo(...)
}