RESTEasy数据自动装配
环境:
JDK1.6
resteasy-jaxrs-2.3.4.Final
1,通过路径变量获取参数值。Webservice里方法如下:
@GET //books/ayb/123/cBookNmae-BookTitlec @Path("books/a{param}b/{id}/c{name}-{title}c") @Produces(MediaType.APPLICATION_JSON) public String getBooksBadger99(@PathParam("id") String id, @PathParam("param") String param,@PathParam("name") String name,@PathParam("title") String title) { return id+ "--"+param+"--"+name+"--"+title; }
在浏览器中输入:
http://localhost:8080/resteasy-json/resteasy/library/books/ayb/123/cBookNmae-BookTitlec
返回结果:
123--y--BookNmae--BookTitle
------jerval
2,通过路径正则表达式获取。Webservice里方法如下:@GET //books/aaabb/test/dsd/stuff //books/aaabb/test/stuff //books/aaabb/stuff @Path("books/aaa{param:b+}/{many:.*}/stuff") @Produces(MediaType.APPLICATION_JSON) public String getBooksBadger90(@PathParam("param") String bs, @PathParam("many") String many) { return bs+ "--"+many; }在浏览器中输入:
http://localhost:8080/resteasy-json/resteasy/library/books/aaabb/test/dsd/stuff http://localhost:8080/resteasy-json/resteasy/library/books/aaabb/test/stuff http://localhost:8080/resteasy-json/resteasy/library/books/aaabb/stuff返回结果:
bb--test/dsd bb--test 报错要想第三个case也能工作,只能将URL变成:
http://localhost:8080/resteasy-json/resteasy/library/books/aaabb//stuff----jerval 3,如果想获取多个不定数量的变量,可以使用如下方法: Webservice里的代码:
@GET //books/name=EJB%203.0;name=testest;author=Bill%20Burke;author=Jerval;id=11111;id=1231 @Path("books/{id}") public String getBook(@PathParam("id") PathSegment id) { System.out.println(id.getPath()); System.out.println(id.getMatrixParameters()); System.out.println(id.toString()); return "test"; }在浏览器中输入:
http://localhost:8080/resteasy-json/resteasy/library/books/name=EJB%203.0;name=testest;author=Bill%20Burke;author=Jerval;id=11111;id=1231返回结果:
name=EJB%203.0 {id=[11111, 1231], author=[Bill%20Burke, Jerval], name=[testest]} name=EJB%203.0;id=11111;id=1231;author=Bill%20Burke;author=Jerval;name=testest这里除了第一个值被存入id.getPath()里外,其它所有值均通过Map的方法放在了id.getMatrixParameters()。