java.lang.ClassCastException:java.util.LinkedHashMap无法强制转换为com。****。db。*****

时间:2015-01-20 05:25:28

标签: java spring

我是Spring框架的新手。我正在尝试使用RESTClient(application / json)发布JSON数据。但是我收到以下错误。 (春季版3.2)

错误:

  2015-01-20 13:16:55,996 INFO  [com.***.ui.restcontroller.PermissionGroup.PermissionGroupController] ADD PERMISSIONGROUP
2015-01-20 13:16:56,002 INFO  [STDOUT] Hibernate: select tnmaxid0_.ENTITY_TYPE as ENTITY1_149_0_, tnmaxid0_.MAX_ID as MAX2_149_0_ from tagnos.tn_max_id tnmaxid0_ where tnmaxid0_.ENTITY_TYPE=?
2015-01-20 13:16:56,003 INFO  [STDOUT] Hibernate: update tagnos.tn_max_id set MAX_ID=? where ENTITY_TYPE=?
2015-01-20 13:16:56,061 ERROR [org.jboss.ejb.plugins.LogInterceptor] RuntimeException in method: public abstract com.***.db.TnPermissionGroup com.***.ejb.UserManagerLocal.addPermissionGroup(com.***.db.TnPermissionGroup) throws java.lang.Exception:
java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.***.db.TnPermission
    at com.***.ejb.UserManagerEJB.addPermissionGroup(UserManagerEJB.java:521)
    at com.***.ejb.UserManagerEJB.addPermissionGroup(UserManagerEJB.java:495)
    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:622)
    at org.jboss.invocation.Invocation.performCall(Invocation.java:359)
    at org.jboss.ejb.StatelessSessionContainer$ContainerInterceptor.invoke(StatelessSessionContainer.java:237)
    at org.jboss.resource.connectionmanager.CachedConnectionInterceptor.invoke(CachedConnectionInterceptor.java:158)
    at org.jboss.ejb.plugins.StatelessSessionInstanceInterceptor.invoke(StatelessSessionInstanceInterceptor.java:169)
    at org.jboss.ejb.plugins.CallValidationInterceptor.invoke(CallValidationInterceptor.java:63)
    at org.jboss.ejb.plugins.AbstractTxInterceptor.invokeNext(AbstractTxInterceptor.java:121)
    at org.jboss.ejb.plugins.TxInterceptorCMT.runWithTransactions(TxInterceptorCMT.java:350)
    at org.jboss.ejb.plugins.TxInterceptorCMT.invoke(TxInterceptorCMT.java:181)
    at org.jboss.ejb.plugins.SecurityInterceptor.invoke(SecurityInterceptor.java:168)
    at org.jboss.ejb.plugins.LogInterceptor.invoke(LogInterceptor.java:205)
    at org.jboss.ejb.plugins.ProxyFactoryFinderInterceptor.invoke(ProxyFactoryFinderInterceptor.java:138)
    at org.jboss.ejb.SessionContainer.internalInvoke(SessionContainer.java:648)
    at org.jboss.ejb.Container.invoke(Container.java:960)
    at org.jboss.ejb.plugins.local.BaseLocalProxyFactory.invoke(BaseLocalProxyFactory.java:430)
    at org.jboss.ejb.plugins.local.StatelessSessionProxy.invoke(StatelessSessionProxy.java:103)
    at com.sun.proxy.$Proxy300.addPermissionGroup(Unknown Source)
    at com.***.business.UserManagerDelegate.addPermissionGroup(UserManagerDelegate.java:236)
    at com.***.ui.restcontroller.PermissionGroup.PermissionGroupController.addPermissionGroup(PermissionGroupController.java:125)
    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:622)
    at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:920)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:827)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:801)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:179)
    at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
    at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:241)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:580)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
    at java.lang.Thread.run(Thread.java:701)

JSON格式:

{  
   "name":"Test",
   "tnPermissions":[  
      {  
         "comp_id":{  
            "permGroupId":"2",
            "functionId":"100",
            "operationType":"5"
         },
         "allow":"1"
      }
   ]
}

