Create Spring Boot application

Keywords: Programming Maven Spring Apache Java

Using development tools: Spring Tool Suite 4

Create a Maven project

  1. Select File - > New - > others - > Maven - > Maven project

    • Create a simple project(skip archetype selection)
    • Use default Workspace location
    • Add project(s) to working set
  2. Choose project type: usually choose to build Maven archetype QuickStart (non web project) model or Maven archetype webapp (Web project).

  3. Fill in project parameters (Group Id, Artifact Id, Version, etc.)

  4. The project structure is as follows

  5. Modify pom.xml file

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.wljkzx.wcwoa</groupId>
  <artifactId>ams</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>ams</name>
  <url>http://maven.apache.org</url>
  
  <parent>
	  <groupId>org.springframework.boot</groupId>
	  <artifactId>spring-boot-starter-parent</artifactId>
	  <version>2.1.6.RELEASE</version>
	  <relativePath /> <!-- lookup parent from repository -->
  </parent>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
	<java.version>1.8</java.version>
  </properties>

  <dependencies>
  	<!-- spring-boot-starter-* -->
  	<dependency>
  		<groupId>org.springframework.boot</groupId>
  		<artifactId>spring-boot-starter-web</artifactId>
  	</dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

Write test code

  1. Write a simple Controller
package org.wljkzx.wcwoa.ams.Controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

	@RequestMapping("/hello")
	public String hello() {
		return "Hello Spring Boot!";
	}

}
  1. Modify Maven's default App class
package org.wljkzx.wcwoa.ams;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Hello world!
 *
 */
@SpringBootApplication
public class App {
	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}
}
  1. Launch project: use the right-click shortcut menu to run the main method

  2. Enter http://localhost:8080/hello access request in browser

Posted by Dennis Madsen on Tue, 29 Oct 2019 12:15:50 -0700