我正在尝试将多个参数传递给Jersey POST方法。目前,我按照以下步骤将单个参数传递给Jersey POST方法。
Client client = ClientBuilder.newClient();
WebTarget target= client.target("http://localhost:8080/Rest/rest/subuser").path("/insertSubUser");
SubUserBean subUserBean=new SubUserBean();
subUserBean.setIdUser(1);
subUserBean.setIdSubUserType(1);
subUserBean.setIdSubUser(15);
subUserBean.setFirstName("Haritha");
subUserBean.setLastName("Wijerathna");
subUserBean.setNumberOfDaysToEditRecord(14);
subUserBean.setUserName("haritha");
subUserBean.setPassword("hariwi88");
subUserBean.setDateCreated(Common.getSQLCurrentTimeStamp());
subUserBean.setLastUpdated(Common.getSQLCurrentTimeStamp());
target.request(MediaType.APPLICATION_JSON_TYPE).post(Entity.entity(subUserBean, MediaType.APPLICATION_JSON_TYPE));
SubUserJSONService.java
@Path("/subuser")
public class SubUserJSONService {
@POST
@Path("/insertSubUser")
@Consumes(MediaType.APPLICATION_JSON)
public String updateSubUser(SubUserBean bean){
SubUserInterface table = new SubUserTable();
String result= table.insertSubUser(bean);
return result;
}
}
现在,我想通过Jersey POST方法将参数传递给以下方法。
public String insertHistory(List<SocialHistoryBean> list, String comment){
//my stuffs
}
上面有什么想法吗?
谢谢。
答案 0 :(得分:4)
您可以尝试使用MultivaluedMap。添加表单数据并将其发送到服务器。下面是一个示例,代码不仅仅针对演示/逻辑流进行测试。
WebTarget webTarget = client.target("http://www.example.com/some/resource");
MultivaluedMap<List, String> formData = new MultivaluedHashMap<List, String>();
formData.add(List, "list1");
formData.add("key2", "value2");
Response response = webTarget.request().post(Entity.form(formData));
在服务器端使用此类似
@Path("/uripath")
@POST -- if this is post or @GET
@Consumes("application/x-www-form-urlencoded;charset=UTF-8") or json..
@Produces("application/json")
public void methodNameHere(@FormParam("list") List<String> list1, @FormParam("key2") String val2) {
System.out.println("Here are I am");
System.out.println("list1" + list1.size);
System.out.println("val2" + val2);
}
在文档中阅读更多here ..
答案 1 :(得分:2)
JSON数据无法传递到List中的服务器。这意味着您应该在SocialHistoryBean类周围创建一个包装器(即包含对象的列表周围)
@XmlRootElement(name = "uw")
public class SocialHistoryBeanWrapper implements Serializable {
private List<SocialHistoryBean> sList ;//this will hold your SocialHistoryBean instances
public SocialHistoryBeanWrapper(){
sList = new ArrayList<User>();
}
public List<User> getUsrList(){
return sList;
}
}
您的服务器端代码将类似于
@POST
@Path("/history")
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.APPLICATION_JSON)
public String insertHistory( @QueryParam("comment") String comment, SocialHistoryBeanWrapper uw) {
do whatever you want with your history data
//userData.setUser(uw.getUsrList().get(0));
return comment; //just echo the string that we have sent from client
}
请注意,注释与@QueryParam一起传递(这意味着它不是POST请求(正文)的一部分,而是在URL字符串中编码。为此,您可以将您的服务称为(客户端代码)< / p>
WebTarget target = client.target(UriBuilder.fromUri("http://localhost:8088/Rest/rest/subuser").build());
SocialHistoryBeanWrapper uw = new SocialHistoryBeanWrapper();
//just populate whatever fields you have;
uw.getUsrList().get(0).setName("Mark Foster");
uw.getUsrList().get(0).setProfession("writer");
uw.getUsrList().get(0).setId(55);
String s = target.path("history").queryParam("comment", "OK").request()
.accept(MediaType.TEXT_PLAIN).post(Entity.entity(uw, MediaType.APPLICATION_JSON), String.class);
System.out.println(s);//this prints OK
答案 2 :(得分:1)
如果您使用的是Jersey 1.x,请查看此示例,了解如何将多个对象发布为 @FormParam
客户端:(纯Java):
public Response testPost(String param1, String param2) {
// Build the request string in this format:
// String request = "param1=1¶m2=2";
String request = "param1=" + param1+ "¶m2=" + param2;
WebClient client = WebClient.create(...);
return client.path(CONTROLLER_BASE_URI + "/test")
.post(request);
}
<强> 服务器: 强>
@Path("/test")
@POST
@Produces(MediaType.APPLICATION_JSON)
public void test(@FormParam("param1") String param1, @FormParam("param2") String param2) {
...
}