pom.xml配置
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.9.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
HelloWorldMain.class文件内容(主程序class)
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication //告诉springboot这是主程序
public class HelloWorldMain {
public static void main(String[] args) {
//Spring应用启动起来(该class文件名为HelloWorldMain.class) args为main传进来的参数名
SpringApplication.run(HelloWorldMain.class,args);
}
}
再页面输出HelloWorld
package com.hello;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.io.Writer;
@Controller
public class HelloWorld {
@ResponseBody //表示下面是返回正文
@RequestMapping("/hello") //表示浏览器路径是/hello
public String hello(){
return "HelloWorld!"; //表示返回正文是HelloWorld!
}
}
package com.hello;
import org.springframework.web.bind.annotation.RequestMapping;
import java.io.Writer;
@RestController //@ResponseBody 和@Controller的结合体
public class HelloWorld {
@RequestMapping("/hello") //表示浏览器路径是/hello
public String hello(){
return "HelloWorld!"; //表示返回正文是HelloWorld!
}
}
效果展示
将springboot应用打包,可直接执行,不需要配置tomcat
<!-- 将应用打包成可执行jar-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
使用方法:
在idea左下角点击此处,调出侧边栏
点击maven,再依次点击下面即可
打包后的jar位置