寫一個代替postman的東西測試之前accessing-data-mysql-0.0.1-SNAPSHOT.jar

寫一個代替postman的端口測試之前accessing-data-mysql-0.0.1-SNAPSHOT.jar


之前的文件:

http://www.reibang.com/p/4820e5f7729c


官網(wǎng)地址:

https://spring.io/guides/gs/consuming-rest/


初始代碼分析:

用RestTemplate獲取指定url返回的數(shù)據(jù)蛇损,并以綁定的數(shù)據(jù)類型轉化减宣。

packagecom.example.consumingrest;

importorg.slf4j.Logger;

importorg.slf4j.LoggerFactory;

importorg.springframework.boot.CommandLineRunner;

importorg.springframework.boot.SpringApplication;

importorg.springframework.boot.autoconfigure.SpringBootApplication;

importorg.springframework.boot.web.client.RestTemplateBuilder;

importorg.springframework.context.annotation.Bean;

importorg.springframework.web.client.RestTemplate;

@SpringBootApplication

publicclassConsumingRestApplication{

privatestaticfinalLoggerlog=LoggerFactory.getLogger(ConsumingRestApplication.class);

publicstaticvoidmain(String[]args) {

? ? SpringApplication.run(ConsumingRestApplication.class,args);

}

@Bean

publicRestTemplaterestTemplate(RestTemplateBuilderbuilder) {

? ? returnbuilder.build();

}

@Bean

publicCommandLineRunnerrun(RestTemplaterestTemplate)throwsException{

? ? returnargs->{

? ? ? ? Quotequote=restTemplate.getForObject(

? ? ? ? ? ? ? ? "https://quoters.apps.pcfone.io/api/random",Quote.class);

? ? ? ? log.info(quote.toString());

? ? };

}


腳注:

[@SpringBootApplication]?開啟自動配置

[LoggerFactory.getLogger(ConsumingRestApplication.class);]?在控制臺打印ConsumingRestApplication.class類的日志叫倍。

[builder.build()]?使用 RestTemplateBuilder.build() 代替 new RestTemplate()


寫一個存數(shù)據(jù)的類(官網(wǎng)上分了兩層)

第一層:

packagecom.example.consumingrest;

importcom.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown=true)

publicclassQuote{

privateStringtype;

privateValuevalue;

publicQuote() {

? }

publicStringgetType() {

returntype;

? }

publicvoidsetType(Stringtype) {

this.type=type;

? }

publicValuegetValue() {

returnvalue;

? }

publicvoidsetValue(Valuevalue) {

this.value=value;

? }

@Override

publicStringtoString() {

return"Quote{"+

"type='"+type+'\''+

", value="+value+

'}';

? }

}

腳注:

[@JsonIgnoreProperties(ignoreUnknown = true)]?class中忽視所有沒寫的屬性(properties)


第二層:

packagecom.example.consumingrest;

importcom.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown=true)

publicclassValue{

privateLongid;

privateStringquote;

publicValue() {

? }

publicLonggetId() {

returnthis.id;

? }

publicStringgetQuote() {

returnthis.quote;

? }

publicvoidsetId(Longid) {

this.id=id;

? }

publicvoidsetQuote(Stringquote) {

this.quote=quote;

? }

@Override

publicStringtoString() {

return"Value{"+

"id="+id+

", quote='"+quote+'\''+

'}';

? }

}


要改的部分:

url

url改成我要調用的耗绿,而不是官網(wǎng)給的default版本。


返回的數(shù)據(jù)格式

由于這里的schema,輸出的數(shù)據(jù)格式時json。json文件中的鍵值對改成我要調用的url的輸出的格式。


我的核心代碼:

目標:實現(xiàn) accessing-data-mysql-0.0.1-SNAPSHOT.jar中增刪改查的postman所做的事控汉。


以get方法顯示所有用戶信息的功能。

由于顯示所有用戶信息返吻,所以現(xiàn)實的是一個List姑子,也就是一個數(shù)組。

@Bean

publicCommandLineRunnerrun(RestTemplaterestTemplate)throwsException{

returnargs->{

Quote[]quotes=restTemplate.getForObject(

"http://192.168.4.31:18001/user/all",Quote[].class);

assertquotes!=null;

log.info(Arrays.toString(quotes));

? };

}

寫一個存數(shù)據(jù)的類(官網(wǎng)上給了兩層來存數(shù)據(jù)测僵,我這邊只用一層)

由于顯示所有用戶信息街佑,所以現(xiàn)實的是一個List,也就是一個數(shù)組捍靠。

packagecom.example.consumingrest;

importcom.fasterxml.jackson.annotation.JsonIgnoreProperties;

importjava.util.ArrayList;

importjava.util.List;

@JsonIgnoreProperties(ignoreUnknown=true)

