`
flycun2
  • 浏览: 26794 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

RESTEasy @path 与正则表达式映射

阅读更多
@path 不限于简单的路径表达式,也可以在@path中插入正则表达式,通过如下格式:
"{" variable-name [ ":" regular-expression ] "}"

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
RESTEasy getStuff() is called!




注:正则表达式中有一部分是可选的,如果没有提供表达式,默认匹配一个特定的段,例如:
@Path("/hello/{var}/stuff")

可以匹配:
GET /hello/foo/stuff
GET /hello/bar/stuff

但是不能匹配:
GET /hello/a/bunch/of/stuff
  • 大小: 4 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics