相关文章推荐
豪情万千的小刀  ·  Android ...·  10 月前    · 
温柔的汉堡包  ·  网络工程师的 Python ...·  2 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Failed to convert value of type 'java.lang.String' to required type 'example.entity.Address'

Ask Question <p th:errors = "*{address}" class="text-danger" th:if="${#fields.hasErrors('address')}"></p> <div class="form-group mb-3"> <label class="form-label">Postal Code</label> <input class="form-control" id="postalcode" name="postalcode" placeholder="Enter Postal Code" th:field="*{postalcode}" type="text" <p th:errors = "*{postalcode}" class="text-danger" th:if="${#fields.hasErrors('postalcode')}"></p> <div class="form-group mb-3"> <label class="form-label">City</label> <input class="form-control" id="city" name="city" placeholder="Enter City" th:field="*{city}" type="text" <p th:errors = "*{city}" class="text-danger" th:if="${#fields.hasErrors('city')}"></p> <div class="form-group mb-3"> <label class="form-label">State</label> <input class="form-control" id="state" name="state" placeholder="Enter State" th:field="*{state}" type="text" <p th:errors = "*{state}" class="text-danger" th:if="${#fields.hasErrors('state')}"></p> <div class="form-group mb-3"> <label class="form-label">Country</label> <input class="form-control" id="country" name="country" placeholder="Enter Country" th:field="*{country}" type="text" <p th:errors = "*{country}" class="text-danger" th:if="${#fields.hasErrors('country')}"></p> <div class="form-group" style="text-align:center"> <button class="btn btn-success" type="submit">Save</button> </form> Controller: @Autowired private AddressRepository addressRepository; @GetMapping("/address/{id}") public String addAddress(@PathVariable("id") Long add_id, Model model){ System.out.println("\nInside addressform :\n"); @SuppressWarnings("deprecation") Address address = addressRepository.getById(add_id); System.out.println(address.getAdd_id()); System.out.println("add_id : "+add_id); model.addAttribute("address", address); return "addressform"; // handler method to handle post request to save address @PostMapping("/address/save") public String saveAddress(@Valid @ModelAttribute("address") Address address) { System.out.println("\nInside address save method."); System.out.println(address.getAdd_id()); System.out.println(address.getAddress()); System.out.println(address.getPostalcode()); System.out.println(address.toString()); addressRepository.save(address); return "redirect:/users"; ERROR : There was an unexpected error (type=Bad Request, status=400).

Failed to convert value of type 'java.lang.String' to required type 'example.entity.Address'; Failed to convert from type [java.lang.String] to type [java.lang.Long] for value 'madhapur' org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'example.entity.Address'; Failed to convert from type [java.lang.String] to type [java.lang.Long] for value 'madhapur' at org.springframework.beans.TypeConverterSupport.convertIfNecessary(TypeConverterSupport.java:79) at org.springframework.beans.TypeConverterSupport.convertIfNecessary(TypeConverterSupport.java:53) at org.springframework.validation.DataBinder.convertIfNecessary(DataBinder.java:729) at org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.createAttributeFromRequestValue(ServletModelAttributeMethodProcessor.java:142) at org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.createAttribute(ServletModelAttributeMethodProcessor.java:78) at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:147) at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:122) at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:181) at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:148) at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:884) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797) at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)

Please add more information to the question. How you are passing the value to the object. What values you are passing. Without this info it is difficult to answer the question. Ashish Kathait Jan 3 at 5:50

Firstly I figured out couple of codes that can cause serious performance issues and is not advised.

Here's a few:

Take off your Generated Getters and Setters and Leave the annotation @Getter and @Setter to do the work.

Take off <input type="hidden" th:field="*{add_id}" /> as it will be auto-generated from your entity Address private Long add_id

Take off address.toString() from your saveAddress controller as this is not needed to parse data to your Entity, your entity receives String already which will be parsed from your form, there is no need to convert with toString again.

One of your error states Failed to convert from type [java.lang.String] to type [java.lang.Long] which simply means you're trying to convert Long to String which should be auto generated by your Entity

I hope this helps!!!

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question . Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers .