访问路径变量和请求参数而不使用注解。

huangapple go评论68阅读模式
英文:

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(&quot;/v1/my-endpoint&quot;)
public class MyRestController {

    @Override
	@GetMapping(value = &quot;/{type}/{code}&quot;, produces = { &quot;application/json&quot; })
    public ResponseEntity&lt;List&lt;MyDTO&gt;&gt; query(
		@PathVariable(value = &quot;type&quot;) final String type,
		@PathVariable(value = &quot;code&quot;) final Integer code,
		@RequestParam(value = &quot;key&quot;, 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(&quot;type&quot;);
String key = request.getParam(&quot;key&quot;);
String endpoint = request.getPath(); // endpoint will hold &quot;my-endpoint&quot;

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(&quot;/v1/my-endpoint&quot;)
public class MyRestController {

	@GetMapping(value = &quot;/{type}/{code}&quot;, produces = { &quot;application/json&quot; })
	public ResponseEntity&lt;List&lt;MyDTO&gt;&gt; query(HttpServletRequest request, HttpServletResponse response) {
		String endpoint = request.getRequestURI();
		String[] data = endpoint.split(&quot;/&quot;);
	    Integer code = Integer.valueOf(data[data.length - 1]);
		String type = data[data.length - 2];
		String key = request.getQueryString().split(&quot;=&quot;)[1];
		System.out.println(code + &quot;\n&quot; + key + &quot;\n&quot; + type + &quot;\n&quot; + 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(&quot;/&quot;);
Integer code = Integer.valueOf(data[data.length - 1]);
String type = data[data.length - 2];
System.out.println(code + &quot;\n&quot; + type + &quot;\n&quot; + endpoint);

huangapple
  • 本文由 发表于 2020年9月18日 23:09:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/63958346.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定