错误获取方法rest json

1 java rest json glassfish jakarta-ee

我试图制作一个休息应用程序,它会返回一个食谱列表,count 方法有效,但是当我尝试获取其中一个或全部时,它给了我这个错误

javax.servlet.ServletException: org.glassfish.jersey.server.ContainerException: java.util.ServiceConfigurationError: javax.json.bind.spi.JsonbProvider: Provider org.eclipse.yasson.JsonBindingProvider not found
Run Code Online (Sandbox Code Playgroud)

我有

<dependency>
            <groupId>org.eclipse</groupId>
            <artifactId>yasson</artifactId>
            <version>1.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

在我的 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();
    }
Run Code Online (Sandbox Code Playgroud)

这是通过 ID 返回配方的函数

public Recipe findOneByID(int id) {
        return entitymanager.find(Recipe.class, id);

    }
Run Code Online (Sandbox Code Playgroud)

并且配方具有以下属性

@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;
Run Code Online (Sandbox Code Playgroud)

任何提示都会有很大帮助。我花了半天时间试图解决这个问题

Mik*_*ike 6

如果您在GlassFish 5.0-b11或更高版本上运行它,那么您需要删除对 Yasson 的依赖(因为 Yasson 已经包含在 GlassFish 5 nightly builds after 中b11),但您应该指定对 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>
Run Code Online (Sandbox Code Playgroud)

请注意,它们需要具有<scope>provided</scope>,因为 GlassFish 5 提供了实现,并且您不希望 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>
Run Code Online (Sandbox Code Playgroud)

我还在<scope>compile</scope>此处明确添加了,因为如果没有提供范围,这是默认值。明确说明这一点有时很有用,因为您将来可能希望将此项目移至 GlassFish 5,并且需要更改范围并完全删除 Yasson 依赖项。

来源http : //json-b.net/getting-started.html