| | 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); |
---|
| | } |
---|
| | } |
---|
| | |
---|
| | |