--Create a web api that just returns a string with spring boot
--You can easily create a template of spring boot with spring initializr
com.example
--Artifact: Project name. This time ʻapi--Options: No touch --Dependencies: Dependent libraries can be added to
build.gradle`
This time add the following
--Spring Web Starter: Basic packages required for web api
--Lombok: A library packed with useful features often used in java
Reference: Notes when touching lombokGenerate the Project
and download the zip.
├── HELP.md
├── README.md
├── build.gradle
├── gradle
│ └── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── settings.gradle
└── src
├── main
│ ├── java
│ │ └── com
│ │ └── example
│ │ └── api
│ │ └── ApiApplication.java
│ └── resources
│ ├── application.properties
│ ├── static
│ └── templates
└── test
└── java
└── com
└── example
└── api
└── ApiApplicationTests.java
--Set up the IDE so that you can edit the zip you just created with Intellij IDEA.
gradle
from ʻImport project from external model`Gradle JVM
is Java 12command
+;
and open the project settings
Select 12 for each of Project SDK
and Project language level
and savebuild.gradle
--Project dependencies are defined in build.gradle
build.gradle
plugins {
id 'org.springframework.boot' version '2.1.7.RELEASE'
id 'java'
}
apply plugin: 'io.spring.dependency-management'
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
--plugins
, ʻapply plugin: Plugin settings --
java: You will be able to perform tasks such as compiling java source code and unit testing. --ʻOrg.springframework.boot
: You will be able to perform spring boot tasks such as bootRun.
--ʻIo.spring.dependency-management: Provides maven BOM functionality (which resolves dependent library versions) not found in gradle --
group,
version: Project settings --
suceCompatibility: Specify the version of the JDK used in the project --Change from
1.8 to
12 to use Java 12 this time. --
repositories: Specify the repository to get the dependent libraries --
dependencies: Definition of external dependencies. The
Spring Web Starter specified in initializr is reflected here. --ʻImplementation
: Used at compile time and run time
--testImplementation
: Used at compile time and run time during testing
--Right-click ʻApiApplication` and execute --The following log will be output and Spring boot will start.
23:07:15: Executing task 'ApiApplication.main()'...
> Task :compileJava
> Task :processResources
> Task :classes
> Task :ApiApplication.main()
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.7.RELEASE)
2019-08-06 23:07:32.447 INFO 20662 --- [ main] com.example.api.ApiApplication : Starting ApiApplication on xxxnoMacBook-Air.local with PID 20662 (/Users/xxx/dev/spring-boot-api/build/classes/java/main started by xxx in /Users/xxx/dev/spring-boot-api)
2019-08-06 23:07:32.452 INFO 20662 --- [ main] com.example.api.ApiApplication : No active profile set, falling back to default profiles: default
2019-08-06 23:07:35.060 INFO 20662 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2019-08-06 23:07:35.118 INFO 20662 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-08-06 23:07:35.119 INFO 20662 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.22]
2019-08-06 23:07:35.309 INFO 20662 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-08-06 23:07:35.309 INFO 20662 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2746 ms
2019-08-06 23:07:35.680 INFO 20662 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-08-06 23:07:36.278 INFO 20662 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2019-08-06 23:07:36.292 INFO 20662 --- [ main] com.example.api.ApiApplication : Started ApiApplication in 4.996 seconds (JVM running for 5.699)
--Access http: // localhost: 8080
with a browser, and if the following screen is displayed, it is OK.
--I created a package called controller
and created a class called HelloWorldController
under it.
--String returns hello world!
when a GET request comes in with the path / hello
--The method name is getHello
, but anything is fine.
HelloWorldController
package com.example.api.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("hello")
public class HelloWorldController {
@RequestMapping(method = RequestMethod.GET)
public String getHello() {
return "hello world!";
}
}
@RestController
--Annotation attached to Controller class that is the endpoint of REST web api
――The same effect as adding the following two annotations
--@Controller
: Indicates that it is a controller class of MVC
--@ResponseBody
: The return value of the method of the given class becomes the response body as it is.
@RequestMapping()
--Annotation that sets the path to access the REST web api
--method = RequestMethod.XXX (XXX: GET, POST, etc.)
can be assigned to each HTTP method.
--Restart the application and access http: // localhost: 8080 / hello
in your browser
--If hello world!
Is displayed on the white screen, it's OK!
Recommended Posts