Java – Spring Boot Content type ‘multipart/form-data;boundary=————————–#;charset=UTF-8’ not supported

javaspring-boot

I'm new to Spring…I have a Spring Boot API application and all of my methods (POST, GET, etc) work great with Postman when the Content-Type is set to application/json, I can send and receive JSON.

However, I'd really like for my methods to also accept a GET or POST in the browser. When I use the browser to do a GET, the API returns an INTERNAL_SERVER_ERROR. I created a small form and try to POST to my API, but then I get the UNSUPPORTED_MEDIA_TYPE: Content type 'multipart/form-data;boundary=————————–802438220043016845671644;charset=UTF-8' not supported

These are 2 of the methods in my @RestController:

@RequestMapping(method = RequestMethod.POST, value = {"","/"})
public ResponseEntity<MyModel> createModel(@Valid @RequestBody MyModelDto modelDto) {
    MyModel model = modelService.createModel(modelDto);
    URI createdModelUrl = ServletUriComponentsBuilder.fromCurrentRequest().path("/{identifier}")
            .buildAndExpand(model.getIdentifier()).normalize().toUri();
    return ResponseEntity.created(createdModelUrl).build();

@RequestMapping(method = RequestMethod.GET, value = "/{identifier}")
public Resource<MyModel> getByIdentifier(@PathVariable("identifier") String identifier) {
    MyModel model = modelService.getByIdentifier(identifier);
    Resource<MyModel> resource = new Resource<>(model);
    return resource;
}

If there's any other code that would be helpful to show, let me know and I'll update the thread.

Best Answer

In createModel method, instead of @RequestBody, please use @ModelAttribute for MyModelDto parameter.

Related Topic