将列表<object>发送到前端时出现MessageBodyWriter错误

时间:2015-05-27 12:08:21

标签: java maven

我从网络摄像头获取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>

1 个答案:

答案 0 :(得分:1)

您可以尝试使用像GSON这样的库,将带有CarMovements的List转换为String,然后返回该String。

比你得到的东西:

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