package com.example.jerseyexercise.resources;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
@Path("shatai")
@Component
public class SHatai {
String name = "hatai";
ArrayList<String> tweetList = new ArrayList<>(); //左側はインスタンス add(x), get(idx), size(), remove(idx)
@GET
//@Produces(MediaType.TEXT_PLAIN)
public String getHelloWorld() {
return "Hello World!";
}
//ここから下に子メソットを書いていく
@GET
@Path("/name")
public String getName() {
return name;
}
@PUT
@Path("/name")
public void setName(@FormParam("name") String newName) {
name = newName;
}
@POST
@Path("/tweets")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED) //送ったパラメーターが消費される post manのリクエストbodyと対応
public void tweet(@FormParam("tweet") String tweet) {
tweetList.add(tweet);
}
@GET
@Path("/tweets")
@Produces(MediaType.APPLICATION_JSON) //戻り値をどういう形で返すのか 引数がvoidまたはstring以外なのでつけることができる
public ArrayList<String> getTweets() {
return tweetList;
}
@GET
@Path("/tweets/{no}")// {} ← これはパスパラメーター
@Produces(MediaType.APPLICATION_JSON)
public String getTweet(@PathParam("no") int n) {
String t = tweetList.get(n);
return t;
}
}