Java 如何将列表转换为json并传入rest客户端url

Java 如何将列表转换为json并传入rest客户端url,java,json,spring,rest,Java,Json,Spring,Rest,我使用spring和RESTfulWeb服务实现了一个应用程序。在这里,我将列表转换为json格式,并尝试传入客户端url,但出现以下异常: java.lang.IllegalArgumentException: Illegal character in query at index 142: http://10.248.84.132:8082/nppWebService/updateModelGPData.do?lgdCode=33589&applicationId=8&uui

我使用spring和RESTfulWeb服务实现了一个应用程序。在这里,我将列表转换为json格式,并尝试传入客户端url,但出现以下异常:

java.lang.IllegalArgumentException: Illegal character in query at index 142: http://10.248.84.132:8082/nppWebService/updateModelGPData.do?lgdCode=33589&applicationId=8&uuid=9a26038f-6dd1-40b6-b847-f2fd16366fc0&jsonData={"noOfTrainingsOrganized":2,"noOfErsParticipated":4,"noOfOfficialsParticipated":5}
    at java.net.URI.create(Unknown Source)
    at com.sun.jersey.api.client.Client.resource(Client.java:433)
    at com.org.ep.trg.webservice.ClientRestController.main(ClientRestController.java:21)
Caused by: java.net.URISyntaxException: Illegal character in query at index 142: http://10.248.84.132:8082/nppWebService/updateModelGPData.do?lgdCode=33589&applicationId=8&uuid=9a26038f-6dd1-40b6-b847-f2fd16366fc0&jsonData={"noOfTrainingsOrganized":2,"noOfErsParticipated":4,"noOfOfficialsParticipated":5}
    at java.net.URI$Parser.fail(Unknown Source)
    at java.net.URI$Parser.checkChars(Unknown Source)
    at java.net.URI$Parser.parseHierarchical(Unknown Source)
    at java.net.URI$Parser.parse(Unknown Source)
    at java.net.URI.<init>(Unknown Source)
    ... 3 more
java.lang.IllegalArgumentException:索引142处的查询中存在非法字符:http://10.248.84.132:8082/nppWebService/updateModelGPData.do?lgdCode=33589&applicationId=8&uuid=9a26038f-6dd1-40b6-b847-f2fd16366fc0和jsonData={“有组织的无培训”:2,“无培训”:4,“无官方参与”:5}
位于java.net.URI.create(未知源)
位于com.sun.jersey.api.client.client.resource(client.java:433)
位于com.org.ep.trg.webservice.ClientRestController.main(ClientRestController.java:21)
原因:java.net.URISyntaxException:索引142处查询中的非法字符:http://10.248.84.132:8082/nppWebService/updateModelGPData.do?lgdCode=33589&applicationId=8&uuid=9a26038f-6dd1-40b6-b847-f2fd16366fc0和jsonData={“有组织的无培训”:2,“无培训”:4,“无官方参与”:5}
在java.net.URI$Parser.fail处(未知源)
位于java.net.URI$Parser.checkChars(未知源)
位于java.net.URI$Parser.parseHierarchy(未知源)
位于java.net.URI$Parser.parse(未知源)
位于java.net.URI。(未知源)
... 3个以上
以下是我的方法:

List<ModelGPStatusEntity>list2=new ArrayList<ModelGPStatusEntity>();
list2.add(new ModelGPStatusEntity(2,4,5));
String gson=new Gson().toJson(list2);
Client client = Client.create();
WebResource webResource = client.resource(
            "http://10.248.84.132:8082/nppWebService/updateModelGPData.do?lgdCode=33589&applicationId=8&uuid=9a26038f-6dd1-40b6-b847-f2fd16366fc0&jsonData="+gson.replace("[","").replace("]",""));

ClientResponse response = webResource.accept("application/json").get(ClientResponse.class);

if (response.getStatus() != 200) {
    throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
}

String output = response.getEntity(String.class);
System.out.println("Output from Server .... \n");
System.out.println(output);
list2=newarraylist();
列表2.添加(新模型GPStatusEntity(2,4,5));
字符串gson=new gson().toJson(list2);
Client=Client.create();
WebResource WebResource=client.resource(
"http://10.248.84.132:8082/nppWebService/updateModelGPData.do?lgdCode=33589&applicationId=8&uuid=9a26038f-6dd1-40b6-b847-f2fd16366fc0&jsonData=“+gson.replace(“[”,”).replace(“]);
ClientResponse response=webResource.accept(“application/json”).get(ClientResponse.class);
if(response.getStatus()!=200){
抛出新的RuntimeException(“失败:HTTP错误代码:+response.getStatus());
}
字符串输出=response.getEntity(String.class);
System.out.println(“从服务器输出…”\n);
系统输出打印项次(输出);
我有两个问题:

  • 在url中传递json数据是否正确?如果错误,传递json和调用客户端url的最佳方式是什么

  • 如果正确,如何解决此异常


  • 调用client.resource()时,您传递的字符对于URI是非法的。特别是,未替换的双引号可能是原因

    尝试使用创建有效的URI,并转义有问题的字符


    或者,大多数RESTWeb服务应该能够接受请求体中包含JSON数据的POST,而不是URL的一部分

    您可以使用POST方法并将整个JSON作为字符串放入request。您可能希望对JSON进行编码(即{应替换为%7B)。另一个选项是将JSON数据的contant作为查询参数传递,或者如果您实际向服务发送数据,则使用POST操作并将其发送到请求的有效负载中