gpt4 book ai didi

java - 将 list 发送到前端时发生 MessageBodyWriter 错误
转载 作者:行者123 更新时间:2023-12-01 11:30:02 26 4
gpt4 key购买 nike

我从网络调用获取 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/

26 4 0