Jordan Blay
2011-12-21 14:53:22 UTC
Hi!
Since Jersey doesn't have the @Required annotation I decided to implement
mine.
In my code, I need to retrieve the query parameters, the form parameters and
the json object fields and values.
My filter is:
private class Filter implements ResourceFilter, ContainerRequestFilter {
private final ArrayList<String> requiredParameters;
protected Filter() {
requiredParameters = null;
}
protected Filter(ArrayList<String> requiredParameters) {
this.requiredParameters = requiredParameters;
}
@Override
public ContainerRequestFilter getRequestFilter() {
return this;
}
@Override
public ContainerResponseFilter getResponseFilter() {
return null;
}
// ContainerRequestFilter
@Override
public ContainerRequest filter(ContainerRequest request) {
if (requiredParameters != null && requiredParameters.size() > 0) {
// loop over them and see if the required parameters are not
// empty
MultivaluedMap<String, String> params = request.getQueryParameters();
params.putAll(request.getFormParameters());
StringBuffer missingParams = new StringBuffer();
for (String reqParam : requiredParameters) {
List<String> paramValues = params.get(reqParam);
if (paramValues == null || paramValues != null && paramValues.size() ==
0)
missingParams.append(reqParam + ",");
}
if (missingParams.length() > 0)
throw new WrongInputException("Required parameters are missing: " +
missingParams);
}
return request;
}
}
As you can see in filter method I can retrieve the query and form param as
map: name and value. I would like to retrieve the same for the json object.
For instance, if I have the object:
@XmlRootElement
public class AnyEmail{
String name;
}
I will also get "name","value" in another map.
What is the way to do it? Is there a prepared Jersey method for it or I have
to use reflection and do it myself?
--
View this message in context: http://jersey.576304.n2.nabble.com/retrieve-json-object-fields-tp7115376p7115376.html
Sent from the Jersey mailing list archive at Nabble.com.
Since Jersey doesn't have the @Required annotation I decided to implement
mine.
In my code, I need to retrieve the query parameters, the form parameters and
the json object fields and values.
My filter is:
private class Filter implements ResourceFilter, ContainerRequestFilter {
private final ArrayList<String> requiredParameters;
protected Filter() {
requiredParameters = null;
}
protected Filter(ArrayList<String> requiredParameters) {
this.requiredParameters = requiredParameters;
}
@Override
public ContainerRequestFilter getRequestFilter() {
return this;
}
@Override
public ContainerResponseFilter getResponseFilter() {
return null;
}
// ContainerRequestFilter
@Override
public ContainerRequest filter(ContainerRequest request) {
if (requiredParameters != null && requiredParameters.size() > 0) {
// loop over them and see if the required parameters are not
// empty
MultivaluedMap<String, String> params = request.getQueryParameters();
params.putAll(request.getFormParameters());
StringBuffer missingParams = new StringBuffer();
for (String reqParam : requiredParameters) {
List<String> paramValues = params.get(reqParam);
if (paramValues == null || paramValues != null && paramValues.size() ==
0)
missingParams.append(reqParam + ",");
}
if (missingParams.length() > 0)
throw new WrongInputException("Required parameters are missing: " +
missingParams);
}
return request;
}
}
As you can see in filter method I can retrieve the query and form param as
map: name and value. I would like to retrieve the same for the json object.
For instance, if I have the object:
@XmlRootElement
public class AnyEmail{
String name;
}
I will also get "name","value" in another map.
What is the way to do it? Is there a prepared Jersey method for it or I have
to use reflection and do it myself?
--
View this message in context: http://jersey.576304.n2.nabble.com/retrieve-json-object-fields-tp7115376p7115376.html
Sent from the Jersey mailing list archive at Nabble.com.