【问题标题】:Failed: Required Request Body is missing: public springframework.http.ResponseEntity...postController失败:缺少所需的请求正文:公共 springframework.http.ResponseEntity...postController
【发布时间】:2020-02-29 06:53:08
【问题描述】:

我已经从 Web 创建了基于应用程序的示例:Angular 的前端和 Spring Boot 的后端 客户) 角网址:http://localhost:4200/ Spring Boot URL:http://localhost:9020/(REST:http://localhost:9020/api/

<h1>Angular Part </h1>

`export class Customer {
    id: number;
    firstname: number;
    lastname: Number;
    age: number;
    active: boolean;}`

import { Customer } from './customer';
    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.lastname: "+customer.lastname);
        console.log("customer.firstname: "+customer.firstname);
        return this.http.post(${this.baseUrl} + `/create`, customer);
      }
      getCustomersList(): Observable<any> {
        return this.http.get(${this.baseUrl}+`/customers`);
      }
    }


    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);
            this.submitted = true;},error => console.log(error));
            this.customer = new Customer();}

      onSubmit() {   
        this.save();}}

春季启动

@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 = "last_Name")
    private String lastName;

    @Column(name = "first_Name")
    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;
    }
}

.

@CrossOrigin(origins = "http://localhost:4200")
@RestController
@RequestMapping("/api")
public class CustomerController {

  @Autowired
  CustomerRepository repository;


  @PostMapping(value = "/create")
  public ResponseEntity<Customer> postCustomer(@RequestBody Customer customer) {
    try {
      Customer _customer = repository.save(new Customer(customer.getFirstName(),customer.getLastName(),customer.getAge(),customer.isActive()));
      return new ResponseEntity<>(_customer, HttpStatus.CREATED);
    } catch (Exception e) {
      return new ResponseEntity<>(null, HttpStatus.EXPECTATION_FAILED);
    }
  }
}

.

CREATE TABLE customer(
       id INT NOT NULL AUTO_INCREMENT,
       firstname VARCHAR(20) NOT NULL,
       lastname VARCHAR(20) NOT NULL,
       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 = 更新
  • spring.jpa.show-sql=true`

    有效负载{"id":518,"lastName":null,"firstName":null,"age":99,"active":true} lastName 和 firstName 应该是字符串值,因为我输入的是字符串enter image description here

    1:https://i.stack.imgur.com/2s8jH.png

【问题讨论】:

  • 我在这里做错了什么?显然我输入的值会导致 numberformat 异常。有什么想法吗?
  • 查看错误。它准确地告诉您出了什么问题:spring 试图将字符串“create”转换为 Java long 类型。这可能意味着您正试图在第二个代码 sn-p 的 getCustomer() 函数中创建一个 ID 为“create”的客户。跟踪对该函数的调用并确保您传入的是 Number 类型。
  • POST: localhost:9020/api/create { "active": true, "age": 55, "firstname": "Test", "lastname": "Test" } 结果 { "id": 591, "lastName": null, "firstName": null, "age": 55, "active": true } 有什么想法吗?
  • 更改后:我收到此错误:Method Create Customer @RequestMapping(value = "/create") public ResponseEntity&lt;Customer&gt; postCustomer(@RequestBody Customer customer) Required request body is missing: public org.springframework.http.ResponseEntity&lt;Customer&gt; CustomerController.postCustomer(Customer) org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public org.springframework.http.ResponseEntity&lt;Customer&gt; CustomerController.postCustomer(Customer)Attached my Post abouve
  • 我需要帮助。你知道我在这里做错了什么吗?

标签: java angular hibernate spring-boot


【解决方案1】:

在Entity类中,列名为“first_name”,但是当你创建表时,它以“firstname”命名,下划线消失了。

【讨论】:

    猜你喜欢
    • 2017-12-02
    • 1970-01-01
    • 2019-09-09
    • 1970-01-01
    • 2017-08-01
    • 2019-10-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-04
    相关资源
    最近更新 更多