我从网络通话中获取JSON,并将其转换为多个对象。之后,我想将其发送到前端并在浏览器中显示。
我收到此错误:

MessageBodyWriter not found for media type=application/json, type=class java.util.ArrayList, genericType=java.util.List<Domain.CarMovements>


这是我用来设置路径的最终方法。

@GET
@Produces("application/json")
@Path("/carMovements")
public List<CarMovements> getCarMovement() {
    List<CarMovements> tol = service.getCarMovements();
    return tol;
}


上面的List<Carmovements> tol中充满了carMovements。当我更改方法以返回String时,例如执行此操作:

 @GET
@Produces("application/json")
@Path("/carMovements")
public String getCarMovement() {
    //List<CarMovements> tol = service.getCarMovements();
    return "test";
}


确实可以,并且会显示在我的浏览器中。我在POM中添加了一些依赖项

    <groupId>com.mycompany</groupId>
<artifactId>RekeningRijdersBackend</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>

<name>RekeningRijdersBackend</name>

    <properties>
    <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>eclipselink</artifactId>
        <version>2.5.2</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId>
        <version>2.5.2</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.10</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20090211</version>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
        <version>7.0</version>
        <type>jar</type>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
                <compilerArguments>
                    <endorseddirs>${endorsed.dir}</endorseddirs>
                </compilerArguments>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.3</version>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.6</version>
            <executions>
                <execution>
                    <phase>validate</phase>
                    <goals>
                        <goal>copy</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${endorsed.dir}</outputDirectory>
                        <silent>true</silent>
                        <artifactItems>
                            <artifactItem>
                                <groupId>javax</groupId>
                                <artifactId>javaee-endorsed-api</artifactId>
                                <version>7.0</version>
                                <type>jar</type>
                            </artifactItem>
                        </artifactItems>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

最佳答案

您可以尝试使用类似GSON的库,将带有CarMovements的列表转换为字符串,然后返回该字符串。

比得到的东西像:

@GET
@Produces("application/json")
@Path("/carMovements")
public String getCarMovement() {
    Gson gson = new Gson();
    return gson.toJson(service.getCarMovements());
}

09-25 17:21