java客户端传递参数_java – 在客户端传递参数
我使用RESTful Web服務(wù).在這個Web服務(wù)中,我必須傳遞一個我想要保存為參數(shù)的bean.
這是服務(wù)器代碼:
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Unidade inserir(Unidade unidade){
Session s = ConnectDb.getSession();
try {
s.getTransaction().begin();
s.save(unidade);
s.getTransaction().commit();
return unidade;
} catch (Exception e) {
e.printStackTrace();
s.getTransaction().rollback();
return null;
} finally {
s.close();
}
}
我在客戶端中有以下代碼:
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource webResource = client.resource("http://localhost:8080/RestauranteWeb/rest/unidades/7");
Builder builder = webResource.accept(MediaType.APPLICATION_JSON);
GenericType genericType = new GenericType() {};
Unidade u = new Unidade();
u.setUnidSigla("KG");
//How to pass this bean as parameter?
Unidade response = builder.post(genericType);
System.out.println(response);
如何將bean作為參數(shù)傳遞給方法?
解決方法:
不確定GenericType的目的是什么.無論如何,請嘗試下面的代碼.
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
Unidade u = new Unidade();
u.setUnidSigla("KG");
WebResource webResource = client.resource("http://localhost:8080/RestauranteWeb/rest/unidades/7");
Unidade response = webResource.accept(MediaType.APPLICATION_JSON)
.type(MediaType.APPLICATION_JSON)
.post(Unidade.class, u);
標簽:java,rest,web-services
來源: https://codeday.me/bug/20190529/1177472.html
總結(jié)
以上是生活随笔為你收集整理的java客户端传递参数_java – 在客户端传递参数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java jediscluster_方便
- 下一篇: pythonasyncio在哪个版本好_