publicclassQuote{

privateIntegerid;

privateStringname;

privateStringemail;

publicQuote() {

?? }

publicIntegergetId() {

returnid;

?? }

publicvoidsetId(Integerid) {

this.id=id;

?? }

publicStringgetName() {

returnname;

?? }

publicvoidsetName(Stringname) {

this.name=name;

?? }

publicStringgetEmail() {

returnemail;

?? }

publicvoidsetEmail(Stringemail) {

this.email=email;

?? }

@Override

publicStringtoString() {

return"Quote{"+

"id="+id+

"name="+name+

"email="+email+'\''+

'}';

?? }

}

腳注:

[@JsonIgnoreProperties(ignoreUnknown = true)]?class中忽視所有沒寫的屬性(properties)


以post方法添加用戶信息

//? private static final Logger log = LoggerFactory.getLogger(ConsumingRestApplication.class);

@Bean

publicStringrun(RestTemplaterestTemplate)throwsException{

Stringurl="http://192.168.4.31:18001/user/add";

//headers

HttpHeadersrequestHeaders=newHttpHeaders();

requestHeaders.setContentType(MediaType.APPLICATION_JSON);

//body

//MultiValueMap<String, String> requestBody = new LinkedMultiValueMap<>();

Map<String,String>requestBody=newHashMap();

requestBody.put("id","2");

requestBody.put("name","2");

requestBody.put("email","2");

//HttpEntity

HttpEntity<Map<String,String>>requestEntity=newHttpEntity<>(requestBody,requestHeaders);

//HttpEntity<MultiValueMap> requestEntity = new HttpEntity<MultiValueMap>(requestBody, requestHeaders);

Strings=restTemplate.postForObject(url,requestEntity,String.class);

returns;

? };

腳注:

[requestHeaders.setContentType(MediaType.APPLICATION_JSON);]?設置請求的數(shù)據(jù)類型

[Map&lt;String, String&gt; requestBody = new HashMap();]?以hashmap來存輸入的Body

[requestBody.put(&quot;id&quot;, &quot;2&quot;);]?加入鍵值對


今日小tip:

一個端口上只能最多存在一個進程沐旨,所以要在服務器上用 consuming-rest-0.0.1-SNAPSHOT.jar來測試 accessing-data-mysql-0.0.1-SNAPSHOT.jar時,要用不同的端口榨婆。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
禁止轉載磁携,如需轉載請通過簡信或評論聯(lián)系作者。
  • 序言:七十年代末良风,一起剝皮案震驚了整個濱河市谊迄,隨后出現(xiàn)的幾起案子闷供,更是在濱河造成了極大的恐慌,老刑警劉巖鳞上,帶你破解...
    沈念sama閱讀 218,525評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異吊档,居然都是意外死亡篙议,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,203評論 3 395
  • 文/潘曉璐 我一進店門怠硼,熙熙樓的掌柜王于貴愁眉苦臉地迎上來鬼贱,“玉大人,你說我怎么就攤上這事香璃≌饽眩” “怎么了?”我有些...
    開封第一講書人閱讀 164,862評論 0 354
  • 文/不壞的土叔 我叫張陵葡秒,是天一觀的道長姻乓。 經常有香客問我,道長眯牧,這世上最難降的妖魔是什么蹋岩? 我笑而不...
    開封第一講書人閱讀 58,728評論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮学少,結果婚禮上剪个,老公的妹妹穿的比我還像新娘。我一直安慰自己版确,他們只是感情好扣囊,可當我...
    茶點故事閱讀 67,743評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著绒疗,像睡著了一般侵歇。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上吓蘑,一...
    開封第一講書人閱讀 51,590評論 1 305
  • 那天盒至,我揣著相機與錄音,去河邊找鬼士修。 笑死枷遂,一個胖子當著我的面吹牛,可吹牛的內容都是我干的棋嘲。 我是一名探鬼主播酒唉,決...
    沈念sama閱讀 40,330評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼沸移!你這毒婦竟也來了痪伦?” 一聲冷哼從身側響起侄榴,我...
    開封第一講書人閱讀 39,244評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎网沾,沒想到半個月后癞蚕,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經...
    沈念sama閱讀 45,693評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡辉哥,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,885評論 3 336
  • 正文 我和宋清朗相戀三年桦山,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片醋旦。...
    茶點故事閱讀 40,001評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡恒水,死狀恐怖,靈堂內的尸體忽然破棺而出饲齐,到底是詐尸還是另有隱情钉凌,我是刑警寧澤,帶...
    沈念sama閱讀 35,723評論 5 346
  • 正文 年R本政府宣布捂人,位于F島的核電站御雕,受9級特大地震影響,放射性物質發(fā)生泄漏滥搭。R本人自食惡果不足惜饮笛,卻給世界環(huán)境...
    茶點故事閱讀 41,343評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望论熙。 院中可真熱鬧福青,春花似錦、人聲如沸脓诡。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,919評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽祝谚。三九已至宪迟,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間交惯,已是汗流浹背次泽。 一陣腳步聲響...
    開封第一講書人閱讀 33,042評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留席爽,地道東北人意荤。 一個月前我還...
    沈念sama閱讀 48,191評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像只锻,于是被迫代替她去往敵國和親玖像。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,955評論 2 355