[英]Store JSON from Restful Web Service in List
I created a RESTful Java client and i am trying to store the JSON from the server to a List, but can't seem to do it. 我创建了一个RESTful Java客户端,并且尝试将JSON从服务器存储到列表中,但似乎无法做到这一点。 Tried to use the findAll() method, but no luck so far.
试图使用findAll()方法,但到目前为止还没有运气。 How else can i do this?
我还能怎么做? My code is as follows:
我的代码如下:
Note: All references to XML have been removed. 注意:已删除所有对XML的引用。 Everything is JSON.
一切都是JSON。
SalaryStructureClient.java SalaryStructureClient.java
package Consumer;
import javax.ws.rs.ClientErrorException;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.WebTarget;
public class SalaryStructureClient {
private WebTarget webTarget;
private Client client;
private static final String BASE_URI = "http://swiftworld.com/mass/web";
public SalaryStructureClient() {
client = javax.ws.rs.client.ClientBuilder.newClient();
webTarget = client.target(BASE_URI).path("salarystructure");
}
public void remove(String id) throws ClientErrorException {
webTarget.path(java.text.MessageFormat.format("{0}", new Object[]{id})).request().delete();
}
Control.java Control.java
public String getCount() {
count = getClient().countREST();
return count;
}
public List<SalaryStructure> getList() {
list = getClient().findAll(List.class); //Don't really know what to do here...
return list;
}
public void saveToDB(){
JsonObject json = Json.createObjectBuilder()
.add("structureLongName", structure.getStructureLongName())
.add("structureShortName", structure.getStructureShortName())
.build();
getClient().create(json);
structure = new SalaryStructure();
}
All the other methods are working perfectly. 所有其他方法均正常运行。
Thanks. 谢谢。
After a long research, finally got it working using the following method: 经过长时间的研究,终于可以使用以下方法使其工作:
public List<SalaryStructure> getList() {
list = client1.target("http://swiftworld.com/mass/web")
.path("salarystructure")
.request(MediaType.APPLICATION_JSON)
.get(new GenericType<List<SalaryStructure>>() {});
return list;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.