[英]Unable to send POST request in SpringMVC
I have created an API using jersey and spring boot. 我使用球衣和弹簧靴创建了一个API。 Now when I hit POST request using Postman with following in request body:
现在,当我在请求正文中使用Postman命中POST请求时:
{
"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: 此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;
}
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? 编辑:在将客户端的方法= RequestMethod.GET更改为RequestMethod.POST时,什么也没发生,仍然出现相同的错误我在做什么错了?
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. 检查完代码问题后,在客户端应用程序中,后端应用程序在8090端口上运行,而在api中,您调用的addEmployee为8080。
Change this String url = "http://localhost:8080/api/addEmployee";
更改此
String url = "http://localhost:8080/api/addEmployee";
to String url = "http://localhost:8090/api/addEmployee";
到
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: 404表示所请求的源不存在,因为以下原因之一:
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)
没有控制器方法来处理POST请求,因此请尝试将
@RequestMapping(value = "/addEmployee", method = RequestMethod.GET)
更改为@RequestMapping(value = "/addEmployee", method = RequestMethod.POST)
try to move this method and make it a service in a restful controller using this annotation @RestController
尝试使用此注释
@RestController
移动此方法并使其在宁静的控制器中作为服务
I see that you are accessing this /api/addEmployee which I think not configured right? 我看到您正在访问此/ api / addEmployee,但我认为配置不正确吗?
We should be using RequestMethod.POST instead of RequestMethod.GET 我们应该使用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.