挨拶、名前を追記しました。
1 parent f5ac297 commit a88e6af5a0a0d86323b18f9811aab01793f9a6fa
shindominato authored on 8 May 2020
Showing 1 changed file
View
46
src/main/java/org/ntlab/hellorest/resources/CommentsShindoRest.java 0 → 100644
package org.ntlab.hellorest.resources;
 
import org.springframework.stereotype.Component;
 
import javax.ws.rs.*;
import java.util.ArrayList;
 
@Component
@Path("/comments-shindo")
public class CommentsShindoRest {
ArrayList<String> comments = new ArrayList<>(); // 文字列の可変長リスト
 
@GET
public String getComments() { // すべてのコメントを取得
String result = "";
for (String comment: comments) { // 拡張for文
result = result + comment + "<br>";
}
return result;
}
 
@POST
public void addComment(@FormParam("comment") String comment) { // 新規コメントを追加
comments.add(comment);
}
 
@DELETE
public void clearComments() { // すべてのコメントを削除
comments.clear();
}
 
@Path("/{cId}")
@GET
public String getComment(@PathParam("cId") String cId) { // 指定したコメントを取得
int id = Integer.parseInt(cId);
return comments.get(id);
}
 
@Path("/{cId}")
@PUT
public void addComment(@PathParam("cId") String cId, @FormParam("comment") String comment) { // 指定したコメントを上書き
int id = Integer.parseInt(cId);
comments.set(id, comment);
}
}