运行期间,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;
}
分享到:
相关推荐
Resource file and Excel file import and export Resource file and Excel file import and export Resource file and Excel file import and export
[基于QoS的资源分配与收发机优化]QoS-Based Resource Allocation and Transceiver Optimization
Resource file and Excel converter Resource file and Excel converter Resource file and Excel converter 解压密码 11112
Blockchain-enabled resource management and sharing for 6G communications.pdf
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
资源与能力关系的力学分析及其战略含意,杜世海,丁慧平,针对以往的有关企业能力的研究中存在的能力与资源难以区分、能力概念模糊、分类繁杂等问题,本文根据资源理论和动态能力理论对资
Resource allocation and cross-layer control in wireless networks
Resource serialization and concurrency control for Db2 z/OS
Internet resource pricing models, mechanisms, and methods
Flink OLAP Improvement of Resource Management and Runtime
This book includes a study of trustworthiness, percentile response time, service availability, and authentication in the networks between users and cloud service providers, and at service stations or ...
Collection and Identification (Discrimination) of Biological Species, Subspecies and Strains for Mushroom Breeding
Oracle Solaris 11.3 Resource Management and Oracle Solaris Zones Developer's Guide-106
Oracle Solaris 11.2 Resource Management and Oracle Solaris Zones Developer's Guide-92
"Small Cell Networks: Deployment, PHY Techniques and Resource Management", Cambridge University Press, New York, USA,
Oracle Solaris 11 Resource Management and Oracle Solaris Zones Developer's Guide-96
Primary field-data collection and methods of interpretation and analysis are also introduced. The second part of the book (Chapters 5–12) focuses on the various subdomains of water resources ...
We investigate the problem of efficient resource control for elastic traffic over the EDCA (Enhanced Distributed Channel Access) and HCCA (Hybrid Coordination Function - HCF - Controlled Channel ...
Oracle Solaris 10 Solaris Containers: Resource Management and Solaris Zones Developer's Guide-90
Blockchain-enabled resource management and sharing for 6G communications