Spring RESTful,错误返回JSON

时间:2017-08-23 09:08:48

标签: java json spring hibernate rest

我在从DAO生成JSON时遇到错误,这是我的代码

控制器

@RequestMapping(value="/findAdpId/", method=RequestMethod.POST , consumes = "application/json", produces = "application/json")
public @ResponseBody AllCustomerHist findAdpId(@RequestBody AllCustomerHist customer){
    String customerId = customer.getCustomerId();
    String srctable = customer.getSrctable();

    System.out.println("customer ID = "+customerId);
    System.out.println("srctable = "+srctable);
    List<AllCustomerHist> adpcust = allCustomerHistService.findAdpId(customerId, srctable);
    return (AllCustomerHist) adpcust;       
}

DAO课程

@SuppressWarnings("unchecked")
public List<AllCustomerHist> findAdpId(String customerId, String srctable) {
    // TODO Auto-generated method stub
    Criteria criteria = getSession().createCriteria(AllCustomerHist.class)
            .setProjection(Projections.projectionList()
                    .add(Projections.property("adpId"), "adpId"))
    .add(Restrictions.eq("customerId", customerId))
    .add(Restrictions.eq("srctable", srctable));

    return (List<AllCustomerHist>)criteria.list();
}

我想要的是,在检索像

这样的JSON时
{
"customerId":"11",
"srctable":"transaction"
}

我的DAO课程将产生

{
"adpId":["abcd123","defw123"]
}

这是错误日志

  

异常

     

org.springframework.web.util.NestedServletException:Request   处理失败;嵌套异常是java.lang.ClassCastException:   java.util.ArrayList无法强制转换为   com.astra.adp.model.AllCustomerHist

     

org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982)

     

org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)

     

javax.servlet.http.HttpServlet.service(HttpServlet.java:661)

     

org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)

     

javax.servlet.http.HttpServlet.service(HttpServlet.java:742)

     

org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

     

根本原因

     

java.lang.ClassCastException:java.util.ArrayList无法强制转换为   com.astra.adp.model.AllCustomerHist

     

com.astra.adp.controller.CustomerController.findAdpId(CustomerController.java:73)

     

sun.reflect.NativeMethodAccessorImpl.invoke0(原生方法)

     

sun.reflect.NativeMethodAccessorImpl.invoke(未知来源)

     

sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)

     

java.lang.reflect.Method.invoke(Unknown Source)

     

org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)

     

org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)

     

org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:114)

     

org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)

     

org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)

     

org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)

     

org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)

     

org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)

     

org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)

     

org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)

     

javax.servlet.http.HttpServlet.service(HttpServlet.java:661)

     

org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)

     

javax.servlet.http.HttpServlet.service(HttpServlet.java:742)

     

org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

     

注意服务器中提供了根本原因的完整堆栈跟踪   日志。

谢谢你的帮助!

0 个答案:

没有答案