• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Getting 415 errors in Spring Boot app claiming application/json is not a supported media type

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am developing a sample app and getting a 415 error stating that application/json is not supported.  I've been tearing my hair out, but am not finding any issue.
I am cribbing the idea from this tutorial:

Spring Boot tutorial (chapter on implementing a POST method)

I am sending the request and confirmed that the request body is valid JSON.  Here is what I believe to be the relevant code:



Error from Postman:



Screenshot of the Postman settings attached.
Screenshot-2023-05-05-at-9.48.58-AM.png
Postman screenshot
Postman screenshot
 
Marshal
Posts: 4518
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Robert Dennett wrote:I am developing a sample app and getting a 415 error stating that application/json is not supported.


I'm not a Spring developer, but in other frameworks you typically need to specify the type of content that the handler consumes.

For example: @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
 
Robert Dennett
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ron McLeod wrote:

Robert Dennett wrote:I am developing a sample app and getting a 415 error stating that application/json is not supported.


I'm not a Spring developer, but in other frameworks you typically need to specify the type of content that the handler consumes.

For example: @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)



Everything I can find says that that's the default media type for Spring Boot apps.  In any case, I tried adding the "consumes" attribute and it made no difference.

Thanks,
Rob
 
Ron McLeod
Marshal
Posts: 4518
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I assume that the object mapper requires that the class specified in the @RequestBody has a non-private no args constructor (either explicit or a default constructor)?  Does your Customer class have one?
 
Robert Dennett
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ron McLeod wrote:I assume that the object mapper requires that the class specified in the @RequestBody has a non-private no args constructor (either explicit or a default constructor)?  Does your Customer class have one?



It should since I added the Lombok annotations @NoArgsConstructor and @AllArgsConstructor:

 
Robert Dennett
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I also tried commenting out the Lombok annotations except for Builder and manually adding an all args constructor, a no args constructor and getters and setters for all fields, all generated using Intellij's generators.
 
Ron McLeod
Marshal
Posts: 4518
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For troubleshooting, try simplifying your Customer class to something bare bones like:
 
Robert Dennett
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ron McLeod wrote:For troubleshooting, try simplifying your Customer class to something bare bones like:



Well, stripping out the other fields was proving to be too complicated in my app, so I created a new one with only one field called name and it worked just fine.

I then tried modifying the JSON sent to the request like this:



but got the same error.

I also created this in the controller



and added this method to the controller



I also get the 415 error when I try to call it.
 
Robert Dennett
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Strike that, the api/v1/customer/test method worked just fine.  I had forgotten to set the content type to application/json.

So it appears that the Customer class can't be hydrated properly.  If that's the case, what needs to change?
 
Ron McLeod
Marshal
Posts: 4518
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may need to tweak the configuration for the object mapper to define how it should deal with extra or missing attributes in the JSON payload.

According to this Baeldung post, the default configuration includes: DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES.

Edit: I'm not sure if this is relevant anymore; I was responding to your earlier post
 
Robert Dennett
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, so this field in the Customer class is what's doing it:



Since a customer may have no orders, I want to be able to create a customer without passing anything into orders, but I don't think that it's the deserializer that's the problem.  I commented out the above and added another field called "test".

Passing both



and



worked just fine.  I would also expect to get a different error than a 415.
 
Marshal
Posts: 28239
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The post title wrote:Getting 415 errors in Spring Boot app


I read this and thought "Wow, that's a lot of errors." However I see you're making good progress on your one error.  
 
Ron McLeod
Marshal
Posts: 4518
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does the stack trace in the error message provide any clues?  The original one you shared pointed back to the AbstractMessageConverterMethodArgumentResolver#readWithMessageConverters method.  Is it still the same now?

 
Robert Dennett
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I should have specified HTTP 415 Unsupported Media Type errors.  That said, I am still stuck.
 
Robert Dennett
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ron McLeod wrote:Does the stack trace in the error message provide any clues?  The original one you shared pointed back to the AbstractMessageConverterMethodArgumentResolver#readWithMessageConverters method.  Is it still the same now?



Yeah, it's the same.
 
Ron McLeod
Marshal
Posts: 4518
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could this be a persistence-related issue which is being reported as an unsupported media problem?

Maybe try a request which has a non-empty array for the orders property.
 
Robert Dennett
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found this on SO

https://stackoverflow.com/questions/55190548/status-415-error-unsupported-media-type-for-post-request-onetomany-many

Which makes me think that I shouldn't use my Customer class (which is an @Entity) in the controller.  The quote in question:


Never use entity classes for data exchange on a rest service. Learn about 3 tier architecture –
Jens
Mar 15, 2019 at 20:57



is that what's going on here?
 
Robert Dennett
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I finally licked this.

Rather than trying to deserialize to my entity, I am using a record as a DTO and then building the entity from that and passing it to the service layer.  It works insofar as it returns a 200 and stores my data in the DB, but I can't say that it's THE solution.  Here's the code snippet from the controller class:

 
Ron McLeod
Marshal
Posts: 4518
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Seems like a good idea - it decouples the API from the entity definition, allowing them to evolve independently.
 
Marshal
Posts: 8869
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Couple of other things.

This:
api/v1/customer

You'd be better to name as (plural):
api/v1/customers

That's the convention. So you'd end up with:

If your API has a tendency to grow, consider creating OpenAPI spec i.e. defined in Swagger for your API which would allow you to design and document API upfront and so later you could generate server stubs and add them to your project (Spring) and implement. Also you'd get the required DTOs generated. And you wouldn't need to type those API mappings by hand.
 
reply
    Bookmark Topic Watch Topic
  • New Topic