Newer
Older
JerseyExercise / src / main / java / com / example / jerseyexercise / resources / NMrest.java
package com.example.jerseyexercise.resources;

import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;
import org.springframework.stereotype.Component;

import javax.print.attribute.standard.Media;
import java.util.HashMap;
import java.util.Set;

@Path("/NMrest")
@Component
public class NMrest {
    private HashMap<String, String> accounts = new HashMap<>();//キー、バリュー
    @GET
    @Produces(MediaType.APPLICATION_JSON)//returnの形をどうするか
    public Set<String> getAccounts(){
        return accounts.keySet();
    }

    @POST
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    public void signup(@FormParam("id") String id, @FormParam("name") String name){
        accounts.put(id,name);//1行分表に追加される
    }

    @Path("/{id}")//続き
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getName(@PathParam("id") String id){
        return accounts.get(id);
    }

    @Path("/{id}")
    @PUT
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    public void setName(@PathParam("id") String id, @FormParam("name") String name){
        if (accounts.get(id) == null) return;
        accounts.put(id,name);
    }

}