ZetCode

JAX-RS @PathParam

最后修改于 2023 年 1 月 10 日

JAX-RS @PathParam 教程演示了如何在具有 Jersey 框架的 RESTful Java Web 应用程序中使用 @PathParam 注解。

Jersey

Jersey 是一个用于在 Java 中开发 RESTful Web 服务的框架。它是 Java API for RESTful Web Services (JAX-RS) 规范的参考实现。

JAX-RS @PathParam

JAX-RS @PathParam 注解将 URI 模板参数的值或包含模板参数的路径段绑定到资源方法的参数、资源类字段或资源类 bean 属性。

JAX-RS @PathParam 示例

以下示例是一个简单的 RESTful 应用程序,它将反转的单词以纯文本形式返回给客户端。

├── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   └── com
    │   │       └── zetcode
    │   │           ├── conf
    │   │           │   └── ApplicationConfig.java
    │   │           └── ws
    │   │               └── ReverseResource.java
    │   └── webapp
    │       └── META-INF
    │           └── context.xml
    └── test
        └── java

这是项目结构。

pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd">
    
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.zetcode</groupId>
    <artifactId>JerseyPathParam</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>JerseyPathParam</name>

   <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    
    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>2.25</version>
        </dependency>
        
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-server</artifactId>
            <version>2.25</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>

        </plugins>
    </build>

</project>

这是 Maven POM 文件。它包含 jersey-container-servletjersey-server 依赖项。

context.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/JerseyPathParam"/>

在 Tomcat 的 context.xml 配置文件中,我们定义了应用的上下文路径。

ApplicationConfig.java
package com.zetcode.conf;

import com.zetcode.ws.HelloResource;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("rest")
public class ApplicationConfig extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> set = new HashSet<>();
        set.add(ReverseResource.class);
        return set;
    }
}

这是应用程序配置类。自 Servlet 3.0 起,无需 web.xml 文件即可部署应用程序。在 Jersey 中,我们创建一个扩展抽象 Application 的配置类,并使用 @ApplicationPath 注解。Application 定义了 JAX-RS 应用程序的组件并提供其他元数据。在这里,我们注册了应用程序所需的资源类、提供程序或属性。

@ApplicationPath("rest")

使用 @ApplicationPath 注解,我们设置了 RESTful Web 服务的路径。

@Override
public Set<Class<?>> getClasses() {
    Set<Class<?>> set = new HashSet<>();
    set.add(ReverseResource.class);
    return set;
}

getClasses 方法中,我们添加了资源类。在我们的例子中,我们有一个 ReverseResource 类。

ReverseResource.java
package com.zetcode.ws;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("reverse")
public class ReverseResource {
    
    @GET
    @Path("/{word}")
    @Produces(MediaType.TEXT_PLAIN)
    public Response getMsg(@PathParam("word") String msg) {

        StringBuilder builder = new StringBuilder(msg);
        String output = builder.reverse().toString();

        return Response.status(200).entity(output).build();
    }    
}

这是 ReverseResource 类。

@Path("reverse")
public class ReverseResource {

@Path 指定资源响应的 URL。

@GET
@Path("/{word}")
@Produces(MediaType.TEXT_PLAIN)
public Response getMsg(@PathParam("word") String msg) {

@PathParam("word") 将路径段中的值绑定到 msg 方法参数。

StringBuilder builder = new StringBuilder(msg);
String output = builder.reverse().toString();

return Response.status(200).entity(output).build();

我们反转接收到的值,并将其作为纯文本发送回客户端。

$ curl https://:8084/JerseyPathParam/rest/reverse/summer
remmus

将应用程序部署到 Tomcat 后,我们使用 curl 向应用程序发送 GET 请求。@PathParam 注解从路径段读取 "summer world"。

在本教程中,我们使用了 JAX-RS @PathParam 注解。