415 Unsupported Media type when doing POST.

huangapple go评论61阅读模式
英文:

415 Unsupported Media type when doing POST

问题

I have these two classes Item and Drink. Where Drink extends Item. Both are PanacheEntities and only consist of a few basic fields.

@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.

英文:

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.

答案1

得分: 0

以下是已翻译的内容:

解决方法对我有用:

pom.xml中注释掉quarkus-resteasy-reactive依赖项,并在pom.xml中添加以下代码。

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

不要忘记重新加载/重建您的应用程序。

英文:

Solution that worked for me:

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

    &lt;dependency&gt;
      &lt;groupId&gt;io.quarkus&lt;/groupId&gt;
      &lt;artifactId&gt;quarkus-resteasy-jsonb&lt;/artifactId&gt;
    &lt;/dependency&gt;

Do not forget to reload/rebuild your application

huangapple
  • 本文由 发表于 2023年6月6日 04:30:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76409800.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定