Issue
I've been trying to work on my simple Spring application, but when i got to the request, i always get a 400 code and i can't tell why. I provided below the Main,Controller and service classes. I double checked the path that i provided for the request, but still nothing. I also posted a picture on how the request looks like enter image description here
package com.example.greenbottle.customer;
import javax.persistence.*;
import java.time.LocalDate;
@Entity
@Table
public class Customer {
@Id
@SequenceGenerator(
name = "customer_sequence",
sequenceName = "customer_sequence",
allocationSize = 1
)
@GeneratedValue(
strategy = GenerationType.SEQUENCE,
generator = "customer_sequence"
)
private Long id;
private String name;
private String phoneNumber;
private LocalDate createdAt;
public Customer(String name, String phoneNumber,LocalDate createdAt) {
this.name = name;
this.phoneNumber = phoneNumber;
this.createdAt = createdAt;
}
public Customer() {}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public LocalDate getCreatedAt() {
return createdAt;
}
public void setCreatedAt(LocalDate createdAt) {
this.createdAt = createdAt;
}
}
package com.example.greenbottle.customer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping(path = "api/v2/customer")
public class CustomerController {
private final CustomerService customerService;
@Autowired
public CustomerController(CustomerService customerService) {this.customerService =
customerService;}
@GetMapping
public List<Customer> getCustomers() {return customerService.getCustomers();}
@PostMapping
public void registerNewCustomer(@RequestBody Customer customer)
{customerService.registerNewCustomer(customer);}
}
package com.example.greenbottle.customer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.List;
import java.util.Optional;
@Service
public class CustomerService {
private final CustomerRepository customerRepository;
@Autowired
public CustomerService(CustomerRepository customerRepository) {this.customerRepository = customerRepository;}
public List<Customer> getCustomers() {
return customerRepository.findAll();
}
public void registerNewCustomer(Customer customer) {
customerRepository.save(customer);
}
}
Solution
HTTP message syntax has to follow the protocol definition, which says, that you must have an empty line after the end of headers and before the start of payload data (body). Not doing so, will result in a malformatted HTTP request.
So, you can try:
POST http://localhost:8080/api/v2/customer
Content-Type: application/json
//MANDATORY EMPTY-LINE HERE
{
"name": "Alex",
...
...
}
Answered By - Giorgi Tsiklauri
Answer Checked By - David Marino (JavaFixing Volunteer)