自定义回复

import javax.ws.rs.*;
import javax.ws.rs.core.Response;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Path("/alphabet/{letter}")
public class AlphabetResource {

    private final Map<String, String> alphabet;
    
    public AlphabetResource() {
        this.alphabet = new HashMap<>();
        this.alphabet.put("A", "Apple");
    }
    
    @GET
    public Response get(@PathParam("letter") String letter) {
        if (alphabet.containsKey(letter)) {
            return Response.ok(alphabet.get(letter)).build();
        } else {
            return Response.status(Response.Status.NOT_FOUND).build();
        }
    }
    
    @PUT
    public Response put(@PathParam("letter") String letter, String value) {
        if (alphabet.containsKey(letter)) {
            return Response.status(Response.Status.CONFLICT).build();
        } else {
            alphabet.put(letter, value);
            return Response.noContent().build();
        }
    }
    
    @POST
    public Response post(@PathParam("letter") String letter, String value) {
        if (alphabet.containsKey(letter) && Objects.equals(alphabet.get(letter), value)) {
            return Response.notModified().build();
        } else {
            alphabet.put(letter, value);
            return Response.noContent().build();
        }
    }
}

可以使用以下 curl 命令调用它们

curl -v -X GET http://localhost:8080/alphabet/A
curl -v -X PUT http://localhost:8080/alphabet/A -d "Avacado"
curl -v -X PUT http://localhost:8080/alphabet/B -d "Banana"
curl -v -X POST http://localhost:8080/alphabet/A -d "Apple"
curl -v -X POST http://localhost:8080/alphabet/A -d "Avacado"