0

When calling myPOstMethod of RESTful app , I got error "400 bad request" with no response body

The method definition is

@PostMapping()
public MyResponseDTO myPostMethod(
            @Parameter(description = "Paramdescription")
            @PathVariable String tokenParam,
            @RequestBody MyRequestDTO requestBody
    ) 
    {   
        //fill response ...
        return new MyResponseDTO(datafilled);
    } 

MyRequestDTO has field definied as following

public class MyRequestDTO extends BaseDTO {

    ......

    @Override
    public ExpType getExpType() {
        return super.getExpType();
    }
    
}

public enum ExpType {
    VALUE1,
    VALUE2,
    VALUE3
}

The error occurs when request value which is NOT contain in the Enum (for example VALUE5 )

I am using

@ControllerAdvice
public class GlobalControllerExceptionHandler extends ResponseEntityExceptionHandler 

As proposed here I am using GlobalControllerExceptionHandler annotated with @ControllerAdvice

With method to catch all exceptions defined as following

@ExceptionHandler(Exception.class)
@ResponseBody
ResponseEntity<Object> handleGeneralException(HttpServletRequest request, Exception ex) {
    .....
} 

But seems that this method does not called

What should be correct handler definition for such case ? Thanks in advance

lm.
  • 4,033
  • 4
  • 25
  • 37
  • what is the @RequestMapping of the controller? how does the request you are sending look like? is there any error in logs? – Emanuel Trandafir Mar 13 '23 at 08:08
  • Here is sample request sent : { "searchString":"some search str", "type" : "VALUE5", "description": "some description" } , The problematic field is TYPE . Did not found errors in log . – lm. Mar 13 '23 at 08:50
  • can you update the question and share the whole `MyRequestDTO` and `BaseDTO` ? probably it is a json deserializing issue – Emanuel Trandafir Mar 13 '23 at 08:58
  • May be helpful: [https://stackoverflow.com/a/64576320/10231374](https://stackoverflow.com/a/64576320/10231374) – Toni Mar 13 '23 at 09:09
  • EmanuelTrandafir , birca123 - Thanks - I succeeded to catch the exception - it is HttpMessageNotReadableException with "JSON parse error..". – lm. Mar 13 '23 at 09:15

1 Answers1

1

This may be a parameter parsing error: JSON parse error.

fixed:

@ExceptionHandler({HttpMessageNotReadableException.class})
Tonny Liu
  • 101
  • 3