一、get请求时,用对象接受基本类型的参数
基本思路:
定义一个基本类作为标识,让所有的参数对象来继承这个基本类;
jersey可以在服务请求后,resource方法调用之前进入到拦截代码块;
在拦截代码块中可以判断resource方法的参数对象是否继承了标识类;
如果是,则利用反射将get提交的参数值set进参数对象;
1、 定义标识类
public class Pagination extends BaseObject{
protected Integer pageSize=20;
protected Integer pageNo=-1; // -1 means don't paging
@JsonIgnore
protected Integer beginNo = 0;
protected Integer total = 0; }
2、关键的一步,需要提供一个provider类继承com.sun.jersey.spi.inject.InjectableProvider
@Provider
public class PaginationProvider implements InjectableProvider
@Context
private HttpContext hc;
@Context
protected HttpServletRequest request;
public PaginationProvider(@Context HttpContext hc, @Context HttpServletRequest request) {
this.hc = hc;
this.request = request;
}
@Override
public ComponentScope getScope() {
return ComponentScope.PerRequest;
}
@Override
public Injectable getInjectable(ComponentContext ic, QueryParam a,
Parameter c) {
final Class clazz = c.getParameterClass(); // 获取参数对象
if (!Pagination.class.isAssignableFrom(clazz)) { // 判断是否继承标识类
return null;
}
return new Injectable
public Object getValue() {
Object parameterBean=null;
try {
parameterBean = clazz.newInstance();
MultivaluedMap
Class clz = clazz;
for (;!clz.equals(Object.class);clz=clz.getSuperclass()) {
Field[] fields = clz.getDeclaredFields();
for (Field field:fields) {
Object value = params.get(field.getName());
if (value == null) {
continue;
}
try {
BeanUtils.setProperty(parameterBean, field.getName(), value); //属性拷贝
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
//对象参数已经注入了所提交的参数值
// 设置上下文 //这里还可以做其他工作
if (ContextPagination.class.isAssignableFrom(clazz)) {
User user = (User)request.getAttribute(CacheBase.USERINFO);
ContextPagination context = (ContextPagination)parameterBean;
context.setCurrUserId(user.getUserId());
context.setCurrFeedId(user.getFeedId());
context.setNetworkId(user.getNetworkId());
context.setOrgId(user.getOrgId());
context.setCurrUserName(user.getUsername());
}
} catch (InstantiationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IllegalAccessException e1) {
e1.printStackTrace();
}
return parameterBean;
}
};
}
}
二、url访问服务之前可以将参数进行拦截,比如对参数进行权限检查,
但post参数只能使用一次,当权限检查使用之后,
resource服务里面就不能获取该参数;
基本思路:将参数取出之后,在设置一遍,听起来很简答,但jersey将设置的方法隐藏的很深,找了好久才找到了
private List getValFromRequest(ContainerRequest request , String[] targets) throws JSONException{
List values = new ArrayList();
String var = null;
if(targets != null && targets.length > 0){
String method = request.getMethod();
if (method.equals("GET")) {
MultivaluedMap map = request.getQueryParameters();
for (int i=0;i
List list = (List)map.get(targets[i]);
var = list.get(0);
values.add(var);
}
}
if (method.equals("POST")) {
MediaType tmp = request.getMediaType();
MediaType media = new MediaType(tmp.getType(), tmp.getSubtype());
if (media.equals(MediaType.APPLICATION_JSON_TYPE)) {
Object object = request.getEntity(Object.class);
for (int i=0;i
try {
var = BeanUtils.getProperty(object, targets[i]);
} catch (IllegalAccessException e) {
logger.error(e.getMessage(),e.getCause());
} catch (InvocationTargetException e) {
logger.error(e.getMessage(),e.getCause());
} catch (NoSuchMethodException e) {
logger.error(e.getMessage(),e.getCause());
}
values.add(var);
}
request.setEntity(Object.class, Object.class, null, null, null, object);
}
}
}
return values;
}