英文:
Accessing path variables and request parameters without annotation
问题
我有很多如下所示的REST控制器:
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/v1/my-endpoint")
public class MyRestController {
@Override
@GetMapping(value = "/{type}/{code}", produces = { "application/json" })
public ResponseEntity<List<MyDTO>> query(
@PathVariable(value = "type") final String type,
@PathVariable(value = "code") final Integer code,
@RequestParam(value = "key", required = false) final String key) {
// ...
}
}
但是,我如何在不使用注释的情况下访问query方法内的路径变量和请求参数?此外,我需要获取端点的路径。就像Servlet中可以做的那样。类似于以下内容(我知道下面的代码是不起作用的):
String type = request.getPathVariable("type");
String key = request.getParam("key");
String endpoint = request.getPath(); // endpoint将保存“my-endpoint”
我在Spring世界中还很新,所以我想这个问题非常基础。但是,我会非常感谢任何帮助。
英文:
I have many rest controllers as following:
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/v1/my-endpoint")
public class MyRestController {
@Override
@GetMapping(value = "/{type}/{code}", produces = { "application/json" })
public ResponseEntity<List<MyDTO>> query(
@PathVariable(value = "type") final String type,
@PathVariable(value = "code") final Integer code,
@RequestParam(value = "key", required = false) final String key) {
// ...
}
}
But, how can I have access to both path variables and request parameters inside the query method without using annotations? Moreover, I need to get the path of the endpoint. It'd be like what can be done with Servlets. Something like that (I know that the code below doesn't work):
String type = request.getPathVariable("type");
String key = request.getParam("key");
String endpoint = request.getPath(); // endpoint will hold "my-endpoint"
I'm new in the Spring world, so I guess the question is very basic. But, I'd appreciate any help.
答案1
得分: 2
// 将控制器更新为以下内容:
@RestController
@RequestMapping("/v1/my-endpoint")
public class MyRestController {
@GetMapping(value = "/{type}/{code}", produces = { "application/json" })
public ResponseEntity<List<MyDTO>> query(HttpServletRequest request, HttpServletResponse response) {
String endpoint = request.getRequestURI();
String[] data = endpoint.split("/");
Integer code = Integer.valueOf(data[data.length - 1]);
String type = data[data.length - 2];
String key = request.getQueryString().split("=")[1];
System.out.println(code + "\n" + key + "\n" + type + "\n" + endpoint);
// .....
}
}
// 示例请求:http://localhost:8080/v1/my-endpoint/def/2?key=abc
// 控制台输出:
2
abc
def
/v1/my-endpoint/def/2
// 添加了通过 Spring 的另一种方式:
String endpoint = ServletUriComponentsBuilder.fromCurrentRequestUri().toUriString();
String[] data = endpoint.split("/");
Integer code = Integer.valueOf(data[data.length - 1]);
String type = data[data.length - 2];
System.out.println(code + "\n" + type + "\n" + endpoint);
英文:
Update Controller to this :
@RestController
@RequestMapping("/v1/my-endpoint")
public class MyRestController {
@GetMapping(value = "/{type}/{code}", produces = { "application/json" })
public ResponseEntity<List<MyDTO>> query(HttpServletRequest request, HttpServletResponse response) {
String endpoint = request.getRequestURI();
String[] data = endpoint.split("/");
Integer code = Integer.valueOf(data[data.length - 1]);
String type = data[data.length - 2];
String key = request.getQueryString().split("=")[1];
System.out.println(code + "\n" + key + "\n" + type + "\n" + endpoint);
.....
}
}
Sample Request : http://localhost:8080/v1/my-endpoint/def/2?key=abc
Output on console :
2
abc
def
/v1/my-endpoint/def/2
Added an alternative way through spring :
String endpoint = ServletUriComponentsBuilder.fromCurrentRequestUri().toUriString();
String[] data = endpoint.split("/");
Integer code = Integer.valueOf(data[data.length - 1]);
String type = data[data.length - 2];
System.out.println(code + "\n" + type + "\n" + endpoint);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论