0

I am getting Bad Request when trying to do POST to createCategory end point. All endpoint works but not for createCategory. Am i doing something wrong?

All my DTO classes using the same annotation but only this one doesn't work. Is it possible that spring doesn't accept single variable response body?

endpoint: http://localhost:8180/api/v1/categories

request body in json:
{
   "name": "Category 1"
}

CategoryController:

    @RestController
    @RequiredArgsConstructor
    @RequestMapping("api/v1/categories")
    public class CategoryController {

    private final CategoryApplicationService categoryApplicationService;

    @PostMapping
    public ResponseEntity<Data<CategoryIDResponse>> createCategory(@RequestBody CreateCategory createCategory){
        return new ResponseEntity<>(new Data<>(categoryApplicationService.createCategory(createCategory)), HttpStatus.CREATED);
    }

    @PatchMapping
    public ResponseEntity<Data<CategoryIDResponse>> updateCategory(@RequestBody UpdateCategory updateCategory){
        return new ResponseEntity<>(new Data<>(categoryApplicationService.updateCategory(updateCategory)), HttpStatus.OK);
    }

    @DeleteMapping("/{categoryID}")
    public ResponseEntity<Data<CategoryIDResponse>> deleteCategory(@PathVariable("categoryID") UUID categoryID){
        return new ResponseEntity<>(new Data<>(categoryApplicationService.deleteCategory(categoryID)), HttpStatus.OK);
    }

    @GetMapping("/{categoryID}")
    public ResponseEntity<Data<GetCategoryResponse>> getCategory(@PathVariable("categoryID") UUID categoryID){
        return new ResponseEntity<>(new Data<>(categoryApplicationService.getCategory(categoryID)), HttpStatus.OK);
    }

    @GetMapping
    public ResponseEntity<Data<List<GetCategoryResponse>>> getAllCategory(){
        return new ResponseEntity<>(new Data<>(categoryApplicationService.getAllCategory()), HttpStatus.OK);
    }
}

DTO:

CreateCategory:

    @Getter
    @Builder
    @AllArgsConstructor
    public class CreateCategory {

    @NotNull
    private final String name;
}
invzbl3
  • 5,872
  • 9
  • 36
  • 76
Fen
  • 5
  • 2
  • add `@Setter` annotation on top of the `CreateCategory` class and replace `@NotNull` with `@Column(name = "NAME", nullable = false)` and try again. – Issa Khodadadi Oct 29 '22 at 06:40
  • CreateCategory is just a DTO class, I think you cant add persistence annotation to this class. @IssaKhodadadi – Fen Oct 29 '22 at 06:54
  • 1
    Check `Content-Type` in request headers to make sure that it is `application/json` – Trash Can Oct 29 '22 at 06:58
  • Yep its application/json @TrashCan – Fen Oct 29 '22 at 07:07
  • You need to also add a setter for `name` so that Spring can set its value – Trash Can Oct 29 '22 at 07:10
  • nope it doesn't work, I think @AllArgsConstructor is enough. All my DTO classes using the same annotation but only this one doesn't work. Is it possible that spring doesn't accept single variable response body? – Fen Oct 29 '22 at 07:22
  • Spring accept single variable request/response body. Can you please share curl request from postman? – Rohit Agarwal Oct 29 '22 at 09:17

2 Answers2

0

Actually, you need to add @Setter annotation to the CreateCategory class by removing the final keyword, because spring will set the fields to the objects using setter methods, and you cant use setters with final variables.

  • 1
    i have tried this but still doesn't work, i tried to add another variable and it worked, example: adding new String fields. I still don't understand why this happen. – Fen Oct 31 '22 at 05:10
  • Can you put the full error stack trace that you are getting? – Jayamal Jayamaha Oct 31 '22 at 05:36
0

This is happening because you have not added NoArgsConstructor in your DTO.

@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class CreateCategory {
   private String name;
}

Change your DTO to above code. Working fine for me.

Hope this helps.

Chetan
  • 94
  • 7
  • Thanks this actually worked for me. But why do i need to add @NoArgsConstructor?. My other DTO classes worked without @NoArgsConstructor and worked fine. – Fen Oct 31 '22 at 13:29
  • You can visit this link for detailed information.[Link](https://stackoverflow.com/questions/68314072/why-to-use-allargsconstructor-and-noargsconstructor-together-over-an-entity) – Chetan Oct 31 '22 at 14:09