我现在正在使用 Spring 4,并且我有一个定义如下的 Controller :
@RequestMapping(value = "/{id}/definitions", method = RequestMethod.PUT)
@ResponseBody
public List<ResponseDTO> update(@PathVariable(value = "id") String id,
HttpServletRequest iRequest) throws Exception
{ ... }
我期望收到一个加密的“id”字符串,该字符串作为请求本身的路径变量的一部分。但我需要做的是重写这个请求 URI 并将其解密为另一个值(例如整数),并仅使用原始 URI 和转换/解密的值形成另一个 HTTP 请求。
如何获取整个 URI 并用整数替换 {id}?
例如,如果传入的原始请求如下所示:
http://mycompany.com/my-service/kjAISOhalkjZjakmbbb/definitions
我想转换上下文路径之后的所有内容:
/kjAISOhalkjZjakmbbb/definitions
至:
/123456/definitions
最后,我可以向另一个服务发出一个请求,如下所示:
http://mycompany.com/my-service-2/123456/definitions
谢谢!
最佳答案
解密该值(无论您如何执行)。 然后只需使用重定向或转发(在您的情况下可能是转发):
@RequestMapping(value = "/{id}/definitions", method = RequestMethod.PUT)
public void update(@PathVariable(value = "id") String id,
HttpServletRequest iRequest) throws Exception {
String decryptedId = //decrypt here
//Do whatever else
//either forward: or redirect:
return "forward:my-service-2/" + decryptedId + "/definitions";
}
关于java - 如何在 Spring 中重写请求 URI 和路径变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36074508/