JAX-RS入门 五: 自动类型转换
2016-06-30 17:30
447 查看
转自:http://liugang594.iteye.com/blog/1497618
一、默认类型转换规则
在上一节中,已经了解了怎么使用那个annotations去提取请求中各种信息,不过得到的信息值默认都是一个string类型。
这一节介绍JAX-RS一些内置的自动类型转换及其规则。
理论上JAX-RS可以将请求信息转换成任一Java类型,只要该Java类型满足以下条件之一:
基本类型: int、short、float、double、byte、char 或 boolean 等
定义了带单个String参数的构造方法
拥有一个static的valueOf(String)方法,并且这个方法返回这个类型的一个实例
java.util.List<T>、java.util.Set<T>或java.util.SortedSet<T>,其中 T 满足条件2或者3,或者是一个String
例如:
转换成int代码
@GET
@Path("{id}")
public String get(@PathParam("id") int id) {...}
转成对象代码
@Path("/myservice")
public class MyService {
@GET
@Produces("text/html")
public String get(@HeaderParam("Referer") URL referer) {
...
}
}
Valueof()方法代码
public enum Color {
BLACK,
BLUE,
RED,
WHITE,
SILVER
}
@GET
@Path("/{model}/{year}")
@Produces("image/jpeg")
public Jpeg getPicture(@PathParam("make") String make,
@PathParam("model") String model,
@MatrixParam("color") Color color) {
...
}
转成一个list代码
import java.util.List;
@Path("/customers")
public class CustomerResource {
@GET
@Produces("application/xml")
public String getCustomers(
@QueryParam("start") int start,
@QueryParam("size") int size,
@QueryParam("orderBy") List<String> orderBy) {
...
}
}
输入:GET /customers?orderBy=last&orderBy=first
如果转换失败,则认为client请求出错,返回一个404错误。
二、定义缺省值 @DefaultValue
通过使用@DefaultValue注释,可以给某个请求参数定义缺省值,当client的请求中未包含此参数,则缺省参数值将被使用,例如:
缺省值代码
@Path("/customers")
public class CustomerResource {
@GET
@Produces("application/xml")
public String getCustomers(@DefaultValue("0") @QueryParam("start") int start,
@DefaultValue("10") @QueryParam("size") int size) {
...
}
}
如果请求中未提供 start 请求参数,则缺省值0将被使用;如果请求中未包含 size 参数,则缺省值10被使用。
三、强制不解码 @Encoded
使用@Encoded注释,用来告诉JAX-RS,不需要自动解码,直接使用编码后的请求值,例如:
@encoded代码
@GET
@Produces("application/xml")
public String get(@Encoded @QueryParam("something") String str) {...}
一、默认类型转换规则
在上一节中,已经了解了怎么使用那个annotations去提取请求中各种信息,不过得到的信息值默认都是一个string类型。
这一节介绍JAX-RS一些内置的自动类型转换及其规则。
理论上JAX-RS可以将请求信息转换成任一Java类型,只要该Java类型满足以下条件之一:
基本类型: int、short、float、double、byte、char 或 boolean 等
定义了带单个String参数的构造方法
拥有一个static的valueOf(String)方法,并且这个方法返回这个类型的一个实例
java.util.List<T>、java.util.Set<T>或java.util.SortedSet<T>,其中 T 满足条件2或者3,或者是一个String
例如:
转换成int代码
@GET
@Path("{id}")
public String get(@PathParam("id") int id) {...}
转成对象代码
@Path("/myservice")
public class MyService {
@GET
@Produces("text/html")
public String get(@HeaderParam("Referer") URL referer) {
...
}
}
Valueof()方法代码
public enum Color {
BLACK,
BLUE,
RED,
WHITE,
SILVER
}
@GET
@Path("/{model}/{year}")
@Produces("image/jpeg")
public Jpeg getPicture(@PathParam("make") String make,
@PathParam("model") String model,
@MatrixParam("color") Color color) {
...
}
转成一个list代码
import java.util.List;
@Path("/customers")
public class CustomerResource {
@GET
@Produces("application/xml")
public String getCustomers(
@QueryParam("start") int start,
@QueryParam("size") int size,
@QueryParam("orderBy") List<String> orderBy) {
...
}
}
输入:GET /customers?orderBy=last&orderBy=first
如果转换失败,则认为client请求出错,返回一个404错误。
二、定义缺省值 @DefaultValue
通过使用@DefaultValue注释,可以给某个请求参数定义缺省值,当client的请求中未包含此参数,则缺省参数值将被使用,例如:
缺省值代码
@Path("/customers")
public class CustomerResource {
@GET
@Produces("application/xml")
public String getCustomers(@DefaultValue("0") @QueryParam("start") int start,
@DefaultValue("10") @QueryParam("size") int size) {
...
}
}
如果请求中未提供 start 请求参数,则缺省值0将被使用;如果请求中未包含 size 参数,则缺省值10被使用。
三、强制不解码 @Encoded
使用@Encoded注释,用来告诉JAX-RS,不需要自动解码,直接使用编码后的请求值,例如:
@encoded代码
@GET
@Produces("application/xml")
public String get(@Encoded @QueryParam("something") String str) {...}
相关文章推荐
- 【iOS】CoreBluetooth1 初识
- CentOS系统mysql数据库自动备份脚本
- Bitmap基本概念及在Android4.4系统上使用BitmapFactory的注意事项
- R语言:gl()函数
- java 内部类(inner class)详解
- lazarus:一个字体引擎例子
- oracle使用数据泵导入导出数据
- sql好文分享
- SpringMVC利用MultipartFile进行文件上传
- 浅谈Redis的键值设计
- Maven+SpringMVC+MyBatis的环境搭建(二)
- ios 跨页返回,从navigationController 删除指定页面
- 【Struts2】:Struts2的类型转换
- JAX-RS入门 四: 注入
- Java 基础部面试
- Linux命令--dmesg
- OPenCV学习笔记(5)——图像缩放
- 初学mysql & python & python链接数据库(mysql) & mysql for linux
- java学习笔记——maven
- 测试App运行状态