缺少所需的请求正文:public org.springframework.http.ResponseEntity<..model.Customer>
Posted
技术标签:
【中文标题】缺少所需的请求正文:public org.springframework.http.ResponseEntity<..model.Customer>【英文标题】:Required request body is missing: public org.springframework.http.ResponseEntity<..model.Customer> 【发布时间】:2020-03-04 09:16:38 【问题描述】:基于来自 Web 的示例创建了两个应用程序:Angular 中的前端和客户) 角网址:http://localhost:4200/ Spring Boot URL:http://localhost:9020/(REST:http://localhost:9020/api/)
根据此处专家对我之前的问题 (https://***.com/posts/58685251) 的说明,我正面临新的例外情况,因为我无法自行解决。
角部分
export class Customer
id: number;
firstname: string;
lastname: string;
age: number;
active: boolean;
.
import Component, OnInit from '@angular/core';
import Customer from '../customer';
import CustomerService from '../customer.service';
@Component(
selector: 'create-customer',
templateUrl: './create-customer.component.html',
styleUrls: ['./create-customer.component.css']
)
export class CreateCustomerComponent implements OnInit
customer: Customer = new Customer();
submitted = false;
constructor(private customerService: CustomerService)
ngOnInit()
newCustomer(): void
this.submitted = false;
this.customer = new Customer();
save()
this.customerService.createCustomer(this.customer)
.subscribe(data => console.log(data);
console.log(this.customer);
this.submitted = true;,error => console.log(error));
this.customer = new Customer();
this.customer.firstname="HHH";
onSubmit()
this.save();
.
import Injectable from '@angular/core';
import HttpClient from '@angular/common/http';
import Observable from 'rxjs';
import Customer from './customer';
@Injectable(
providedIn: 'root'
)
export class CustomerService
private baseUrl = 'http://localhost:9020/api/';
constructor(private http: HttpClient)
getCustomer(id: number): Observable<Object>
return this.http.get(`$this.baseUrl` + `customers/$id`);
createCustomer(customer: Customer): Observable<Object>
console.log(customer);
return this.http.post(`$this.baseUrl` + `create`, customer);
updateCustomer(id: number, value: any): Observable<Object>
return this.http.put(`$this.baseUrl/$id`, value);
deleteCustomer(id: number): Observable<any>
return this.http.delete(`$this.baseUrl` + `customers/$id`, responseType: 'text' );
getCustomersList(): Observable<any>
return this.http.get(`$this.baseUrl` + `customers`);
getCustomersByAge(age: number): Observable<any>
return this.http.get(`$this.baseUrl` + `customers/age/$age`);
deleteAll(): Observable<any>
return this.http.delete(`$this.baseUrl` + `customers/delete`, responseType: 'text' );
.
CREATE TABLE customer(
id INT NOT NULL AUTO_INCREMENT,
firstname VARCHAR(20) NOT NULL,
lastname VARCHAR(20) NOT NULL,
age INT,
active boolean,
PRIMARY KEY (id)
);
application.properties:
- server.port=9020
- spring.datasource.url=jdbc:h2:file:./testdb
- spring.datasource.username=H2 spring.datasource.password=password
- spring.jpa.hibernate.ddl-auto = update
- spring.jpa.show-sql=true
春季启动
@CrossOrigin(origins = "http://localhost:4200")
@RestController
@RequestMapping("/api")
public class CustomerController
@Autowired
CustomerRepository repository;
@RequestMapping(value = "/create")
public ResponseEntity<Customer> postCustomer(@RequestBody Customer customer)
try
LOG.setLevel(Level.ALL);
LOG.info("public ResponseEntity<Customer> postCustomer(@RequestBody Customer customer");
LOG.info("customer.getFirstName(): "+customer.getFirstName());
//new Customer(customer.getId(),customer.getFirstName(),customer.getLastName(),customer.getAge(),customer.isActive())
Customer _customer = repository.save(customer);
return new ResponseEntity<>(_customer, HttpStatus.CREATED);
catch (Exception e)
return new ResponseEntity<>(null, HttpStatus.EXPECTATION_FAILED);
.
@ResponseBody
@Entity
@Table(name = "Customer")
public class Customer implements Serializable
private static final long serialVersionUID = -3009157732242241606L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(name = "lastname")
private String lastname;
@Column(name = "firstname")
private String firstname;
@Column(name = "age")
private int age;
@Column(name = "active")
private boolean active = true;
public Customer()
public Customer(String firstName, String lastName, int age, boolean active)
this.firstname = firstName;
this.lastname = lastName;
this.age = age;
this.active=active;
public long getId()
return id;
public String getFirstName()
return firstname;
public void setFirstName(String firstName)
this.firstname = firstName;
public void setLastName(String lastName)
this.lastname = lastName;
public String getLastName()
return this.lastname;
public void setAge(int age)
this.age = age;
public int getAge()
return this.age;
public boolean isActive()
return active;
public void setActive(boolean active)
this.active = active;
@Override
public String toString()
String info = String.format("Customer info: id = %d, firstname = %s, lastname = %s, age = %d", firstname,
lastname, age, id);
return info;
预期结果是坚持使用正确的名字和姓氏的客户,但我得到了例外:
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Thu Nov 07 17:53:03 CET 2019
There was an unexpected error (type=Bad Request, status=400).
Required request body is missing: public org.springframework.http.ResponseEntity<.model.Customer> .controller.CustomerController.postCustomer(model.Customer)
org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public org.springframework.http.ResponseEntity<model.Customer> .controller.CustomerController.postCustomer(com.javasampleapproach.mysql.model.Customer)
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:160)
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:130)
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:127)
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:167)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:134)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:892)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1039)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
【问题讨论】:
能否请您从Customer
实体类中删除@ResponseBody
?
在这种情况下,我没有得到异常,但字段 lastname 和 fristname 仍然为空,尽管我添加了字符串:`"id":42,"age":99,"active":true,"姓氏":null,"firstName":null``
把@RequestMapping(value = "/create")
改成@PostMapping(value = "/create")
嗨@reflexdemon 不幸的是没有改进
嗨@reflexdemon 我直到现在才能解决这个问题。我仍然是例外:Sun Nov 10 21:19:14 CET 2019 There was an unexpected error (type=Method Not Allowed, status=405). Request method 'GET' not supported org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported at org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping.handleNoMatch(RequestMappingInfoHandlerMapping.java:200)
【参考方案1】:
尝试这样做:
@RequestMapping(value = "/create")
public ResponseEntity<Customer> postCustomer(@RequestBody Customer customer)
try
LOG.setLevel(Level.ALL);
LOG.info("public ResponseEntity<Customer> postCustomer(@RequestBody Customer customer");
LOG.info("customer.getFirstName(): "+customer.getFirstName());
//new Customer(customer.getId(),customer.getFirstName(),customer.getLastName(),customer.getAge(),customer.isActive())
Customer _customer = repository.save(customer);
return new ResponseEntity<Customer>(_customer, HttpStatus.CREATED);
catch (Exception e)
return new ResponseEntity<Customer>(null, HttpStatus.EXPECTATION_FAILED);
【讨论】:
【参考方案2】:只有当你返回 ResponseEntity :
时你才会这样做 @RequestMapping(value = "$jwt.route.registration.path", method = RequestMethod.POST)
public ResponseEntity<User> registration(@RequestBody ExternalUser externalUser)
System.out.println(externalUser.getUsername());
User userInstance=((JwtUserDetailsService)userDetailsService).saveUser(externalUser);
return new ResponseEntity<User>(userInstance,HttpStatus.OK);
【讨论】:
嗨@LingarajSahoo 为什么我需要注册?在我的应用中,我没有安全元素 我举了一个responseEntity怎么写的例子 嗨@LingarajSahoo,仍然面临同样的问题:( 嗨@LingarajSahoo,仍然面临同样的问题:( Whitelabel 错误页面此应用程序没有/error 的显式映射,因此您将其视为后备。CET 11 月 7 日星期四 20:02:58 2019 出现意外错误(type=Bad Request, status=400)。缺少必需的请求正文:public org.springframework.http.ResponseEntity以上是关于缺少所需的请求正文:public org.springframework.http.ResponseEntity<..model.Customer>的主要内容,如果未能解决你的问题,请参考以下文章
使用 ContentCachingRequestWrapper 后缺少所需的请求正文
使用 HttpServletRequestWrapper 制作副本后缺少所需的请求正文
缺少所需的请求正文内容:org.springframework.web.method.HandlerMethod$HandlerMethodParameter