简体   繁体   中英

Unable to send POST request in SpringMVC

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.

Form which calls this is:

<form name="myform" method="post" action="addEmployee" >
        <input type="submit" value="Save">
</form>

EDIT: On changing client side's method = RequestMethod.GET to RequestMethod.POST, nothing happens, still getting same erro What I'm doing wrong?

After reviewing your code problem is at client side app where your back end is running on 8090 port while in api you calling is having 8080 for addEmployee.

Change this String url = "http://localhost:8080/api/addEmployee"; to String url = "http://localhost:8090/api/addEmployee"; and you should be good.

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



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

        EmployeeInfo employee = new EmployeeInfo();
        employee.setName("Ashish");
        employee.setEmail("anyhing");
        employee.setDateOfBirth("mybirthday");
        employee.setLocation("home");

        System.out.println("WE HAVE REACHED HERE");
        String response = restTemplate.postForObject(url, employee, String.class);
        System.out.println(response);

        return "redirect:/home";
    }

The 404 means that the requested source does not exist maby because one of these reasons:

  1. There is no controller method to handle the POST request so try to change the which @RequestMapping(value = "/addEmployee", method = RequestMethod.GET) to @RequestMapping(value = "/addEmployee", method = RequestMethod.POST)

  2. try to move this method and make it a service in a restful controller using this annotation @RestController

  3. I see that you are accessing this /api/addEmployee which I think not configured right?

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;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM