无法识别的字段类未标记为可忽略

Posted

技术标签:

【中文标题】无法识别的字段类未标记为可忽略【英文标题】:Unrecognized field class not marked as ignorable 【发布时间】:2020-04-19 02:20:48 【问题描述】:

具备以下条件:

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;
    


类 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;
    


类 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 正确调用 http://localhost:8080/avdisws/bookService/test:


    "book":
    
        "title": "Some book",
        "author": "Some author",
        "price": 89.21
    

但是在 DESA 环境中,使用 Jboss EAP 7.1 我得到:

无法识别的字段“book”(类 paqA.paqB.paqC.Book),未标记为可忽略

如果我使用下一个 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) 并不能解决问题。

谢谢!

【问题讨论】:

【参考方案1】:

对我来说,这解决了问题:

<?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>

【讨论】:

以上是关于无法识别的字段类未标记为可忽略的主要内容,如果未能解决你的问题,请参考以下文章

“ValueError:无法识别的标记样式'hline'”。 Matplotlib plot() 和 scatter() 函数无法识别一堆标记

无法识别的标记前缀或设备过滤器'sc'(在VS2012中)

Python argparse 忽略无法识别的参数

文件上传角度 - spring boot - mysql

“无法识别的类型'员工'。忽略。C:/ .....”从.xml读取输入并将输出写入.xls文件+ perl时出错

Wordpress 标记中无法识别的 iframe 渲染