欧美三级电影完整|亚洲一二三四久久|性爱视频精品一区二区免费在线观看|国产精品啪啪视频|婷婷六月综合操人妻视频网站|99爱免费视频在线观看|美女一级片在线观看|北京熟女88av|免费看黄色A级电影|欧美黄色毛片儿

springboot學(xué)習(xí)之構(gòu)建 RESTful Web服務(wù)

2023-04-12


springboot學(xué)習(xí)之構(gòu)建 RESTful Web服務(wù)

  • 學(xué)習(xí)目標(biāo)
  • 學(xué)習(xí)環(huán)境
  • 學(xué)習(xí)內(nèi)容
  • 新建web工程
  • 新建實(shí)體類
  • 新建服務(wù)接口
  • 運(yùn)行、測(cè)試
  • 攜帶name參數(shù)
  • 無(wú)name參數(shù)
  • 學(xué)習(xí)總結(jié)

學(xué)習(xí)目標(biāo)


  • 新建一個(gè)get請(qǐng)求,服務(wù)請(qǐng)求url為http://127.0.0.1:8080/greeting 返回一個(gè)應(yīng)答信息:
{"id":1,"content":"Hello, World!"}
  • 請(qǐng)求中可以攜帶參數(shù)如下:
http://127.0.0.1:8080/greeting?name=lili
  • 應(yīng)答信息中的“World”可以被“王山”覆蓋,返回信息如下:
{"id":1,"content":"Hello, lili!"}

學(xué)習(xí)環(huán)境


idea
maven3.6.3
jdk1.8


學(xué)習(xí)內(nèi)容


新建web工程

1、File->New->Project……




2、點(diǎn)擊Next




3、配置項(xiàng)目信息,繼續(xù)Next




4、選擇依賴,繼續(xù)Next




5、設(shè)置項(xiàng)目名稱和路徑位置,點(diǎn)擊Finish




6、工程搭建完畢。


新建實(shí)體類

package com.spring.demo.domain;

public class Greeting {

    private long id;
    private String content;

    public Greeting(long id, String content) {
        this.id = id;
        this.content = content;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    @Override
    public String toString() {
        return "Greeting{" +
                "id=" + id +
                ", content='" + content + '\'' +
                '}';
    }
}

新建服務(wù)接口

package com.spring.demo.web;

import com.spring.demo.domain.Greeting;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/greeting")
public class GreetingController {

    @GetMapping()
    //@RequestMapping(method=GET)
    public Greeting greeting(String name){
        Greeting greeting = null;
        if (name != null && !"".equals(name)) {
            greeting = new Greeting(1, "Hello, "+name+"!");
        } else {
            greeting = new Greeting(1, "Hello, World!");
        }
        return greeting;
    }
}

運(yùn)行、測(cè)試

攜帶name參數(shù)


無(wú)name參數(shù)


學(xué)習(xí)總結(jié)


通過(guò)自己的實(shí)踐和官網(wǎng)提供的demo,可做如下總結(jié)。


  1. @RestController表示該類的每個(gè)方法都返回一個(gè)domain以替代view,它同時(shí)包含@Controller和@ResponseBody
  2. @GetMapping()也可寫成@RequestMapping(method=GET)
  3. 使用@RequestParam進(jìn)行控制器接口優(yōu)化:
@RestController
public class GreetingController {

	private static final String template = "Hello, %s!";
	private final AtomicLong counter = new AtomicLong();

	@GetMapping("/greeting")
	public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name) {
		return new Greeting(counter.incrementAndGet(), String.format(template, name));
	}
}
  1. @RequestParam將查詢參數(shù)name的值綁定到greeting方法的name參數(shù)上,如果name參數(shù)值為空,默認(rèn)值使用“World”。
  2. 返回的Greeting轉(zhuǎn)化為json格式的數(shù)據(jù),不用我們處理,Spring框架自帶的消息轉(zhuǎn)換器
    MappingJackson2HttpMessageConverter自動(dòng)轉(zhuǎn)換Greeting為JSON。
  3. 官方代碼中使用了AtomicLong這個(gè)類用來(lái)實(shí)現(xiàn)主鍵自增。
  4. @SpringBootApplication包含了@Configuration、@EnableAutoConfiguration、@ComponentScan。
  5. main()方法使用SpringApplication.run()方法去運(yùn)行程序。



本文僅代表作者觀點(diǎn),版權(quán)歸原創(chuàng)者所有,如需轉(zhuǎn)載請(qǐng)?jiān)谖闹凶⒚鱽?lái)源及作者名字。

免責(zé)聲明:本文系轉(zhuǎn)載編輯文章,僅作分享之用。如分享內(nèi)容、圖片侵犯到您的版權(quán)或非授權(quán)發(fā)布,請(qǐng)及時(shí)與我們聯(lián)系進(jìn)行審核處理或刪除,您可以發(fā)送材料至郵箱:service@tojoy.com