簡體   English   中英

無法在SpringMVC中發送POST請求

[英]Unable to send POST request in SpringMVC

我使用球衣和彈簧靴創建了一個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時,什么也沒發生,仍然出現相同的錯誤我在做什么錯了?

檢查完代碼問題后,在客戶端應用程序中,后端應用程序在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";
    }

404表示所請求的源不存在,因為以下原因之一:

  1. 沒有控制器方法來處理POST請求,因此請嘗試將@RequestMapping(value = "/addEmployee", method = RequestMethod.GET)更改為@RequestMapping(value = "/addEmployee", method = RequestMethod.POST)

  2. 嘗試使用此注釋@RestController移動此方法並使其在寧靜的控制器中作為服務

  3. 我看到您正在訪問此/ api / addEmployee,但我認為配置不正確嗎?

我們應該使用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;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM