spring-projects / spring-projects/spring-data-rest

Lombok generated constructor not found for deserialization on POST requests [DATAREST-884]

Open
#1,254 5 comments 0 reactions 1 assignee View on GitHub

@odrotbohm is already working on this.

Since Dec 31, 2020.

type: bug type: task
Dominant language
Java
Stars
958
Forks
568
PR merge metrics
No merged PRs in 30d

Description

Kai Tödter opened DATAREST-884 and commented

With Spring Boot 1.3.7 the following is working, with Spring Boot 1.4.0 not anymore:
You find all the examples in this report at https://github.com/toedter/chatty/
Branches:
master -> Spring Boot 1.3.7 => working
Spring-Boot-1.4.0 -> Spring Boot 1.4.0 => not working

Consider 2 REST resources with repositories: User and ChatMessage
ChatMessage has a relation to user:
@ManyToOne
private User author;

With Spring Boot 1.3.7 it was possible to create a new ChatMessage including a relation to an existing user with one POST request, when http://localhost:8080/api/users/toedter_k points to a valid User resource :

curl 'http://localhost:8080/api/messages' -i -X POST -H 'Content-Type: application/hal+json' -d '{"author":"http://localhost:8080/api/users/toedter_k","text":"Hello!"}'

This call gives the following error when using Spring Boot 1.4.0:
o.s.d.r.w.RepositoryRestExceptionHandler : Could not read document: Can not construct instance of com.toedter.chatty.server.boot.user.User: no String-argument constructor/factory method to deserialize from String value ('http://localhost:8080/api/users/toedter_k')

You find the corresponding test (Spring Restdocs) at
https://github.com/toedter/chatty/blob/master/subprojects/com.toedter.chatty.server.boot/src/test/java/com/toedter/chatty/server/boot/ApiDocumentation.java

    @Test
    public void messagesCreateExample() throws Exception {
        Map<String, String> user = new HashMap<String, String>();
        user.put("id", "toedter_k");
        user.put("fullName", "toedter_k");
        user.put("email", "kai@toedter.com");

        String userLocation = this.mockMvc
                .perform(
                        post("/api/users").contentType(MediaTypes.HAL_JSON).content(
                                this.objectMapper.writeValueAsString(user)))
                .andExpect(status().isCreated()).andReturn().getResponse()
                .getHeader("Location");

        Map<String, Object> chatMessage = new HashMap<String, Object>();
        chatMessage.put("text", "Hello!");
        chatMessage.put("author", userLocation);

        this.mockMvc.perform(
                post("/api/messages").contentType(MediaTypes.HAL_JSON).content(
                        this.objectMapper.writeValueAsString(chatMessage))).andExpect(
                status().isCreated())
                .andDo(document("messages-create-example",
                        requestFields(
                                fieldWithPath("text").description("The text of the chat message"),
                                fieldWithPath("author").description("The author of the chat message. This must be the URL to an existing user resource."))));
    }

Affects: 2.5.2 (Hopper SR2), 2.5.3 (Hopper SR3)

1 votes, 4 watchers

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.