При выполнении примера с Rest API c сайта Oracle, при запросе @PUT
выпадает ошибка :
Can not construct instance of java.util.UUID from String value '3136': not a valid textual representation, problem: UUID has to be represented by the standard 36-char rep resentation at [Source: org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream@628b3b; line: 1, column: 2] (through reference chain: Note["id"])
Вот сам код:
Note.java
public class Note {
private final UUID id;
private final String title;
private Note(NoteBuilder builder){
this.id = builder.id;
this.title = builder.title;
}
public Note(){
Note not = new Note.NoteBuilder().id().build();
this.id = not.getId();
this.title = not.getTitle();
}
public Note(UUID id, String title) {
Note not = new Note.NoteBuilder().id()
.title(title)
.build();
this.id = not.getId();
this.title = not.getTitle();
}
public UUID getId(){
return this.id;
}
public String getTitle() {
return this.title;
}
@Override
public String toString(){
return "ID: " + id
+ " Title: " + title;
}
public static class NoteBuilder{
private UUID id;
private String title = "";
public NoteBuilder id(){
this.id = UUID.randomUUID();
return this;
}
public NoteBuilder title(String title){
this.title = title;
return this;
}
public Note build(){
return new Note(this);
}
}
}
NoteList.java
public class NoteList {
private static final CopyOnWriteArrayList<Note> nList = new CopyOnWriteArrayList<>();
static {
nList.add(
new Note.NoteBuilder().id()
.title("1 заметка")
.build()
);
nList.add(
new Note.NoteBuilder().id()
.title("2 заметка")
.build()
);
}
private NoteList() {
}
public static CopyOnWriteArrayList<Note> getInstance(){
return nList;
}
}
NoteService.java
@Path("/notes")
public class NoteService {
private final CopyOnWriteArrayList<Note> nList = NoteList.getInstance();
@GET
@Path("/all")
@Produces(MediaType.APPLICATION_JSON)
public String getAll(){
Gson gson = new Gson();
return gson.toJson(nList);
}
@POST
@Path("/add")
@Produces(MediaType.APPLICATION_JSON)
public Response addNote(Note note){
nList.add(note);
return Response.status(201).build();
}
@DELETE
@Path("/remove/{id}")
public Response deleteNote(@PathParam("id") UUID id){
Predicate<Note> note = n -> n.getId().equals(id);
if (nList.removeIf(note)) {
return Response.status(Response.Status.OK).build();
}else {
return Response.status(Response.Status.NOT_FOUND).build();
}
}
@PUT
@Path("{id}/update")
@Produces(MediaType.APPLICATION_JSON)
public Response updateNote(Note note){
int matchIdx = 0;
Optional<Note> match = nList.stream()
.filter(n -> n.getId().equals(note.getId()))
.findFirst();
if (match.isPresent()) {
matchIdx = nList.indexOf(match.get());
nList.set(matchIdx, note);
return Response.status(Response.Status.OK).build();
} else {
return Response.status(Response.Status.NOT_FOUND).build();
}
}
При запросе @GET
выводится следующее:
[{"id": "313695e0-72a5-4aab-9047-fbec516dab4f", "title":"1 заметка", "id": "9e4b9d09-347b-439c-aecf-b259b086f987","title":"2 заметка"}]
Правда почему то в не в виде дерева. Но тем не менее, это всё равно объект JSON
, как я понимаю.
Запрос @PUT
подаю через сURL в таком виде:
curl -X PUT -i -H "Content-Type:application/json" -d "{\"id\":313695e0-72a5-4aab-9047-fbec516dab4f,\"title\":\"новая заметка\"}" http://localhost:8080/test_war_exploded/notes/313695e0-72a5-4aab-9047-fbec516dab4f/update
Сервер выдаёт
400 Bad Request
, т.е. была синтаксическая ошибка в запросе. Пытается создать объект UUID из первых четырёх цифр моего id в виде String. Причём здесь String собственно? Почему система преобразовывает из UUID в String. Или надо как-то по-другому писать запрос?
"{\"id\":313695e0-...
? У оракла там кавычек нет, правда у них id типа int, а у меня объект UUID