Home > Enterprise >  Bad request in RestController spring boot 2.7
Bad request in RestController spring boot 2.7

Time:01-13

I checked in several different ways to check where is bug but I still do not know the answer.

That is my RestController

@RestController
public class CustomerController {
    @PostMapping(value = "/customer")
    public ResponseEntity<CustomerResponse> addCustomer(@RequestBody @Valid Customer custRequest) throws Exception {
        ModelMapper modelMapper = new ModelMapper();
        CustomerDto customerDto = modelMapper.map(custRequest, CustomerDto.class);
        CustomerDto addCust = customer.addCustomer(customerDto);
        CustomerResponse custResponse = modelMapper.map(addCust, CustomerResponse.class);
        return new ResponseEntity<CustomerResponse>(custResponse, HttpStatus.CREATED);
    }
}

That is my Model

@Entity
@Table(name = "customers")
public class Customer {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    private String idCustomer;
    private String email;
    @OneToMany(mappedBy = "customer",cascade = CascadeType.ALL,fetch = FetchType.EAGER)
    private List<Choice> choices;
    // Getter and setter and constructor
}

maven dependencies

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.modelmapper</groupId>
            <artifactId>modelmapper</artifactId>
            <version>2.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
</dependencies>

response of postman enter image description here

How can i resolve this problem to post a new customer .

CodePudding user response:

@Valid lets the framework check against the parameter on the method invocation and, if the validation fails, an HTTP 400 Bad Request status is thrown. You are having a complex type and that type requires a validation too. Here's a link: https://medium.com/javarevisited/are-you-using-valid-and-validated-annotations-wrong-b4a35ac1bca4

Try removing the @Valid just to see the difference and the above article gives you an example implementation.

Also, using an Entity as a Request object is a highly discouraged paradigm. You are encouraged to make a DTO object specific to your request as a start. There are situations where you have to inherit, but this question's scope is not that.

CodePudding user response:

The validation of Customer seems to fail. I would

  1. look at the logs to error details
  2. think about having a separate class for the request object. You do not really want to mix database related and request related concerns.
  • Related