title: spring mvc form bean convertion from string to long
body: <p>I have a very simple crud. It´s composed by a bean that represent form data. I have a field with a long value. In jsp i write this: <code>&lt;form:hidden path="id"/&gt;</code>. When i submit the form i get HTTP Status 400 error.</p>
  
  <p>in short when a submit a long value spring mvc can not convert it to long to construct my form object.</p>
  
  <p>the flow is simple: the user click edit over e company. the controller via a service retrieve the company, then the view is rendered, and finaly the user submit changes.. but well when the long value is submit i got the error.</p>
  
  <p>The debug information tells me this: </p>
  
  <pre><code>DefaultHandlerExceptionResolver [DEBUG] Resolving exception from handler [public java.lang.String ar.com.held.controller.CompanyController.edit(java.lang.Long,org.springframework.ui.Model)]: org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long'; nested exception is java.lang.NumberFormatException: For input string: "save"
  </code></pre>
  
  <p>here is the code:</p>
  
  <p>my form bean:</p>
  
  <pre><code>public class CompanyForm {
  
      private Long id;
  
      public Long getId() {
          return id;
      }
  
      public void setId(Long id) {
          this.id = id;
      }
  
      @NotBlank
      private String name;
      @NotBlank
      private String addressStreet;
      @NotBlank
      private String addressCity;
      @NotBlank
      private String addressState;
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      public String getAddressStreet() {
          return addressStreet;
      }
  
      public void setAddressStreet(String addressStreet) {
          this.addressStreet = addressStreet;
      }
  
      public String getAddressCity() {
          return addressCity;
      }
  
      public void setAddressCity(String addressCity) {
          this.addressCity = addressCity;
      }
  
      public String getAddressState() {
          return addressState;
      }
  
      public void setAddressState(String addressState) {
          this.addressState = addressState;
      }
  
  }
  </code></pre>
  
  <p>my controller:</p>
  
  <pre><code>@RequestMapping(value={"edit/{companyId}"})
      public String edit(@PathVariable Long companyId, Model model){
          CompanyForm form = new CompanyForm();
          if(companyId!=null){
              Company company = companyService.find(companyId);
              if(company!=null)
                  modelMapper.map(company, form);
          }
          model.addAttribute("form", form);
          return "company/edit";
      }
  
      @RequestMapping(value={"new"})
      public String edit(Model model){
          return this.edit(null, model);
      }
  
      @RequestMapping(value="save", method={RequestMethod.POST})
      public String save(@ModelAttribute("form") @Valid CompanyForm companyForm, BindingResult result){
          if(result.hasErrors())
              return "company/edit";
          Company company;
          if(companyForm.getId()!=null)
              company = companyService.find(companyForm.getId());
          else
              company = new Company();
          modelMapper.map(companyForm, company);
          companyService.save(company, getLoggedUser());
          return "redirect:list";
      }
  </code></pre>
  
  <p>my jsp:</p>
  
  <pre><code>&lt;form:form method="post" action="save" modelAttribute="form" cssClass="form-horizontal" &gt;
          &lt;fieldset&gt;
  
              &lt;form:hidden path="id"/&gt;
              ++++  others fields +++
              &lt;div class="form-actions"&gt;
                  &lt;input type="submit" value="Guardar" class="btn btn-primary btn-large"/&gt;
              &lt;/div&gt;
          &lt;/fieldset&gt;
      &lt;/form:form&gt;
  </code></pre>
  
  <p>debug information</p>
  
  <pre><code>2012-02-17 18:17:45 AntPathRequestMatcher [DEBUG] Checking match of request : '/companies/edit/save'; against '/resources/**'
  2012-02-17 18:17:45 FilterChainProxy [DEBUG] /companies/edit/save at position 1 of 10 in additional filter chain; firing Filter: 'SecurityContextPersistenceFilter'
  2012-02-17 18:17:45 HttpSessionSecurityContextRepository [DEBUG] Obtained a valid SecurityContext from SPRING_SECURITY_CONTEXT: 'org.springframework.security.core.context.SecurityContextImpl@bcc22d3c: Authentication: org.springframework.security.authentication.UsernamePasswordAuthenticationToken@bcc22d3c: Principal: ar.com.held.auth.UserDetails@1421801; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@166c8: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: 93B19E8B81EBB7F4BFEECF4FFFC46593; Granted Authorities: COMPANY_OWNER'
  2012-02-17 18:17:45 FilterChainProxy [DEBUG] /companies/edit/save at position 2 of 10 in additional filter chain; firing Filter: 'LogoutFilter'
  2012-02-17 18:17:45 FilterChainProxy [DEBUG] /companies/edit/save at position 3 of 10 in additional filter chain; firing Filter: 'UsernamePasswordAuthenticationFilter'
  2012-02-17 18:17:45 FilterChainProxy [DEBUG] /companies/edit/save at position 4 of 10 in additional filter chain; firing Filter: 'DefaultLoginPageGeneratingFilter'
  2012-02-17 18:17:45 FilterChainProxy [DEBUG] /companies/edit/save at position 5 of 10 in additional filter chain; firing Filter: 'RequestCacheAwareFilter'
  2012-02-17 18:17:45 FilterChainProxy [DEBUG] /companies/edit/save at position 6 of 10 in additional filter chain; firing Filter: 'SecurityContextHolderAwareRequestFilter'
  2012-02-17 18:17:45 FilterChainProxy [DEBUG] /companies/edit/save at position 7 of 10 in additional filter chain; firing Filter: 'AnonymousAuthenticationFilter'
  2012-02-17 18:17:45 AnonymousAuthenticationFilter [DEBUG] SecurityContextHolder not populated with anonymous token, as it already contained: 'org.springframework.security.authentication.UsernamePasswordAuthenticationToken@bcc22d3c: Principal: ar.com.held.auth.UserDetails@1421801; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@166c8: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: 93B19E8B81EBB7F4BFEECF4FFFC46593; Granted Authorities: COMPANY_OWNER'
  2012-02-17 18:17:45 FilterChainProxy [DEBUG] /companies/edit/save at position 8 of 10 in additional filter chain; firing Filter: 'SessionManagementFilter'
  2012-02-17 18:17:45 FilterChainProxy [DEBUG] /companies/edit/save at position 9 of 10 in additional filter chain; firing Filter: 'ExceptionTranslationFilter'
  2012-02-17 18:17:45 FilterChainProxy [DEBUG] /companies/edit/save at position 10 of 10 in additional filter chain; firing Filter: 'FilterSecurityInterceptor'
  2012-02-17 18:17:45 AntPathRequestMatcher [DEBUG] Checking match of request : '/companies/edit/save'; against '/companies/*'
  2012-02-17 18:17:45 FilterSecurityInterceptor [DEBUG] Public object - authentication not attempted
  2012-02-17 18:17:45 FilterChainProxy [DEBUG] /companies/edit/save reached end of additional filter chain; proceeding with original chain
  2012-02-17 18:17:45 DispatcherServlet [DEBUG] DispatcherServlet with name 'spring' processing POST request for [/Held/companies/edit/save]
  2012-02-17 18:17:45 RequestMappingHandlerMapping [DEBUG] Looking up handler method for path /companies/edit/save
  2012-02-17 18:17:45 RequestMappingHandlerMapping [DEBUG] Returning handler method [public java.lang.String ar.com.held.controller.CompanyController.edit(java.lang.Long,org.springframework.ui.Model)]
  2012-02-17 18:17:45 DefaultListableBeanFactory [DEBUG] Returning cached instance of singleton bean 'companyController'
  2012-02-17 18:17:45 StandardEnvironment [DEBUG] Initializing new StandardEnvironment
  2012-02-17 18:17:45 StandardEnvironment [DEBUG] Adding [systemProperties] PropertySource with lowest search precedence
  2012-02-17 18:17:45 StandardEnvironment [DEBUG] Adding [systemEnvironment] PropertySource with lowest search precedence
  2012-02-17 18:17:45 StandardEnvironment [DEBUG] Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment]
  2012-02-17 18:17:45 StandardEnvironment [DEBUG] Initializing new StandardEnvironment
  2012-02-17 18:17:45 StandardEnvironment [DEBUG] Adding [systemProperties] PropertySource with lowest search precedence
  2012-02-17 18:17:45 StandardEnvironment [DEBUG] Adding [systemEnvironment] PropertySource with lowest search precedence
  2012-02-17 18:17:45 StandardEnvironment [DEBUG] Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment]
  2012-02-17 18:17:45 StandardEnvironment [DEBUG] Initializing new StandardEnvironment
  2012-02-17 18:17:45 StandardEnvironment [DEBUG] Adding [systemProperties] PropertySource with lowest search precedence
  2012-02-17 18:17:45 StandardEnvironment [DEBUG] Adding [systemEnvironment] PropertySource with lowest search precedence
  2012-02-17 18:17:45 StandardEnvironment [DEBUG] Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment]
  2012-02-17 18:17:45 StandardEnvironment [DEBUG] Initializing new StandardEnvironment
  2012-02-17 18:17:45 StandardEnvironment [DEBUG] Adding [systemProperties] PropertySource with lowest search precedence
  2012-02-17 18:17:45 StandardEnvironment [DEBUG] Adding [systemEnvironment] PropertySource with lowest search precedence
  2012-02-17 18:17:45 StandardEnvironment [DEBUG] Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment]
  2012-02-17 18:17:45 StandardEnvironment [DEBUG] Initializing new StandardEnvironment
  2012-02-17 18:17:45 StandardEnvironment [DEBUG] Adding [systemProperties] PropertySource with lowest search precedence
  2012-02-17 18:17:45 StandardEnvironment [DEBUG] Adding [systemEnvironment] PropertySource with lowest search precedence
  2012-02-17 18:17:45 StandardEnvironment [DEBUG] Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment]
  2012-02-17 18:17:45 ExceptionHandlerExceptionResolver [DEBUG] Resolving exception from handler [public java.lang.String ar.com.held.controller.CompanyController.edit(java.lang.Long,org.springframework.ui.Model)]: org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long'; nested exception is java.lang.NumberFormatException: For input string: "save"
  2012-02-17 18:17:45 ResponseStatusExceptionResolver [DEBUG] Resolving exception from handler [public java.lang.String ar.com.held.controller.CompanyController.edit(java.lang.Long,org.springframework.ui.Model)]: org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long'; nested exception is java.lang.NumberFormatException: For input string: "save"
  2012-02-17 18:17:45 DefaultHandlerExceptionResolver [DEBUG] Resolving exception from handler [public java.lang.String ar.com.held.controller.CompanyController.edit(java.lang.Long,org.springframework.ui.Model)]: org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long'; nested exception is java.lang.NumberFormatException: For input string: "save"
  2012-02-17 18:17:45 DispatcherServlet [DEBUG] Null ModelAndView returned to DispatcherServlet with name 'spring': assuming HandlerAdapter completed request handling
  2012-02-17 18:17:45 DispatcherServlet [DEBUG] Successfully completed request
  2012-02-17 18:17:45 ExceptionTranslationFilter [DEBUG] Chain processed normally
  2012-02-17 18:17:45 SecurityContextPersistenceFilter [DEBUG] SecurityContextHolder now cleared, as request processing completed
  2012-02-17 18:26:56 AntPathRequestMatcher [DEBUG] Checking match of request : '/companies/edit/save'; against '/resources/**'
  2012-02-17 18:26:56 FilterChainProxy [DEBUG] /companies/edit/save at position 1 of 10 in additional filter chain; firing Filter: 'SecurityContextPersistenceFilter'
  2012-02-17 18:26:56 HttpSessionSecurityContextRepository [DEBUG] Obtained a valid SecurityContext from SPRING_SECURITY_CONTEXT: 'org.springframework.security.core.context.SecurityContextImpl@bcc22d3c: Authentication: org.springframework.security.authentication.UsernamePasswordAuthenticationToken@bcc22d3c: Principal: ar.com.held.auth.UserDetails@1421801; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@166c8: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: 93B19E8B81EBB7F4BFEECF4FFFC46593; Granted Authorities: COMPANY_OWNER'
  2012-02-17 18:26:56 FilterChainProxy [DEBUG] /companies/edit/save at position 2 of 10 in additional filter chain; firing Filter: 'LogoutFilter'
  2012-02-17 18:26:56 FilterChainProxy [DEBUG] /companies/edit/save at position 3 of 10 in additional filter chain; firing Filter: 'UsernamePasswordAuthenticationFilter'
  2012-02-17 18:26:56 FilterChainProxy [DEBUG] /companies/edit/save at position 4 of 10 in additional filter chain; firing Filter: 'DefaultLoginPageGeneratingFilter'
  2012-02-17 18:26:56 FilterChainProxy [DEBUG] /companies/edit/save at position 5 of 10 in additional filter chain; firing Filter: 'RequestCacheAwareFilter'
  2012-02-17 18:26:56 FilterChainProxy [DEBUG] /companies/edit/save at position 6 of 10 in additional filter chain; firing Filter: 'SecurityContextHolderAwareRequestFilter'
  2012-02-17 18:26:56 FilterChainProxy [DEBUG] /companies/edit/save at position 7 of 10 in additional filter chain; firing Filter: 'AnonymousAuthenticationFilter'
  2012-02-17 18:26:56 AnonymousAuthenticationFilter [DEBUG] SecurityContextHolder not populated with anonymous token, as it already contained: 'org.springframework.security.authentication.UsernamePasswordAuthenticationToken@bcc22d3c: Principal: ar.com.held.auth.UserDetails@1421801; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@166c8: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: 93B19E8B81EBB7F4BFEECF4FFFC46593; Granted Authorities: COMPANY_OWNER'
  2012-02-17 18:26:56 FilterChainProxy [DEBUG] /companies/edit/save at position 8 of 10 in additional filter chain; firing Filter: 'SessionManagementFilter'
  2012-02-17 18:26:56 FilterChainProxy [DEBUG] /companies/edit/save at position 9 of 10 in additional filter chain; firing Filter: 'ExceptionTranslationFilter'
  2012-02-17 18:26:56 FilterChainProxy [DEBUG] /companies/edit/save at position 10 of 10 in additional filter chain; firing Filter: 'FilterSecurityInterceptor'
  2012-02-17 18:26:56 AntPathRequestMatcher [DEBUG] Checking match of request : '/companies/edit/save'; against '/companies/*'
  2012-02-17 18:26:56 FilterSecurityInterceptor [DEBUG] Public object - authentication not attempted
  2012-02-17 18:26:56 FilterChainProxy [DEBUG] /companies/edit/save reached end of additional filter chain; proceeding with original chain
  2012-02-17 18:26:56 DispatcherServlet [DEBUG] DispatcherServlet with name 'spring' processing POST request for [/Held/companies/edit/save]
  2012-02-17 18:26:56 RequestMappingHandlerMapping [DEBUG] Looking up handler method for path /companies/edit/save
  2012-02-17 18:26:56 RequestMappingHandlerMapping [DEBUG] Returning handler method [public java.lang.String ar.com.held.controller.CompanyController.edit(java.lang.Long,org.springframework.ui.Model)]
  2012-02-17 18:26:56 DefaultListableBeanFactory [DEBUG] Returning cached instance of singleton bean 'companyController'
  2012-02-17 18:26:56 StandardEnvironment [DEBUG] Initializing new StandardEnvironment
  2012-02-17 18:26:56 StandardEnvironment [DEBUG] Adding [systemProperties] PropertySource with lowest search precedence
  2012-02-17 18:26:56 StandardEnvironment [DEBUG] Adding [systemEnvironment] PropertySource with lowest search precedence
  2012-02-17 18:26:56 StandardEnvironment [DEBUG] Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment]
  2012-02-17 18:26:56 StandardEnvironment [DEBUG] Initializing new StandardEnvironment
  2012-02-17 18:26:56 StandardEnvironment [DEBUG] Adding [systemProperties] PropertySource with lowest search precedence
  2012-02-17 18:26:56 StandardEnvironment [DEBUG] Adding [systemEnvironment] PropertySource with lowest search precedence
  2012-02-17 18:26:56 StandardEnvironment [DEBUG] Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment]
  2012-02-17 18:26:56 StandardEnvironment [DEBUG] Initializing new StandardEnvironment
  2012-02-17 18:26:56 StandardEnvironment [DEBUG] Adding [systemProperties] PropertySource with lowest search precedence
  2012-02-17 18:26:56 StandardEnvironment [DEBUG] Adding [systemEnvironment] PropertySource with lowest search precedence
  2012-02-17 18:26:56 StandardEnvironment [DEBUG] Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment]
  2012-02-17 18:26:56 StandardEnvironment [DEBUG] Initializing new StandardEnvironment
  2012-02-17 18:26:56 StandardEnvironment [DEBUG] Adding [systemProperties] PropertySource with lowest search precedence
  2012-02-17 18:26:56 StandardEnvironment [DEBUG] Adding [systemEnvironment] PropertySource with lowest search precedence
  2012-02-17 18:26:56 StandardEnvironment [DEBUG] Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment]
  2012-02-17 18:26:56 StandardEnvironment [DEBUG] Initializing new StandardEnvironment
  2012-02-17 18:26:56 StandardEnvironment [DEBUG] Adding [systemProperties] PropertySource with lowest search precedence
  2012-02-17 18:26:56 StandardEnvironment [DEBUG] Adding [systemEnvironment] PropertySource with lowest search precedence
  2012-02-17 18:26:56 StandardEnvironment [DEBUG] Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment]
  2012-02-17 18:26:56 ExceptionHandlerExceptionResolver [DEBUG] Resolving exception from handler [public java.lang.String ar.com.held.controller.CompanyController.edit(java.lang.Long,org.springframework.ui.Model)]: org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long'; nested exception is java.lang.NumberFormatException: For input string: "save"
  2012-02-17 18:26:56 ResponseStatusExceptionResolver [DEBUG] Resolving exception from handler [public java.lang.String ar.com.held.controller.CompanyController.edit(java.lang.Long,org.springframework.ui.Model)]: org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long'; nested exception is java.lang.NumberFormatException: For input string: "save"
  2012-02-17 18:26:56 DefaultHandlerExceptionResolver [DEBUG] Resolving exception from handler [public java.lang.String ar.com.held.controller.CompanyController.edit(java.lang.Long,org.springframework.ui.Model)]: org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long'; nested exception is java.lang.NumberFormatException: For input string: "save"
  2012-02-17 18:26:56 DispatcherServlet [DEBUG] Null ModelAndView returned to DispatcherServlet with name 'spring': assuming HandlerAdapter completed request handling
  2012-02-17 18:26:56 DispatcherServlet [DEBUG] Successfully completed request
  2012-02-17 18:26:56 ExceptionTranslationFilter [DEBUG] Chain processed normally
  2012-02-17 18:26:56 SecurityContextPersistenceFilter [DEBUG] SecurityContextHolder now cleared, as request processing completed
  </code></pre>