I running a very simple REST web service to receive string params in post query, process them and send back a string response. In order to parse the params from string
我运行一个非常简单的REST Web服务来接收查询中的字符串参数,处理它们并发回字符串响应。为了从字符串解析params
param1=testData1¶m2=testData2&
I'm using String.split()
as under:
我正在使用String.split(),如下所示:
@POST
@Produces(MediaType.TEXT_PLAIN)
public String post(String str) {
String[] parts = str.split("&");
String part1 = parts[0]; // param1=testData1
String part2 = parts[1]; // param2=testData2
......do something on these values and return String....
return str;
}
for getting the param values I'll have to split again..... can someone please suggest a cleaner and elegant solution to get these param values..
为了获得参数值,我将不得不再次分裂.....有人可以建议一个更清洁和优雅的解决方案来获得这些参数值..
1 个解决方案
#1
Assuming its form parameters, one way that will work for either Jersey 1.x or Jersey 2.x is to inject MultivaluedMap
假设它的表单参数,一种适用于Jersey 1.x或Jersey 2.x的方法是注入多值映射
@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public String post(MultivaluedMap<String, String> form) {
StringBuilder builder = new StringBuilder();
for (Map.Entry<String, List<String>> params : form.entrySet()) {
for (String key: params.getValue()) {
builder.append("key: ").append(params.getKey()).append("; ")
.append("value: ").append(params.getValue().get(0)).append("\n");
}
}
return builder.toString();
}
If you know ahead of time the name of the form key, you could create a bean.
如果您提前知道表单键的名称,则可以创建一个bean。
public class Bean {
@FormParam("key1")
private String value1;
@FormParam("key2")
private String value2;
// getter and setters
}
For Jersey 2.x, you would inject with @BeanParam
对于Jersey 2.x,你会注入@BeanParam
@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response post(@BeanParam Bean bean) {
}
For Jersey 1.x you would inject with @InjectParam
对于Jersey 1.x,你会注入@InjectParam
@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response post(@InjectParam Bean bean) {
}
Note that the bean injection will work for all the other @XxxParam
s also, not just @FormParam
, for instance @QueryParam
, @PathParam
. You can mix and match them.
请注意,bean注入也适用于所有其他@XxxParams,而不仅仅是@FormParam,例如@QueryParam,@ PathParam。你可以混合搭配它们。
Or if you know the names ahead of time, if you don't need the bean, you can simply declare all the form params in the method signature
或者如果您提前知道名称,如果您不需要bean,则只需在方法签名中声明所有表单参数
@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response post(@FormParam("key1") String value1,
@FormParam("key2") String value2) {
}
#1
Assuming its form parameters, one way that will work for either Jersey 1.x or Jersey 2.x is to inject MultivaluedMap
假设它的表单参数,一种适用于Jersey 1.x或Jersey 2.x的方法是注入多值映射
@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public String post(MultivaluedMap<String, String> form) {
StringBuilder builder = new StringBuilder();
for (Map.Entry<String, List<String>> params : form.entrySet()) {
for (String key: params.getValue()) {
builder.append("key: ").append(params.getKey()).append("; ")
.append("value: ").append(params.getValue().get(0)).append("\n");
}
}
return builder.toString();
}
If you know ahead of time the name of the form key, you could create a bean.
如果您提前知道表单键的名称,则可以创建一个bean。
public class Bean {
@FormParam("key1")
private String value1;
@FormParam("key2")
private String value2;
// getter and setters
}
For Jersey 2.x, you would inject with @BeanParam
对于Jersey 2.x,你会注入@BeanParam
@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response post(@BeanParam Bean bean) {
}
For Jersey 1.x you would inject with @InjectParam
对于Jersey 1.x,你会注入@InjectParam
@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response post(@InjectParam Bean bean) {
}
Note that the bean injection will work for all the other @XxxParam
s also, not just @FormParam
, for instance @QueryParam
, @PathParam
. You can mix and match them.
请注意,bean注入也适用于所有其他@XxxParams,而不仅仅是@FormParam,例如@QueryParam,@ PathParam。你可以混合搭配它们。
Or if you know the names ahead of time, if you don't need the bean, you can simply declare all the form params in the method signature
或者如果您提前知道名称,如果您不需要bean,则只需在方法签名中声明所有表单参数
@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response post(@FormParam("key1") String value1,
@FormParam("key2") String value2) {
}