英文:
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.
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-jsonb</artifactId>
</dependency>
Do not forget to reload/rebuild your application
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论