Unable to send POST request in SpringMVC

2116 views java
5

I have created an API using jersey and spring boot. Now when I hit POST request using Postman with following in request body:

{
     "name":"something", "email":"something","location":"something","dateOfBirth":"something"
}

It works. Function to save this data is:

@POST
@Path("/addEmployee")
    @Produces(MediaType.TEXT_PLAIN)
    public String addEmployee(@RequestBody Employee employee) {
        service.save(employee);
        return "Saved Successfully";
    }

Employee model is:

@Entity
@Table(name = "employee")
@XmlRootElement(name = "employee")
@EntityListeners(AuditingEntityListener.class)
public class Employee {
    public Employee() {
    }
    @Id
    @Column(nullable = false, name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false, name = "name")
    private String name;

    @Column(nullable = false, name = "email")
    private String email;

    @Column(nullable = false, name = "location")
    private String location;

    @Column(nullable = false, name = "DOB")
    private String dateOfBirth;

    // getters and setters

This api is called by follwing function at client side:

@RequestMapping(value = "/addEmployee", method = RequestMethod.GET)
    public ModelAndView addEmployee(ModelAndView model) {

        RestTemplate restTemplate = new RestTemplate();
        String url = "http://localhost:8080/api/addEmployee";

        EmployeeInfo employee = new EmployeeInfo();
        employee.setName("Ashish");
        employee.setEmail("anyhing");
        employee.setDateOfBirth("mybirthday");
        employee.setLocation("home");
        ResponseEntity<String> response = restTemplate.postForEntity(url, employee, String.class);

        model.setViewName("homePage");
        return model;
    }

Employee info class is:

public class EmployeeInfo {

    private String name;

    private String email;

    private String location;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getEmail() {
        return email;
    }

Error I'm getting is :

  2018-09-16 15:57:13.706 ERROR 14892 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.client.HttpClientErrorException: 404 null] with root cause

org.springframework.web.client.HttpClientErrorException: 404 null
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:86) ~[spring-web-4.3.19.RELEASE.jar:4.3.19.RELEASE]
    at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:708) ~[spring-web-4.3.19.RELEASE.jar:4.3.19.RELEASE]
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:661) ~[spring-web-4.3.19.RELEASE.jar:4.3.19.RELEASE]
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:621) ~[spring-web-4.3.19.RELEASE.jar:4.3.19.RELEASE]
    at org.springframework.web.client.RestTemplate.postForEntity(RestTemplate.java:415) ~[spring-web-4.3.19.RELEASE.jar:4.3.19.RELEASE]
    at com.example.controller.Home.addEmployee(Home.java:82) ~[classes/:na]

and a long list like this.

What I'm doing wrong?

answered question

what is your api rest configuration ? How do you handle your route server side? You're getting a 404 because you get a 404. Problem is located in server side.

We should be using RequestMethod.POST insted of RequestMethod.GET

2 Answers

1

You have defined method GET, should be POST

@RequestMapping(value = "/addEmployee", method = RequestMethod.POST)
    public ModelAndView addEmployee(ModelAndView model) {

posted this
2

We should be using RequestMethod.POST instead of RequestMethod.GET

@RequestMapping(value = "/addEmployee", method = RequestMethod.POST)
    public ModelAndView addEmployee(ModelAndView model) {

        RestTemplate restTemplate = new RestTemplate();
        String url = "http://localhost:8080/api/addEmployee";

        EmployeeInfo employee = new EmployeeInfo();
        employee.setName("Ashish");
        employee.setEmail("anyhing");
        employee.setDateOfBirth("mybirthday");
        employee.setLocation("home");
        ResponseEntity<String> response = restTemplate.postForEntity(url, employee, String.class);

        model.setViewName("homePage");
        return model;
    }

posted this

Have an answer?

JD

Please login first before posting an answer.