【问题标题】:Unable to send POST request in SpringMVC无法在 Spring MVC 中发送 POST 请求
【发布时间】:2018-09-16 10:35:24
【问题描述】:

我使用 jersey 和 spring boot 创建了一个 API。现在,当我在请求正文中使用 Postman 发出 POST 请求时:

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

它有效。保存此数据的函数是:

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

员工模型是:

@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

这个api是由客户端下面的函数调用的:

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

员工信息类是:

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

我得到的错误是:

  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]

还有一长串这样的清单。

调用它的形式是:

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

编辑:在将客户端的方法 = RequestMethod.GET 更改为 RequestMethod.POST 时,没有任何反应,仍然出现相同的错误 我做错了什么?

【问题讨论】:

  • 我们应该使用 RequestMethod.POST 的 RequestMethod.GET
  • 使用浏览器检查模式,点击提交按钮时检查浏览器控制台发出了什么请求..
  • 参见stackoverflow.com/a/36600434/2587435,了解默认的 /error 映射和 404,每个 your comment

标签: java spring spring-mvc spring-boot jersey


【解决方案1】:

在查看您的代码问题后,您的后端运行在 8090 端口上的客户端应用程序中,而在 api 中您调用的 addEmployee 使用的是 8080。

将此String url = "http://localhost:8080/api/addEmployee"; 更改为String url = "http://localhost:8090/api/addEmployee";,您应该会很好。

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

【讨论】:

  • 嘿,我仍然遇到同样的错误,将请求方法更改为 POST。 Whitelabel 错误页面 此应用程序没有针对 /error 的显式映射,因此您将其视为后备。 Sun Sep 16 16:27:58 IST 2018 出现意外错误(类型=内部服务器错误,状态=500)。 404 空
  • 抱歉,暂时不要使用 GET,您可以更改 restTemplate.postForObject 而不是 restTemplate.postForEntity
  • 是的,改成 restTemplate.postForEntity 但还是一样的错误
  • 嗯,你的控制器被调用了吗(你调试过)??只需通过从 @Produces(MediaType.TEXT_PLAIN) 更改为 application/json 来检查或将其删除以获取默认行为,您是否可以在 github 上共享您的演示代码,我可以检查..
  • 你能试试我更新的回复,如果不行,请在github上分享代码,我会检查
【解决方案2】:

404 表示请求的源不存在,可能是因为以下原因之一:

  1. 没有控制器方法来处理 POST 请求,所以尝试将 which@RequestMapping(value = "/addEmployee", method = RequestMethod.GET) 更改为 @RequestMapping(value = "/addEmployee", method = RequestMethod.POST)

  2. 尝试使用此注解@RestController移动此方法并使其成为restful控制器中的服务

  3. 我看到您正在访问这个我认为配置不正确的 /api/addEmployee?

【讨论】:

  • 嘿,1. 将 GET 更改为 POST,但这并没有解决问题。 2.好的,我要试试这个,很快就会更新你。 3. 但是如果我使用邮递员发送 POST 请求,这条路线会按预期工作。
  • 我建议把你所有的restful服务放在一个restful Controller上,这样更容易返回JSON格式的响应
【解决方案3】:

我们应该使用 RequestMethod.POST 而不是 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;
    }

【讨论】:

  • 这里是完整的例子journaldev.com/3358/…
  • 嘿,我将方法类型更改为 POST,但仍然出现相同的错误。
猜你喜欢
  • 1970-01-01
  • 2021-08-09
  • 1970-01-01
  • 1970-01-01
  • 2020-05-09
  • 1970-01-01
  • 2022-01-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多