JAX-RS @Path URI matching example

本文详细介绍了JAX-RS中使用@Path注解进行URL路径匹配的方法,包括普通路径匹配、带有参数的路径匹配及利用正则表达式进行复杂路径匹配等。通过多个实例展示了如何灵活地设置和获取路径参数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

In JAX-RS, you can use @Path to bind URI pattern to a Java method. See following examples to show you how it works.

1. Normal URI Matching

See normal URI matching with @Path annotation.

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;

@Path("/users")
public class UserRestService {

    @GET
    public Response getUser() {

        return Response.status(200).entity("getUser is called").build();

    }

    @GET
    @Path("/vip")
    public Response getUserVIP() {

        return Response.status(200).entity("getUserVIP is called").build();

    }
}

URI pattern : “/users

getUser is called

URI pattern : “/users/vip

getUserVIP is called

2. URI Matching and Parameter

The value within an open brace “`{” and close brace “}”, is represents a parameter, and can be access with@PathParam`.

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

@Path("/users")
public class UserRestService {

    @GET
    @Path("{name}")
    public Response getUserByName(@PathParam("name") String name) {

        return Response.status(200)
            .entity("getUserByName is called, name : " + name).build();

    }

}

URI Pattern : “/users/mkyong

getUserByName is called, name : mkyong

URI Pattern : “/users/abcdefg

getUserByName is called, name : abcdefg

3. URI Matching and Regular Expression

@Path support complex URI matching with regular expression, via following expression :

{" variable-name [ ":" regular-expression ] "} 
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;

@Path("/users")
public class UserRestService {

    @GET
    @Path("{id : \\d+}") //support digit only
    public Response getUserById(@PathParam("id") String id) {

       return Response.status(200).entity("getUserById is called, id : " + id).build();

    }

    @GET
    @Path("/username/{username : [a-zA-Z][a-zA-Z_0-9]}")
    public Response getUserByUserName(@PathParam("username") String username) {

       return Response.status(200)
        .entity("getUserByUserName is called, username : " + username).build();

    }

    @GET
    @Path("/books/{isbn : \\d+}")
    public Response getUserBookByISBN(@PathParam("isbn") String isbn) {

       return Response.status(200)
        .entity("getUserBookByISBN is called, isbn : " + isbn).build();

    }

}

URI Pattern : “/users/999”

getUserById is called, id : 999

URI Pattern : “/users/123456”

getUserById is called, id : 123456

URI Pattern : “/users/username/aaa” , failed, don’t match “[a-zA-Z][a-zA-Z_0-9]”, first character need “[a-zA-Z]”, second character need “[a-zA-Z_0-9]”.

Could not find resource for relative : /users/username/aaa

URI Pattern : “/users/username/a9

getUserByUserName is called, username : a9

URI Pattern : “users/books/999

getUserBookByISBN is called, isbn : 999
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值