使用 gradle 構建工具創建了簡單的 spring boot rest api
package com.example.demosb;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemosbApplication {
public static void main(String[] args) {
SpringApplication.run(DemosbApplication.class, args);
}
}
我的控制器類
package com.example.demosb;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class demoSbController {
@GetMapping("/welcome")
public ResponseEntity<String> welcome() {
return ResponseEntity.ok("Welcome To demo !");
}
}
build.gradle 中的依賴項
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-jetty'
runtimeOnly 'org.apache.derby:derby'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
implementation 'org.springframework.boot:spring-boot-starter-security'
在 url 上:http://localhost:8888 顯示登錄頁面,其中我提供用戶名:用戶和密碼是 ,這會導致 404 錯誤,因為路徑“/”上沒有請求映射,這是可以的
但是對于 url:http://localhost:8888/welcome,仍然收到 404 錯誤,其中預期是回應物體 json 物件,訊息為:“歡迎觀看演示!”
uj5u.com熱心網友回復:
在依賴項中添加的是 JPA 和 Derby。洗掉它們,看看它現在是否有效。必須配置與資料庫的連接。
uj5u.com熱心網友回復:
它現在有效,將在這方面回復進展
在我的 build.gradle 中,從 spring-web 依賴項和configuration.all 任務中排除了與 tomcat 相關的依賴項,因為想要使用 jetty 作為嵌入式服務器
...
implementation('org.springframework.boot:spring-boot-starter-web')
{
exclude module: 'spring-boot-starter-tomcat'
}
...
configurations.all {
exclude module: 'spring-boot-starter-tomcat'
exclude group: 'org.apache.tomcat'
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/362658.html
