Search code examples
javajpapostjakarta-eequarkus

415 Unsupported Media type when doing POST


I have these two classes Item and Drink. Where Drink extends Item. Both are PanacheEntities and only consists a few basic fields. Code samples of this part (I left the getters and setters out of it):

@Entity
@Table(name = "t_items")
@Inheritance(strategy = InheritanceType.JOINED)
public class Item extends PanacheEntity {
   @Column(nullable = false)
   private String title;
   @Column
   private String description;
   @Column(nullable = false)
   private float price;
   @Column
   private LocalDateTime lastUpdatedDate;
   @Column(nullable = false)
   private String lastUpdatedUser;
}

@Entity
@Table(name = "t_drinks")
public class Drink extends Item {
    @Column
    private int volume;

    @Column
    private boolean containsAlcohol;

    @Column
    private float alcoholPercentage;
}

I am still in the phase of creating some basic CRUD operations. So I have this POST operation in the controller which I want to test via Postman, but it returns a 415 error code. This is the controller function:

@POST
@Path("/drinks")
public Response addDrink(Drink drink, @Context UriInfo uriInfo) {
    Long drinkId = drinkRepository.persistDrink(drink);
    UriBuilder builder = uriInfo.getAbsolutePathBuilder().path(Long.toString(drinkId));
    Log.info("drink persist");
    return Response.created(builder.build()).build();
}

And this is the repository function that gets called:

@Transactional(Transactional.TxType.REQUIRED)
public Long persistDrink(Drink drink) {
    Drink.persist(drink);
    Log.info(String.valueOf(drink.id));
    return drink.id;
}

I do know that there should be a domain layer in between for best practices, but that will be fixed after this problem.

This is the body I enter in the Postman call:

{
    "title": "Cola",
    "description": "Refreshing soda",
    "price": 2.5,
    "lastUpdatedDate": "2023-06-05T10:00:00",
    "lastUpdatedUser": "John Doe",
    "volume": 500,
    "containsAlcohol": false,
    "alcoholPercentage": 0.0
}

Where is the problem? I already tried to add @Consumes(MediaType.APPLICATION_JSON), but that does not work either.


Solution

  • Solution that worked for me:

    Commenting the quarkus-resteasy-reactive dependency in the pom.xml and adding this code in the pom.xml instead.

        <dependency>
          <groupId>io.quarkus</groupId>
          <artifactId>quarkus-resteasy-jsonb</artifactId>
        </dependency>
    

    Do not forget to reload/rebuild your application