JAX-RS @QueryParam
最后修改于 2023 年 1 月 10 日
JAX-RS @QueryParam 教程展示了如何在带有 Jersey 框架的 RESTful Java Web 应用中使用 @QueryParam
注解。
Jersey
Jersey 是一个用于在 Java 中开发 RESTful Web 服务的框架。它是 Java API for RESTful Web Services (JAX-RS) 规范的参考实现。另一个流行的 JAX-RS 实现是 JBoss 的 RESTEasy。
JAX-RS @QueryParam
JAX-RS @QueryParam 注解将 HTTP 查询参数的值绑定到资源方法的参数、资源类的字段或资源类的 Bean 属性。
JAX-RS @QueryParam 示例
以下示例是一个简单的 RESTful 应用,它将一些与上下文相关的数据作为纯文本返回给客户端。
├── pom.xml └── src ├── main │ ├── java │ │ └── com │ │ └── zetcode │ │ ├── conf │ │ │ └── ApplicationConfig.java │ │ └── ws │ │ └── MyResource.java │ ├── resources │ └── webapp │ └── META-INF │ └── context.xml └── test └── java
这是项目结构。
<?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>JerseyQueryParam</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>JerseyQueryParam</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-servlet
和 jersey-server
。
<?xml version="1.0" encoding="UTF-8"?> <Context path="/JerseyQueryParam"/>
在 Tomcat 的 context.xml
配置文件中,我们定义了应用的上下文路径。
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(MyResource.class); return set; } }
这是应用程序配置类。自 Servlet 3.0 起,无需 web.xml
文件即可部署应用程序。Application
定义了 JAX-RS 应用程序的组件并提供额外的元数据。在这里,我们注册应用程序所需的资源类、提供程序或属性。
@ApplicationPath("rest")
使用 @ApplicationPath
注解,我们设置了 RESTful Web 服务的路径。
@Override public Set<Class<?>> getClasses() { Set<Class<?>> set = new HashSet<>(); set.add(MyResource.class); return set; }
在 getClasses
方法中,我们添加资源类。在本例中,我们有一个 MyResource
类。
package com.zetcode.ws; import javax.ws.rs.DefaultValue; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; @Path("myresource") public class MyResource { @GET @Produces(MediaType.TEXT_PLAIN) public Response message(@DefaultValue("Guest") @QueryParam("name") String name, @DefaultValue("0") @QueryParam("age") int age) { StringBuilder builder = new StringBuilder(); builder.append(name).append(" is ") .append(age).append(" years old"); String output = builder.toString(); return Response.status(200).entity(output).build(); } }
这是 MyResource
类。
@Path("myresource") public class MyResource {
@Path
指定资源响应的 URL。
@GET @Produces(MediaType.TEXT_PLAIN)
@GET
注解表示带注解的方法响应 HTTP GET 请求。使用 @Produces
注解,我们定义该方法产生纯文本。
public Response message(@DefaultValue("Guest") @QueryParam("name") String name, @DefaultValue("0") @QueryParam("age") int age) {
我们将 @QueryParam
注解用于方法参数。我们期望两个参数:name
和 age
。@DefaultValue
值提供了参数的默认值。当 URL 中缺少这些参数时,将使用它们。
StringBuilder builder = new StringBuilder(); builder.append(name).append(" is ") .append(age).append(" years old"); String output = builder.toString();
根据提供的参数值,我们构建一条消息。
return Response.status(200).entity(output).build();
我们将包含消息的响应发送给客户端。
$ curl "localhost:8084/JerseyQueryParam/rest/myresource?name=Peter&age=45" Peter is 45 years old
在应用程序部署到 Tomcat 后,我们使用 curl
向应用程序发送一个 GET 请求。URL 包含这两个参数。
在本教程中,我们使用了 JAX-RS @QueryParam
注解。