无法识别的字段 class 未标记为可忽略
Unrecognized field class not marked as ignorable
具有以下特征:
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>avdisws</display-name>
<servlet-mapping>
<servlet-name>resteasy-servlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>resteasy-servlet</servlet-name>
<servlet-class>
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>paqA.paqB.paqC.RestApplication</param-value>
</init-param>
</servlet>
</web-app>
Class Book.java:
@XmlRootElement(name = "book")
@XmlType(propOrder = {"title", "author", "price"})
public class Book {
private String title;
private String author;
private Double price;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
}
Class BookService.java:
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
@Path("bookService")
@Produces(MediaType.APPLICATION_JSON + "; charset=ISO-8859-15")
@Consumes(MediaType.APPLICATION_JSON)
public class BookService {
@POST
@Path("test")
public Book getBook(@Context HttpServletRequest request, Book book) {
Book returnedBook = new Book();
returnedBook.setAuthor("Test Author");
returnedBook.setTitle("Test Title");
returnedBook.setPrice(99.99);
return returnedBook;
}
}
Class RestApplication:
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
@ApplicationPath("/")
public class RestApplication extends Application {
private Set<Object> singletons = new HashSet<Object>();
public RestApplication() {
super();
singletons.add(new BookService());
}
@Override
public Set<Object> getSingletons() {
return singletons;
}
}
在我的本地 JBoss AS 6 环境中,我可以使用 JSON:[=19 正确调用 http://localhost:8080/avdisws/bookService/test =]
{
"book":
{
"title": "Some book",
"author": "Some author",
"price": 89.21
}
}
但是在 DESA 环境中,使用 Jboss EAP 7.1 我得到:
无法识别的字段"book" (class paqA.paqB.paqC.Book),未标记为可忽略
如果我用 next JSON 调用,它不会失败:
{
"title": "Some book",
"author": "Some author",
"price": 89.21
}
这些是我正在使用的库:
javax.servlet-api-3.1.0.jar
jaxrs-api-3.0.10.Final.jar
resteasy-jaxrs-3.0.10.Final.jar
resteasy-client-3.0.10.Final.jar
resteasy-jaxb-provider-3.0.10.Final.jar
resteasy-jettison-provider-3.0.10.Final.jar
httpclient-4.5.3.jar
httpcore-4.4.6.jar
commons-io-2.6.jar
commons-logging-1.2.jar
jettison-1.3.8.jar
使用@JsonIgnoreProperties(ignoreUnknown = true)没有解决问题。
谢谢!
对我来说,这解决了问题:
<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
<deployment>
<exclusions>
<module name="org.jboss.resteasy.resteasy-jackson2-provider" />
</exclusions>
</deployment>
</jboss-deployment-structure>
具有以下特征:
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>avdisws</display-name>
<servlet-mapping>
<servlet-name>resteasy-servlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>resteasy-servlet</servlet-name>
<servlet-class>
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>paqA.paqB.paqC.RestApplication</param-value>
</init-param>
</servlet>
</web-app>
Class Book.java:
@XmlRootElement(name = "book")
@XmlType(propOrder = {"title", "author", "price"})
public class Book {
private String title;
private String author;
private Double price;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
}
Class BookService.java:
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
@Path("bookService")
@Produces(MediaType.APPLICATION_JSON + "; charset=ISO-8859-15")
@Consumes(MediaType.APPLICATION_JSON)
public class BookService {
@POST
@Path("test")
public Book getBook(@Context HttpServletRequest request, Book book) {
Book returnedBook = new Book();
returnedBook.setAuthor("Test Author");
returnedBook.setTitle("Test Title");
returnedBook.setPrice(99.99);
return returnedBook;
}
}
Class RestApplication:
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
@ApplicationPath("/")
public class RestApplication extends Application {
private Set<Object> singletons = new HashSet<Object>();
public RestApplication() {
super();
singletons.add(new BookService());
}
@Override
public Set<Object> getSingletons() {
return singletons;
}
}
在我的本地 JBoss AS 6 环境中,我可以使用 JSON:[=19 正确调用 http://localhost:8080/avdisws/bookService/test =]
{
"book":
{
"title": "Some book",
"author": "Some author",
"price": 89.21
}
}
但是在 DESA 环境中,使用 Jboss EAP 7.1 我得到:
无法识别的字段"book" (class paqA.paqB.paqC.Book),未标记为可忽略
如果我用 next JSON 调用,它不会失败:
{
"title": "Some book",
"author": "Some author",
"price": 89.21
}
这些是我正在使用的库:
javax.servlet-api-3.1.0.jar
jaxrs-api-3.0.10.Final.jar
resteasy-jaxrs-3.0.10.Final.jar
resteasy-client-3.0.10.Final.jar
resteasy-jaxb-provider-3.0.10.Final.jar
resteasy-jettison-provider-3.0.10.Final.jar
httpclient-4.5.3.jar
httpcore-4.4.6.jar
commons-io-2.6.jar
commons-logging-1.2.jar
jettison-1.3.8.jar
使用@JsonIgnoreProperties(ignoreUnknown = true)没有解决问题。
谢谢!
对我来说,这解决了问题:
<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
<deployment>
<exclusions>
<module name="org.jboss.resteasy.resteasy-jackson2-provider" />
</exclusions>
</deployment>
</jboss-deployment-structure>