HelloController.java
HelloController.java
package com.example.demo.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController // 该注解为组合注解,等同于Spring中@Controller+@ResponseBody注解
public class HelloController {
@GetMapping("/hello") // 该注解等同于Spring框架中@RequestMapping(RequestMethod.GET)注解
public String hello(){
return "你好,Spring Boot";
}
}
DemoApplication.java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
DemoApplicationTests.java
package com.example.demo;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class DemoApplicationTests {
@Test
void contextLoads() {
}
}
pom.xml
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
运行
控制台输出消息
在浏览器地址栏输入
http://localhost:8080/hello
发表评论