Controller.java

 /**************** ADD PERMISSIONGROUP **********************/

     @RequestMapping(method=RequestMethod.POST,consumes="application/json")
     @ResponseStatus(HttpStatus.CREATED)
     public @ResponseBody Object addPermissionGroup(@RequestBody PermissionGroupRequestCriteria permissiongroupRequestCriteria,BindingResult bindingResult) throws Exception{
         TnPermissionGroup grp = null;

        try{
            permissiongroupValidator.validate(permissiongroupRequestCriteria, bindingResult);
            if(bindingResult.hasErrors())
            {
                Map<String,String> fieldErrorMap = new HashMap<String,String>();
                for(FieldError fieldError : bindingResult.getFieldErrors())
                {
                     fieldErrorMap.put(fieldError.getField(), fieldError.getDefaultMessage());
                }
                String localizedErrorMessage = messageSource.getMessage("permissiongroup.insert.unsuccessful", null, currentLocale);
                ErrorResponse resp =new ErrorResponse(localizedErrorMessage);
                resp.setErrorMessages(fieldErrorMap);
                return new ResponseEntity<ErrorResponse>(resp,HttpStatus.BAD_REQUEST);
            }


            grp = new TnPermissionGroup(permissiongroupRequestCriteria.getPermGroupId(),permissiongroupRequestCriteria.getName(),permissiongroupRequestCriteria.getDescription(),permissiongroupRequestCriteria.getTnPermissions());

            grp = userDelegate.addPermissionGroup(grp);

        } catch(Exception e){
            String localizedErrorMessage = messageSource.getMessage("permissiongroup.insert.unsuccessful", null, currentLocale);
            ErrorResponse resp = new ErrorResponse(localizedErrorMessage);
            return new ResponseEntity<ErrorResponse>(resp,HttpStatus.BAD_REQUEST);
        }
        argumentsToReplace[0]= grp.getName();
        String localizedErrorMessage = messageSource.getMessage("permissiongroup.insert.successful", argumentsToReplace, currentLocale);
        EmptySuccessResponse resp = new EmptySuccessResponse(localizedErrorMessage);
        resp.setResource(grp);
        return new ResponseEntity<EmptySuccessResponse>(resp, HttpStatus.CREATED); 
    }

TnPermissionGroup.java

public class TnPermissionGroup implements Serializable {

    private Integer permGroupId;

    private String name;

    private String description;

    private Set tnPermissions;

    public TnPermissionGroup() {
    }

    public TnPermissionGroup(Integer permGroupId, String name, Set tnPermissions) {
        this.permGroupId = permGroupId;
        this.name = name;
        this.tnPermissions = tnPermissions;
    }

    /** full constructor */
    public TnPermissionGroup(Integer permGroupId, String name, String description, Set tnPermissions) {
        this.permGroupId = permGroupId;
        this.name = name;
        this.description = description;
        this.tnPermissions = tnPermissions;
    }

    public Integer getPermGroupId() {
        return this.permGroupId;
    }

    public void setPermGroupId(Integer permGroupId) {
        this.permGroupId = permGroupId;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return this.description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Set getTnPermissions() {
        return this.tnPermissions;
    }

    public void setTnPermissions(Set tnPermissions) {
        this.tnPermissions = tnPermissions;
    }

    public void addPermission(TnPermission perm)
    {
        if (tnPermissions == null) {
            tnPermissions = new HashSet();
        }
        tnPermissions.add(perm);
    }

}

TnPermission.java

  public class TnPermission implements Serializable {

        private com.***.db.TnPermissionPK comp_id;

        private Byte allow;

        /** full constructor */
        public TnPermission(com.***.db.TnPermissionPK comp_id, Byte allow) {
            this.comp_id = comp_id;
            this.allow = allow;
        }

        /** default constructor */
        public TnPermission() {
        }

        /** minimal constructor */
        public TnPermission(com.***.db.TnPermissionPK comp_id) {
            this.comp_id = comp_id;
        }

        public com.***.db.TnPermissionPK getComp_id() {
            return this.comp_id;
        }

        public void setComp_id(com.***.db.TnPermissionPK comp_id) {
            this.comp_id = comp_id;
        }

        public Byte getAllow() {
            return this.allow;
        }

        public void setAllow(Byte allow) {
            this.allow = allow;
        }
    }

TnPermissionPK.java

public class TnPermissionPK implements Serializable {

    private Integer permGroupId;
    private Integer functionId;
    private Integer operationType;

    /** full constructor */
    public TnPermissionPK(Integer permGroupId, Integer functionId, Integer operationType) {
        this.permGroupId = permGroupId;
        this.functionId = functionId;
        this.operationType = operationType;
    }

    /** default constructor */
    public TnPermissionPK() {
    }

    public Integer getPermGroupId() {
        return this.permGroupId;
    }

    public void setPermGroupId(Integer permGroupId) {
        this.permGroupId = permGroupId;
    }

    public Integer getFunctionId() {
        return this.functionId;
    }

    public void setFunctionId(Integer functionId) {
        this.functionId = functionId;
    }

    public Integer getOperationType() {
        return this.operationType;
    }

    public void setOperationType(Integer operationType) {
        this.operationType = operationType;
    }
}

为什么JSON数据POST没有解析。我认为JSON格式是错误的。  请帮帮我。

1 个答案:

答案 0 :(得分:0)

为什么不接受json作为服务方法的字符串输入,然后使用Jackson或Gson将它们解析为java对象(在您的情况下为PermissionGroupRequestCriteria),这样您就能够处理将错误的json传递给服务时的条件。