REST POST controller saying: Could not read JSON: No content to map due to end-of-input

前端 未结 2 1340
孤独总比滥情好
孤独总比滥情好 2020-12-09 04:24

I\'m doing an integration test against a REST controller POST handler. Well, I\'m trying to.

It gives me the HttpMessageNotReadableException exception: Could not rea

相关标签:
2条回答
  • 2020-12-09 05:03

    In my opinion the problem is with content format. Your endpoint expect that data will be sent as application/json but in test you send it as application/x-www-form-urlencoded (it doesn't matter that you set proper content type header in request). Try to send admin object in json format (as a body of a request):

    {
     "firstname" : "Stephane",
     "lastname" : "Eybert",
     "login" : "stephane",
     "password" : "toto"
    }
    

    BTW the /admin/crud does not fallow REST resource addressing rules, you should change it to /admin. The crud (CREATE, READ, UPDATE, DELETE) will map to HTTP methods (POST, GET, PUT, DELETE)

    0 讨论(0)
  • 2020-12-09 05:12

    I replaced the .param() methods in favor of the .content() one:

            post("/admin/crud").headers(httpHeaders)
            .contentType(MediaType.APPLICATION_JSON)
            .accept(MediaType.APPLICATION_JSON)
            .content("{ \"firstname\" : \"" + admin0.getFirstname() + "\", \"lastname\" : \"" + admin0.getLastname() + "\", \"email\" : \"" + admin0.getEmail() + "\", \"login\" : \"" + admin0.getLogin() + "\", \"password\" : \"" + admin0.getPassword() + "\", \"passwordSalt\" : \"" + admin0.getPasswordSalt() + "\" }")
        ).andDo(print())
        .andExpect(status().isCreated())
        .andExpect(jsonPath("$.firstname").value(admin0.getFirstname()))
        .andExpect(jsonPath("$.lastname").value(admin0.getLastname()))
        .andExpect(jsonPath("$.email").value(admin0.getEmail()))
        .andExpect(jsonPath("$.login").value(admin0.getLogin()))
        .andExpect(jsonPath("$.password").value(admin0.getPassword()))
        .andExpect(jsonPath("$.passwordSalt").value(admin0.getPasswordSalt()))
        .andReturn();
    

    And it now works as expected.

    0 讨论(0)
提交回复
热议问题