我试图制作一个可以返回食谱列表的rest应用,count方法有效,但是当我尝试获取其中一个或全部食谱时,会出现此错误
javax.servlet.ServletException: org.glassfish.jersey.server.ContainerException: java.util.ServiceConfigurationError: javax.json.bind.spi.JsonbProvider: Provider org.eclipse.yasson.JsonBindingProvider not found
我有
<dependency>
<groupId>org.eclipse</groupId>
<artifactId>yasson</artifactId>
<version>1.0</version>
</dependency>
在我的
pom.xml
中功能看起来像这样
@GET
@Path("/{id : \\d+}")
@Produces(APPLICATION_JSON)
public Response getBook(@PathParam("id") @Min(1) int id) {
Recipe recipe = recipeRepository.findOneByID(id);
if (recipe == null)
return Response.status(Response.Status.NOT_FOUND).build();
return Response.ok(recipe).build();
}
这是按ID返回配方的函数
public Recipe findOneByID(int id) {
return entitymanager.find(Recipe.class, id);
}
配方具有以下属性
@Id
private int id;
private String complexity;
private int cookingTime;
private String description;
private int estimatedTime;
private String imageUrl;
private String information;
private boolean isPromoted;
private int preparationTime;
private float servings;
private String title;
private String type;
//bi-directional many-to-one association to Allergen
@OneToMany(mappedBy="recipe")
private List<Allergen> allergens;
//bi-directional many-to-one association to Ingredient
@OneToMany(mappedBy="recipe")
private List<Ingredient> ingredients;
//bi-directional many-to-one association to Mediaitem
@OneToMany(mappedBy="recipe")
private List<Mediaitem> mediaitems;
//bi-directional many-to-one association to Nutritionvalue
@OneToMany(mappedBy="recipe")
private List<Nutritionvalue> nutritionvalues;
//bi-directional many-to-one association to Step
@OneToMany(mappedBy="recipe")
private List<Step> steps;
任何提示都会有很大帮助。我花了半天时间来解决这个问题
最佳答案
如果您在GlassFish 5.0-b11或更高版本上运行此代码,则需要删除对Yasson的依赖关系(因为Yasson已包含在b11
之后的GlassFish 5夜间构建中),但是您应该指定对JSON-B 1.0的依赖关系API(实际上可能是JSON-P 1.1),因为还没有Java EE 8伞式依赖项可以涵盖所有Java EE 8规范。
删除Yasson并添加以下内容:
<dependency>
<groupId>javax.json.bind</groupId>
<artifactId>javax.json.bind-api</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>1.1</version>
<scope>provided</scope>
</dependency>
请注意,由于GlassFish 5提供了实现,因此它们必须具有
<scope>provided</scope>
,并且您不希望Maven在您的应用程序中构建库。如果使用的是
GlassFish 5.0-b10
或更低版本,则需要在上面的pom.xml中指定这些相同的依赖项以及Yasson依赖项,因为Yasson是实现,并且需要存在:<dependency>
<groupId>javax.json.bind</groupId>
<artifactId>javax.json.bind-api</artifactId>
<version>1.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.eclipse</groupId>
<artifactId>yasson</artifactId>
<version>1.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>1.1</version>
<scope>compile</scope>
</dependency>
我还在此处明确添加了
<scope>compile</scope>
,因为如果未提供范围,则这是默认设置。对此进行明确说明有时很有用,因为您将来可能希望将此项目移至GlassFish 5,并且需要更改范围并完全删除Yasson依赖项。资料来源:http://json-b.net/getting-started.html