我有一组实体(列表),我需要将其转换为 json 或从 json 转换。
POJO:
public class Task {
private Long id;
private String title;
private Boolean done;
(...)
}
泽西岛产生以下结果
[{"id":1,"title":"T1","done":false},{"id":2,"title":"T2","done":false}]
当我调用该方法时:
@GET
@Override
@Produces("application/json")
public List<Task> findAll() {
(...)
return tasks;
}
到目前为止,一切都很好。
现在,我需要使用类似的 JSON 字符串。我认为以下方法可以解决问题:
@PUT
@Consumes("application/json")
public void save(List<Task> tasks) {
(...)
}
但相反,我收到以下错误:
SEVERE: line 1:0 no viable alternative at input
'"[{\"id\":1,\"title\":\"T1\",\"done\":true},{\"id\":2,\"title\":\"T2\",\"done\":false}]"'
我究竟做错了什么?这甚至可能吗?
泽西岛的依赖:
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>2.1</version>
</dependency>
这是 web.xml 配置
<servlet>
<servlet-name>Jersey Servlet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>my.rest.package</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Servlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>