0

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?

A.Gautam
  • 35
  • 4
  • We should be using RequestMethod.POST insted of RequestMethod.GET – Vipul Gulhane Sep 16 '18 at 10:45
  • use browser inspection mode,, the while clicking submit button check browser console what request is been made.. – Sayantan Mandal Sep 16 '18 at 11:47
  • See https://stackoverflow.com/a/36600434/2587435 about the default /error mapping and the 404, per [your comment](https://stackoverflow.com/questions/52353227/unable-to-send-post-request-in-springmvc#comment91651582_52353284) – Paul Samsotha Sep 17 '18 at 16:51

3 Answers3

2

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";
    }
kj007
  • 6,073
  • 4
  • 29
  • 47
  • Hey, I'm still getting same error, changed request method to POST. Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback. Sun Sep 16 16:27:58 IST 2018 There was an unexpected error (type=Internal Server Error, status=500). 404 null – A.Gautam Sep 16 '18 at 10:59
  • sorry, leave it GET for now and can you change restTemplate.postForObject instead of restTemplate.postForEntity – kj007 Sep 16 '18 at 11:06
  • Yes, changed to restTemplate.postForEntity but still same error – A.Gautam Sep 16 '18 at 11:12
  • hmm, is your controller is called(did you debug)?? just check by changing to application/json from @Produces(MediaType.TEXT_PLAIN) or remove it for default behavior, is it possible for you to share your demo code on github, i can check.. – kj007 Sep 16 '18 at 11:16
  • Can you try my updated response if not works then please share code on github, I will check – kj007 Sep 16 '18 at 11:27
  • check it out https://github.com/gautam2705/JerseyAPI https://github.com/gautam2705/clientToCallAPI – A.Gautam Sep 16 '18 at 11:37
  • @A.Gautam Check my updated answer, you should be able to fix it by this. – kj007 Sep 16 '18 at 14:22
1

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?

  • Hey, 1. changed GET to POST, but that didn't resolve the issue. 2. Okay i'm going to try this, will update you shortly. 3. But this route works as expected if I send POST request using postman. – A.Gautam Sep 16 '18 at 12:21
  • I suggest putting all your restful service on a restful Controller to make it easier to return a JSON format response – Ahmad Ababneh Sep 16 '18 at 13:20
0

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;
    }
Vipul Gulhane
  • 761
  • 11
  • 16