日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java客户端传递参数_java – 在客户端传递参数

發(fā)布時間:2025/3/21 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java客户端传递参数_java – 在客户端传递参数 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

我使用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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。