@PathParam和@PathVariable有什么区别

sur*_*esh 38 jax-rs spring-mvc restful-url path-parameter

据我所知,两者都有同样的目的.除了@PathVariable来自Spring MVC并@PathParam来自JAX-RS 的事实.有什么见解吗?

Pre*_*raj 25

@PathVariable@PathParam都用于从URI模板访问参数

区别:

  • 正如你所说,@ PathVariable来自spring,而@PathParam来自JAX-RS.
  • @PathParam只能与REST一起使用,其中@PathVariable在Spring中使用,因此它可以在MVC和REST中使用.


Omk*_*kar 18

PathParam:

将URI参数值分配给方法参数.在春天,它是@RequestParam.

例如.,

http://localhost:8080/books?isbn=1234

@GetMapping("/books/")
    public Book getBookDetails(@RequestParam("isbn") String isbn) {
Run Code Online (Sandbox Code Playgroud)

PathVariable:

将URI占位符值分配给方法参数.

例如.,

http://localhost:8080/books/1234

@GetMapping("/books/{isbn}")
    public Book getBook(@PathVariable("isbn") String isbn) {
Run Code Online (Sandbox Code Playgroud)

  • 在你的第一个例子中它不是请求参数吗 (4认同)
  • 您的 PathParam 标题具有误导性,因为您给出的解释是@RequestParam(不是 PathParam) (3认同)
  • @Omkar你的解释是错误的!PathParam是javax.ws.rs,相当于Spring的PathVariable。类似地,javax.ws.rs 中的 QueryParam 相当于 RequestParam (2认同)

FuS*_*SsA 8

@PathParam是一个参数注释,它允许您将变量URI路径片段映射到方法调用中.

@Path("/library")
public class Library {

   @GET
   @Path("/book/{isbn}")
   public String getBook(@PathParam("isbn") String id) {
      // search my database and get a string representation and return it
   }
}
Run Code Online (Sandbox Code Playgroud)

有关详细信息:JBoss DOCS

在Spring MVC中,您可以在方法参数上使用@PathVariable注释将其绑定到URI模板变量的值以获取更多详细信息:SPRING DOCS

  • 换句话说,它也是一样的,但`@ PathVariable`相当于在Spring中使用(?) (2认同)