Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I am new to spring framework. I am trying to POST JSON data using RESTClient (application/json). But I am getting the following error. (Spring version 3.2)

ERROR :

  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 Format:

{  
   "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;
    }
}

Why JSON data POST is not parsing. I think JSON format is wrong. Help me please.

share|improve this question
    
JSON format is ok. What you are missing is a set of annotations on your TnPermission* classes that would tell the JAX-RS implementation you are using (Jersey?) how to parse and assign data from JSON. – R4J Jan 20 '15 at 7:16
    
Is this the line the exception is being thrown from: grp = userDelegate.addPermissionGroup(grp); ? Can you give us full stacktrace? – R4J Jan 20 '15 at 7:42
    
Now, you can see the full stacktrace in the question – N123 Jan 20 '15 at 8:51
    
Is this line from stacktrace (at com.***.ejb.UserManagerEJB.addPermissionGroup(UserManagerEJB.java:521)) equal to this line from code snippet (grp = userDelegate.addPermissionGroup(grp);) ? – R4J Jan 20 '15 at 9:15
    
I think, you are right both are equal. – N123 Jan 20 '15 at 9:26

Why don't you accept the json as string input to service method and then parse them to java object (in your case PermissionGroupRequestCriteria) using Jackson or Gson, in this way you will be able to handle condition when wrong json is passed to service.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.