すべて0から作成するには覚えることが多いREST-APIを、頭を使わず、いろんなサイトからネタを寄せ集めてささっと作成する方法をメモ。
環境
Java12
Intellij
Spring Initializr
Initializr generates spring boot project with just what you need to start quickly!
Spring Initializrからプロジェクトをダウンロード。
REST-APIで使いそうな依存関係をチョイス。Spring Web Starter
だけ選べばRESTは作れる。
Generate the project
をクリックしてzipでダウンロード。
ダウンロードしたら解凍してIntellijで開く。
SpringBootのRESTのガイドライン
This guide walks you through the process of creating a "hello world" RESTful web service with Spring.
ここに載っているソースを参考にする。
ここに記載があるgitのプロジェクトをベースとしてRESTを作成してもいいんだが、、
Spring Initializr
のほうが余計なものを含んでなかったので分かりやすかった。。
参考にするクラス(ApplicationクラスはInitializrに含まれているので作成する必要はない。)
- Greeting
- GreetingController
で、自作した2クラス。
public class User {
private String name;
private String age;
private String sex;
public User(String name, String age, String sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
public String getName() {
return this.name;
}
public String getAge() {
return this.age;
}
public String getSex() {
return this.sex;
}
}
@RestController
public class UserController {
@RequestMapping("/user")
public User test(String name, String age, String sex) {
return new User(name, age, sex);
}
}
GET
リクエストで/user
を作成する。
パラメータでname
、age
、sex
を指定できる。
@RestController
や@RequestMapping
などのアノテーションの説明はこちらが非常に参考になります。
https://qiita.com/tag1216/items/3680b92cf96eb5a170f0
起動と動作確認
作成したら、IntellijのgradleからbootRun
する。
0:30:12: Executing task 'bootRun'...
> Task :compileJava UP-TO-DATE
> Task :processResources UP-TO-DATE
> Task :classes UP-TO-DATE
> Task :bootRun
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.7.RELEASE)
~中略~
2019-08-21 00:30:17.900 INFO 12736 --- [ restartedMain] .w.s.a.s.AnnotationActionEndpointMapping : Supporting [WS-Addressing August 2004, WS-Addressing 1.0]
2019-08-21 00:30:17.900 INFO 12736 --- [ restartedMain] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.hateoas.config.HateoasConfiguration' of type [org.springframework.hateoas.config.HateoasConfiguration$$EnhancerBySpringCGLIB$$1ba530a3] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-08-21 00:30:19.097 INFO 12736 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2019-08-21 00:30:19.142 INFO 12736 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-08-21 00:30:19.143 INFO 12736 --- [ restartedMain] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.22]
2019-08-21 00:30:19.422 INFO 12736 --- [ restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-08-21 00:30:19.422 INFO 12736 --- [ restartedMain] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 3794 ms
2019-08-21 00:30:20.899 INFO 12736 --- [ restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-08-21 00:30:22.000 INFO 12736 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2019-08-21 00:30:22.144 INFO 12736 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2019-08-21 00:30:22.155 INFO 12736 --- [ restartedMain] com.example.demo.DemoApplication : Started DemoApplication in 7.223 seconds (JVM running for 8.169)
起動を確認したらブラウザから作成したAPI/user
を叩いてみる。
引数を&
でくっつける。
http://localhost:8080/user/?name=testUser&age=20&sex=female
画面にUser
クラスがJSON
形式で表示されました