6

I´m trying to send a POST request that is an Person object that contains a list of contacts. But I don´t know if this is the correct syntax to send a list:

 {
    "name":"romulo",
    "contacts" :  [ 
        { 
            "contact" : "3466577" 

        },
            { 
            "contact" : "532423" 

            }
        ]
    }

But keeps me returning a 404 error

what i´m doing wrong?

post method:

@PostMapping("/person")
public void addPerson(@Valid @RequestBody Person person) {
    Person savedPerson = personRepository.save(person);
    List<Contact> contacts = person.getContacts();
    for (Contact contact1 : contacts) {
        contactRepository.save(contact1);
    }

}
Vy Do
  • 46,709
  • 59
  • 215
  • 313
Rômulo Sorato
  • 1,570
  • 5
  • 18
  • 29
  • Well is there any documentation on this API, and, if so, have you checked it? – Tim Biegeleisen Feb 25 '18 at 03:07
  • I think it must return `ResponseBody`, current method return nothing, therefore 404 error is rationale. – Vy Do Feb 25 '18 at 03:11
  • It´s a test that i´m trying to do:Contact List Create a REST API which will store people and their contacts. A person can have multiple contacts such as phone, email or whatsapp. The API should allow to create, update, get and delete both the people and the contacts. – Rômulo Sorato Feb 25 '18 at 03:12
  • I tried to return a Person object, but nothing has changed.I am mapping something wrong? – Rômulo Sorato Feb 25 '18 at 03:19
  • We need more details about what address are you trying to call. Spring returns 404 when the endpoint is not found. – Dherik Aug 22 '19 at 18:05

2 Answers2

5

In My Case it was simple mistake and spend an hour for figuring it out that i had space at the end of my url.

Ajay Kopperla
  • 199
  • 2
  • 5
4

HTTP 404 is returned when the server is unable to find method to match your exact request.

For the mentioned request have the url as http://<context>/requestpath with request method as POST.(http://localhost:8080/person)

Check the request body and all the fields should exactly match the Person object else it may return HTPP 400.

Chaitanya
  • 121
  • 2
  • 1
    Hi there,thanks for the answer.The correct url is :http://localhost:8080/api/person because of the base url mapping @RestController @RequestMapping("/api") public class ContactController. i tried to put all the fields but is still not working: { "personId": 1 "name":"romulo", "contacts" : [ { "id":1 "contact" : "3466577" }, { "contact" : "532423" } ] } – Rômulo Sorato Feb 25 '18 at 18:31