---
title: "RESTEasy @path 与正则表达式映射"
author: "flycun2"
date: "2013-06-26T10:52:20Z"
tags: []
categories: ["Web前端"]
slug: "resteasy-path"
draft: false
lastmod: "2013-06-26T10:52:20Z"
id: 1894553
url: "https://flycun2.iteye.com/blog/1894553"
---

**@path 不限于简单的路径表达式，也可以在@path中插入正则表达式，通过如下格式：**

```expression

"{" variable-name [ ":" regular-expression ] "}"

```


```java
package com.example.rest.resteasy.service;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;

@Path("/hello")
public class HelloWorldRestService {
	// @Path and regular expression mappings
	@GET
	@Path("{var:.*}/stuff")
	public Response getStuff() {
		String result = "RESTEasy getStuff() is called! ";
		return Response.status(200).entity(result).build();
	}
}

```


请求1：http://localhost:8080/resteasy-example/hello/aa/stuff
请求2：http://localhost:8080/resteasy-example/hello/aa/bb/stuff

```html
RESTEasy getStuff() is called!
```

![](http://dl2.iteye.com/upload/attachment/0086/1975/94e7444f-33c4-3086-8325-6606616789ad.png)


**注：正则表达式中有一部分是可选的，如果没有提供表达式，默认匹配一个特定的段，例如:**

```expression
@Path("/hello/{var}/stuff")
```

可以匹配:

```expression
GET /hello/foo/stuff
GET /hello/bar/stuff
```

但是不能匹配:

```expression
GET /hello/a/bunch/of/stuff
```