Controller
package com.example.demo import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.PostMapping import org.springframework.web.bind.annotation.PathVariable import org.springframework.web.bind.annotation.RequestBody import org.springframework.web.bind.annotation.RestController @RestController class DemoController() { @GetMapping("/user") fun getUser(): User { val user = User( username = "grahamcox", screenName = "Graham" ) return user } @PostMapping("/user") fun registerUser(@RequestBody user: User): User { return user } }
Userクラス
package com.example.demo import com.fasterxml.jackson.annotation.JsonCreator data class User @JsonCreator constructor( val username: String, val screenName: String )
GETリクエスト
http://localhost:8080/userにアクセスすると,{"username":"grahamcox","screenName":"Graham"}
のようなJSONが返ってくる
POSTリクエスト
curl --data '{"username":"test", "screenName":"mituba"}' -v -X POST -H 'Content-Type:application/json' http://localhost:8080/user
{"username":"test","screenName":"mituba"}
のようなJSONが返ってくる
参考
参考にしました