java - 将 list<Object> 发送到前端时发生 MessageBodyWriter 错误

标签 java maven

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

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

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

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

以上List<Carmovements> tol正在充满汽车运动。当我更改方法以返回字符串时,例如执行以下操作:

 @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());
}

关于java - 将 list<Object> 发送到前端时发生 MessageBodyWriter 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30481967/

相关文章:

Java Integer.valueOf 为范围内的有效数字生成 NumberFormatException

java - 在需要连接到具有不同 URL 的数据库的 tomcat 中部署两个项目

java - 如何检查不同步工作的多个线程的功能

maven - Sonar 分析失败 - 无法请求/批量引导/属性

java - 需要解释java.lang.ClassNotFoundException : org. springframework.web.context.ContextLoaderListener

Java -for循环只重复最后一个值

java - 一个月中倒数第二个星期的 Cron 表达式

java - JUnit 测试用例未通过 Maven 运行

java - 如何将文件夹添加到类路径?

maven - 何时在构建工具的 Sonarqube 插件上使用 Sonar 扫描仪?