我正試圖讓一個簡單的Spring Boot運行。如果我在main中把它設定為HelloWorldRest1Application.class,它就可以運行并作業。一旦我把它改為HelloWorld,我就會在localhost上看不到它了。
2021-09-19 19:36:40.009 INFO 1664 --- [ restartedMain ] c.sw409.demo.HelloWorldRest1Application : Starting HelloWorldRest1Application在DESKTOP-55I895V上使用Java 16.0.1,PID為1664。 PID 1664 (C:Users CostDesktopFall21Advanced JavajavaHelloWorldRest1 argetclasses由ncost啟動,在 C:用戶 costDesktopFall 21Advanced JavajavaHelloWorldRest1) 2021-09-19 19:36:40.013 INFO 1664 --- [ restartedMain ] c.sw409.demo.HelloWorldRest1Application : 沒有設定活動組態檔。 回落到默認組態檔:默認 2021-09-19 19:36:40.045 INFO 1664 --- [restartedMain]. .e.DevToolsPropertyDefaultsPostProcessor : Devtools屬性默認值 激活! 將'spring.devtools.add-properties'設定為'false'來禁用。 2021-09-19 19:36:40.160 INFO 1664 --- [ restartedMain ] c.sw409.demo.HelloWorldRest1Application : 開始了 HelloWorldRest1Application在0.52秒內完成(JVM運行了1.366)
。
主程式
package com.sw409.demo。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.sw409.HelloWorldRest.models.HelloWorldBean;
import com.sw409.HelloWorldRest.services.HelloWorld。
@SpringBootApplication[/span
public class HelloWorldRest1Application {
public static void main(String[] args) {
SpringApplication.run(HelloWorld.class, args)。
}
HelloWorldBean
package com.sw409.HelloWorldRest.models。
public class HelloWorldBean {
字串mes。
public HelloWorldBean(String str) {
this.mes = str;
}
public String getMes() {
return mes。
}
public void setMes(String mes) {
this.mes = mes;
}
public String toString() {
return mes;
}
}
HelloWorld
package com.sw409.HelloWorldRest.services。
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import com.sw409.HelloWorldRest.models.HelloWorldBean;
@RestController[/span
public class HelloWorld {
@GetMapping("/hello")
public String helloworld() {
return ("hello world") 。
}
@GetMapping("/hello/{name}")
public String helloworld(@PathVariable("name")String str) {
return "hello" str;
}
@GetMapping("/helloworldbean/{name}")
public HelloWorldBean hello(@PathVariable("name")/span> String str) {
return new HelloWorldBean("大家好!" str)。
}
}
uj5u.com熱心網友回復:
將HelloWorldRest1Application移到包com.sw409中,并將HelloWorldRest1Application設為你的主類,如下:
package com.sw409;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.sw409.HelloWorldRest.models.HelloWorldBean;
import com.sw409.HelloWorldRest.services.HelloWorld。
@SpringBootApplication[/span
public class HelloWorldRest1Application {
public static void main(String[] args) {
SpringApplication.run(HelloWorldRest1Application.class, args) 。
}
@SpringBootApplication封裝了@Configuration、@EnableAutoConfiguration和@ComponentScan注釋的默認屬性。@ComponentScan的默認值意味著@ComponentScan所使用的包上的所有子包都被掃描。這就是為什么在專案的基礎包中包含主類通常是一個很好的做法。
。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/324180.html
標籤:
