spring boot 第一个例子

朴灿烈づ我的快乐病毒、 2022-09-24 12:20 332阅读 0赞

spring boot是一个非常好的框架,在这个例子中我感觉到他的简单,配置的简单。
因为我是用maven进行开发的,所以使用的spring boot的maven方式。
pom.xml:

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  2. <modelVersion>4.0.0</modelVersion>
  3. <groupId>com.sunhuwh.springboot</groupId>
  4. <artifactId>spring-boot-demo</artifactId>
  5. <packaging>jar</packaging>
  6. <version>0.0.1-SNAPSHOT</version>
  7. <name>spring-boot-demo Maven Webapp</name>
  8. <url>http://maven.apache.org</url>
  9. <properties>
  10. <java.version>1.8</java.version>
  11. </properties>
  12. <!-- Inherit defaults from Spring Boot -->
  13. <parent>
  14. <groupId>org.springframework.boot</groupId>
  15. <artifactId>spring-boot-starter-parent</artifactId>
  16. <version>1.4.1.BUILD-SNAPSHOT</version>
  17. </parent>
  18. <!-- Add typical dependencies for a web application -->
  19. <dependencies>
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-web</artifactId>
  23. </dependency>
  24. </dependencies>
  25. <!-- Package as an executable JAR -->
  26. <build>
  27. <plugins>
  28. <plugin>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-maven-plugin</artifactId>
  31. <executions>
  32. <execution>
  33. <goals>
  34. <goal>repackage</goal>
  35. </goals>
  36. </execution>
  37. </executions>
  38. </plugin>
  39. </plugins>
  40. </build>
  41. <!-- Allow access to Spring milestones and snapshots -->
  42. <!-- (you don't need this if you are using anything after 0.5.0.RELEASE) -->
  43. <repositories>
  44. <repository>
  45. <id>spring-snapshots</id>
  46. <url>http://repo.spring.io/snapshot</url>
  47. <snapshots><enabled>true</enabled></snapshots>
  48. </repository>
  49. <repository>
  50. <id>spring-milestones</id>
  51. <url>http://repo.spring.io/milestone</url>
  52. </repository>
  53. </repositories>
  54. <pluginRepositories>
  55. <pluginRepository>
  56. <id>spring-snapshots</id>
  57. <url>http://repo.spring.io/snapshot</url>
  58. </pluginRepository>
  59. <pluginRepository>
  60. <id>spring-milestones</id>
  61. <url>http://repo.spring.io/milestone</url>
  62. </pluginRepository>
  63. </pluginRepositories>
  64. </project>

Application:启动

  1. import org.springframework.boot.SpringApplication;
  2. import org.springframework.boot.autoconfigure.SpringBootApplication;
  3. //@Configuration
  4. //@EnableAutoConfiguration
  5. //@ComponentScan
  6. @SpringBootApplication//简化了上面的
  7. public class Application {
  8. public static void main(String[] args) {
  9. SpringApplication.run(Application.class, args);
  10. }
  11. }

web:使用@RestController+@RequestMapping就可以达到开发RESTful web services的目的,response只会一直通过response body发送。

  1. import org.springframework.beans.factory.annotation.Autowired;
  2. import org.springframework.web.bind.annotation.RequestMapping;
  3. import org.springframework.web.bind.annotation.RestController;
  4. import com.sunhuwh.springboot.service.DemoService;
  5. @RestController
  6. @RequestMapping
  7. public class IndexController {
  8. @Autowired
  9. private DemoService demoService;
  10. @RequestMapping("/")
  11. public String index(){
  12. demoService.test();
  13. return "hello world!";
  14. }
  15. }

发表评论

表情:
评论列表 (有 0 条评论,332人围观)

还没有评论,来说两句吧...

相关阅读