Newer
Older
JerseyExercise / src / main / java / com / example / jerseyexercise / resources / NNitta.java
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("nnitta")
@Component
public class NNitta {
    String name = "noname";
    ArrayList<String> tweetList = new ArrayList<>();

    @GET
    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)
    public void tweet(@FormParam("tweet") String tweet) {
        tweetList.add(tweet);
    }

    @GET
    @Path("/tweets")
    @Produces(MediaType.APPLICATION_JSON)
    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;
    }

}