运行期间,Parameters, fields, and bean properties可通过HTTP request message对应的值注入来进行初始化,注入时
支持的数据类型包括:
(1) 原生类型,如:int,char,long;
(2) 拥有单个String 参数的构造函数的Object;
(3) 拥有静态方法valueOf(),并且该方法接受单个String 参数的Object;
(4) List<T>, Set<T>, or SortedSet<T> objects where T satisfies the other conditions in the list
1. Injecting data from a request URI
(1) Getting data from the URI's path
例如:URI template中的color注入itemColr field.
import javax.ws.rs.Path; import javax.ws.rs.PathParam @Path("/boxes/{shape}/{color}") public class Box{ @PathParam("color") String itemColor; }
(2) Using query parameters
URI with a query string template: http://fusesource.org?name=value;name2=value2;...
例如:处理HTTP Post :/monstersforhire/daikaiju?id=jonas, 方法updateMonster()中type值为:daikaiju,
id值为:jonas.
import javax.ws.rs.QueryParam; import javax.ws.rs.PathParam; import javax.ws.rs.POST; import javax.ws.rs.Path; @Path("/monstersforhire/") public class MonsterService{ @POST @Path("\{type}") public void updateMonster(@PathParam("type") String type,@QueryParam("id") String id){ } }
(3) Using matrix parameters
与query parameters不同的是,matrix parameters可以在URI中任何地方出现,采用“;”与URI的主要Path分离;
例如:/mostersforhire/daikaiju;id=jonas 有一个 matrix parameter:id;
/monstersforhire/japan;type=daikaiju/flying;wingspan=40有两个matrix parameters:type,wingspan.
import javax.ws.rs.MatrixParam; import javax.ws.rs.POST; import javax.ws.rs.Path; @Path("/monstersforhire/") public class MonsterService { @POST public void updateMonster(@MatrixParam("type") String type, @MatrixParam("id") String id) { } }
处理HTTP Post :/monstersforhire;type=daikaiju;id=whale, 方法updateMonster()中type值为:daikaiju,
id值为:whale.
(4) Disabling URI decoding
默认情况下,URI是被编码的,可在下面三个不同层次取消编码,即@Encoded注解放置的位置:
class level:这将禁止URI编码 for 类中的parameters, field, and bean properties;
method level: 这将禁止URI编码 for 类中的parameters;
parameter/field level:这将禁止URI编码 for 类中的parameters.
例如:getMonster()没有视同编码,addMonster()只禁用type参数编码:
@Path("/monstersforhire/") public class MonsterService{ @GET @Encoded @Path("\{type}") public Monster getMonster(@PathParam("type") String type, @QueryParam("id") String id){ } @PUT @Path("\{id}") public void addMonster(@Encoded @PathParam("type") String type,@QueryParam("id") String id){ } }
2. Injecting data from the HTTP message header
(1) Injecting information from the HTTP headers
import javax.ws.rs.HeaderParam; public class RecordKeeper { @HeaderParam("If-Modified-Since") String oldestDate; }
(2) Injecting information from a cookie
import javax.ws.rs.CookieParam; public class CB { @CookieParam("handle") String handle; }
3. Injecting data from HTML forms
Using the @FormParam annotation to inject form data
例如:提交上来的的title,tags,body数据将被注入updatePost()对应的三个参数上:
import javax.ws.rs.FormParam; import javax.ws.rs.POST; @POST public boolean updatePost(@FormParam("title") String title,@FormParam("tags") String tags,@FormParam("body") String post){ }
4. Specifying a default value to inject
@DefaultValue可以与下面的注解联合使用:
@PathParam
@QueryParam
@MatrixParam
@FormParam
@HeaderParam
@CookieParam
例如:下面例子中如果reques URI:baseURI/monster?id=1&type=fomóiri, 则返回id=1,type=fomóiri的数据;如果
request URI中忽略了这两个参数,则将返回id=42,type=bogeyman的数据。
import javax.ws.rs.DefaultValue; import javax.ws.rs.PathParam; import javax.ws.rs.QueryParam; import javax.ws.rs.GET; import javax.ws.rs.Path; @Path("/monster") public class MonsterService{ @Get public Monster getMonster(@QueryParam("id") @DefaultValue("42") int id, @QueryParam("type") @DefaultValue("bogeyman") String type) { } }
5. Using Fuse Services Framework extensions
Fuse Services Framework extensions扩展JAX-WS提供了允许开发者替换一系列的注解而至使用一个注解,
当前该扩展只支持下面的标准注解:
@PathParam
@QueryParam
@MatrixParam
@FormParam
为了使用该扩展使用序列化注入值入bean,需按照下面要求处理:
(1) 指定主角的参数是一个空字符串;
(2) 确保注解的参数是一个bean,并且该bean的field匹配即将被注入的值。
例如:如果request URI包括两个Query Paramter:type,id, 那么对应的值将被注入Monster bean.
import javax.ws.rs.QueryParam; import javax.ws.rs.PathParam; import javax.ws.rs.POST; import javax.ws.rs.Path; @Path("/monstersforhire/") public class MonsterService { @POST public void updateMonster(@QueryParam("")Monster bean) { } } public class Monster { String type; String id; }