JAX-RS POST消息无法从Application / JSON转换为POJO

时间:2017-07-08 01:05:56

标签: java jax-rs

public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";

Class ItemOrder:

@Path("/{tableId}/order")
    @POST
    @Produces(MediaType.TEXT_PLAIN)
    @Consumes(MediaType.APPLICATION_JSON )
    public String giveOrder(List<ItemOrder> itemOrderList, @PathParam("tableID") int tableId){
Customer currentCustomer = restaurant.getTables().get(tableId).getCurrentCustomer();
for (int i = 0; i<itemOrderList.size(); i++){
            currentCustomer.giveOrder(itemOrderList.get(i));
        }

String retStr = "Thank you " + currentCustomer.getCustomerName() + ". Your food will be served soon";
return retStr;
}

班级项目:

@XmlRootElement
public class ItemOrder {
    private Item mItem;
    private int mNumberOfPlates;


    public ItemOrder(){
        mItem = null;
        mNumberOfPlates = 0;
    }
    public ItemOrder(Item item, int numberOfPlates){
        mItem = item;
        mNumberOfPlates = numberOfPlates;
    }

    public Item getItem() {
        return mItem;
    }
    public void setItem(Item mItem) {
        this.mItem = mItem;
    }
    public int getNumberOfPlates() {
        return mNumberOfPlates;
    }
    public void setNumberOfPlates(int mNumberOfPlates) {
        this.mNumberOfPlates = mNumberOfPlates;
    }
}

JSON Post Body:

@XmlRootElement
public class Item {
    private int mItemId;
    private String mName;
    private float mPrice;

    public Item(){

    }

    public Item(int id, String name, float price){
        mItemId = id;
        mName = name;
        mPrice = price;
    }
    public int getItemId() {
        return mItemId;
    }
    public void setItemId(int mItemId) {
        this.mItemId = mItemId;
    }
    public String getItemName() {
        return mName;
    }
    public void setItemName(String mName) {
        this.mName = mName;
    }
    public float getItemPrice() {
        return mPrice;
    }
    public void setItemPrice(float mPrice) {
        this.mPrice = mPrice;
    }
}

通过上面的JaxRS POST消息设置,我总是得到参数类型不匹配错误,如下所示:

{"itemOrderList":[
{"item":{"itemId":11,"itemName":"Tea","itemPrice":10.0},"numberOfPlates":5},
{"item":{"itemId":22,"itemName":"Coffee","itemPrice":20.0},"numberOfPlates":5},
{"item":{"itemId":33,"itemName":"Bread","itemPrice":30.0},"numberOfPlates":5}
    ]
}

我真的很无能为力。我甚至为ItemOrder类编写了一个MessageBodyReader,如下所示。什么都没有用。

SEVERE: Servlet.service() for servlet [com.somitsolutions.training.java.restaurant1.App] in context with path [/restaurant1] threw exception [javax.ws.rs.ProcessingException: Resource Java method invocation error.] with root cause
java.lang.IllegalArgumentException: argument type mismatch
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:143)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:160)
    at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$TypeOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:203)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:97)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)
    at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:303)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
    at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:286)
    at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1072)
    at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:399)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:381)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:344)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:221)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:521)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1096)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:674)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

1 个答案:

答案 0 :(得分:0)

是的,我知道有时候我表现得如此愚蠢。 POST方法的JSON有效负载应为

[
    {
        "item": {
            "itemId": 11,
            "itemName": "Tea",
            "itemPrice": 10
        },
        "numberOfPlates": 5
    },
    {
        "item": {
            "itemId": 22,
            "itemName": "Coffee",
            "itemPrice": 20
        },
        "numberOfPlates": 5
    },
    {
        "item": {
            "itemId": 33,
            "itemName": "Bread",
            "itemPrice": 30
        },
        "numberOfPlates": 5
    }
]