Sachin
Sachin

Reputation: 139

IllegalArgumentException by Spring controller

I have a view(jsp) which use jquery to show/hide some field. I am using jquery to post data using ajax to the controller(spring mvc). Works good when I simply entered values in the page but if I click on a link to unhide 2 text boxes, input value into them and then send to the controller I got this exception. I have checked spring logs and I can see all properties were converted to bind with spring but soon after that exception was raised. Below is what I can see in the logs. I have checked all parameters multiple times and nothing is missing/misspelled in jsp/js/java. Any help would be much appreciated

 19:40:16,875 DEBUG GenericConversionService:254 - Caching under ConverterCacheKey [sourceType = java.lang.String, targetType = @org.springframework.web.bind.annotation.RequestParam java.lang.String]
java.lang.String -> java.lang.Number : org.springframework.core.convert.support.StringToNumberConverterFactory@21e8bf76
19:40:17,640 DEBUG GenericConversionService:186 - Converted to 540
19:40:17,644 DEBUG GenericConversionService:139 - Checking if I can convert java.lang.String to @org.springframework.web.bind.annotation.RequestParam int
19:40:17,646 DEBUG GenericConversionService:358 - Searching for converters indexed by sourceType [java.lang.String]
19:40:17,647 DEBUG GenericConversionService:429 - and indexed by targetType [java.lang.Integer]
19:40:17,649 DEBUG GenericConversionService:473 - Found matchable converters java.lang.String -> @org.springframework.format.annotation.NumberFormat java.lang.Integer: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@4a6ca1a6
19:40:17,651 DEBUG GenericConversionService:560 - Matching java.lang.String -> @org.springframework.format.annotation.NumberFormat java.lang.Integer: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@4a6ca1a6
19:40:17,653 DEBUG GenericConversionService:570 - Did not match converter java.lang.String -> @org.springframework.format.annotation.NumberFormat java.lang.Integer: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@4a6ca1a6
19:40:17,654 DEBUG GenericConversionService:429 - and indexed by targetType [java.lang.Comparable]
19:40:17,656 DEBUG GenericConversionService:429 - and indexed by targetType [java.lang.Number]
19:40:17,658 DEBUG GenericConversionService:473 - Found matchable converters java.lang.String -> java.lang.Number : org.springframework.core.convert.support.StringToNumberConverterFactory@21e8bf76
19:40:17,660 DEBUG GenericConversionService:576 - Matched converter java.lang.String -> java.lang.Number : org.springframework.core.convert.support.StringToNumberConverterFactory@21e8bf76
19:40:17,661 DEBUG GenericConversionService:254 - Caching under ConverterCacheKey [sourceType = java.lang.String, targetType = @org.springframework.web.bind.annotation.RequestParam int]
19:40:17,663 DEBUG GenericConversionService:147 - Yes, I can convert
19:40:17,665 DEBUG GenericConversionService:159 - Converting value '1380' of java.lang.String to @org.springframework.web.bind.annotation.RequestParam int
19:40:17,667 DEBUG GenericConversionService:246 - Matched cached converter java.lang.String -> java.lang.Number : org.springframework.core.convert.support.StringToNumberConverterFactory@21e8bf76
19:40:17,668 DEBUG GenericConversionService:186 - Converted to 1380
19:40:17,672 DEBUG GenericConversionService:139 - Checking if I can convert java.lang.String to @org.springframework.web.bind.annotation.RequestParam int
19:40:17,673 DEBUG GenericConversionService:358 - Searching for converters indexed by sourceType [java.lang.String]
19:40:17,675 DEBUG GenericConversionService:429 - and indexed by targetType [java.lang.Integer]
19:40:17,677 DEBUG GenericConversionService:473 - Found matchable converters java.lang.String -> @org.springframework.format.annotation.NumberFormat java.lang.Integer: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@4a6ca1a6
19:40:17,679 DEBUG GenericConversionService:560 - Matching java.lang.String -> @org.springframework.format.annotation.NumberFormat java.lang.Integer: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@4a6ca1a6
19:40:17,680 DEBUG GenericConversionService:570 - Did not match converter java.lang.String -> @org.springframework.format.annotation.NumberFormat java.lang.Integer: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@4a6ca1a6
19:40:17,682 DEBUG GenericConversionService:429 - and indexed by targetType [java.lang.Comparable]
19:40:17,684 DEBUG GenericConversionService:429 - and indexed by targetType [java.lang.Number]
19:40:17,685 DEBUG GenericConversionService:473 - Found matchable converters java.lang.String -> java.lang.Number : org.springframework.core.convert.support.StringToNumberConverterFactory@21e8bf76
19:40:17,687 DEBUG GenericConversionService:576 - Matched converter java.lang.String -> java.lang.Number : org.springframework.core.convert.support.StringToNumberConverterFactory@21e8bf76
19:40:17,689 DEBUG GenericConversionService:254 - Caching under ConverterCacheKey [sourceType = java.lang.String, targetType = @org.springframework.web.bind.annotation.RequestParam int]
19:40:17,691 DEBUG GenericConversionService:147 - Yes, I can convert
19:40:17,693 DEBUG GenericConversionService:159 - Converting value '540' of java.lang.String to @org.springframework.web.bind.annotation.RequestParam int
19:40:17,694 DEBUG GenericConversionService:246 - Matched cached converter java.lang.String -> java.lang.Number : org.springframework.core.convert.support.StringToNumberConverterFactory@21e8bf76
19:40:17,696 DEBUG GenericConversionService:186 - Converted to 540
19:40:17,699 DEBUG GenericConversionService:139 - Checking if I can convert java.lang.String to @org.springframework.web.bind.annotation.RequestParam int
19:40:17,701 DEBUG GenericConversionService:358 - Searching for converters indexed by sourceType [java.lang.String]
19:40:17,703 DEBUG GenericConversionService:429 - and indexed by targetType [java.lang.Integer]
19:40:17,705 DEBUG GenericConversionService:473 - Found matchable converters java.lang.String -> @org.springframework.format.annotation.NumberFormat java.lang.Integer: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@4a6ca1a6
19:40:17,707 DEBUG GenericConversionService:560 - Matching java.lang.String -> @org.springframework.format.annotation.NumberFormat java.lang.Integer: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@4a6ca1a6
19:40:17,708 DEBUG GenericConversionService:570 - Did not match converter java.lang.String -> @org.springframework.format.annotation.NumberFormat java.lang.Integer: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@4a6ca1a6
19:40:17,710 DEBUG GenericConversionService:429 - and indexed by targetType [java.lang.Comparable]
19:40:17,712 DEBUG GenericConversionService:429 - and indexed by targetType [java.lang.Number]
19:40:17,713 DEBUG GenericConversionService:473 - Found matchable converters java.lang.String -> java.lang.Number : org.springframework.core.convert.support.StringToNumberConverterFactory@21e8bf76
19:40:17,715 DEBUG GenericConversionService:576 - Matched converter java.lang.String -> java.lang.Number : org.springframework.core.convert.support.StringToNumberConverterFactory@21e8bf76
19:40:17,717 DEBUG GenericConversionService:254 - Caching under ConverterCacheKey [sourceType = java.lang.String, targetType = @org.springframework.web.bind.annotation.RequestParam int]
19:40:17,719 DEBUG GenericConversionService:147 - Yes, I can convert
19:40:17,720 DEBUG GenericConversionService:159 - Converting value '1380' of java.lang.String to @org.springframework.web.bind.annotation.RequestParam int
19:40:17,722 DEBUG GenericConversionService:246 - Matched cached converter java.lang.String -> java.lang.Number : org.springframework.core.convert.support.StringToNumberConverterFactory@21e8bf76
19:40:17,724 DEBUG GenericConversionService:186 - Converted to 1380
19:40:17,727 DEBUG GenericConversionService:139 - Checking if I can convert java.lang.String to @org.springframework.web.bind.annotation.RequestParam int
19:40:17,729 DEBUG GenericConversionService:358 - Searching for converters indexed by sourceType [java.lang.String]
19:40:17,731 DEBUG GenericConversionService:429 - and indexed by targetType [java.lang.Integer]
19:40:17,732 DEBUG GenericConversionService:473 - Found matchable converters java.lang.String -> @org.springframework.format.annotation.NumberFormat java.lang.Integer: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@4a6ca1a6
19:40:17,734 DEBUG GenericConversionService:560 - Matching java.lang.String -> @org.springframework.format.annotation.NumberFormat java.lang.Integer: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@4a6ca1a6
19:40:17,736 DEBUG GenericConversionService:570 - Did not match converter java.lang.String -> @org.springframework.format.annotation.NumberFormat java.lang.Integer: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@4a6ca1a6
19:40:17,737 DEBUG GenericConversionService:429 - and indexed by targetType [java.lang.Comparable]
19:40:17,739 DEBUG GenericConversionService:429 - and indexed by targetType [java.lang.Number]
19:40:17,741 DEBUG GenericConversionService:473 - Found matchable converters java.lang.String -> java.lang.Number : org.springframework.core.convert.support.StringToNumberConverterFactory@21e8bf76
19:40:17,743 DEBUG GenericConversionService:576 - Matched converter java.lang.String -> java.lang.Number : org.springframework.core.convert.support.StringToNumberConverterFactory@21e8bf76
19:40:17,744 DEBUG GenericConversionService:254 - Caching under ConverterCacheKey [sourceType = java.lang.String, targetType = @org.springframework.web.bind.annotation.RequestParam int]
19:40:17,746 DEBUG GenericConversionService:147 - Yes, I can convert
19:40:17,748 DEBUG GenericConversionService:159 - Converting value '540' of java.lang.String to @org.springframework.web.bind.annotation.RequestParam int
19:40:17,750 DEBUG GenericConversionService:246 - Matched cached converter java.lang.String -> java.lang.Number : org.springframework.core.convert.support.StringToNumberConverterFactory@21e8bf76
19:40:17,780 DEBUG GenericConversionService:186 - Converted to 540
19:40:17,784 DEBUG GenericConversionService:139 - Checking if I can convert java.lang.String to @org.springframework.web.bind.annotation.RequestParam int
19:40:17,786 DEBUG GenericConversionService:358 - Searching for converters indexed by sourceType [java.lang.String]
19:40:17,788 DEBUG GenericConversionService:429 - and indexed by targetType [java.lang.Integer]
19:40:17,790 DEBUG GenericConversionService:473 - Found matchable converters java.lang.String -> @org.springframework.format.annotation.NumberFormat java.lang.Integer: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@4a6ca1a6
19:40:17,792 DEBUG GenericConversionService:560 - Matching java.lang.String -> @org.springframework.format.annotation.NumberFormat java.lang.Integer: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@4a6ca1a6
19:40:17,794 DEBUG GenericConversionService:570 - Did not match converter java.lang.String -> @org.springframework.format.annotation.NumberFormat java.lang.Integer: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@4a6ca1a6
19:40:17,795 DEBUG GenericConversionService:429 - and indexed by targetType [java.lang.Comparable]
19:40:17,797 DEBUG GenericConversionService:429 - and indexed by targetType [java.lang.Number]
19:40:17,799 DEBUG GenericConversionService:473 - Found matchable converters java.lang.String -> java.lang.Number : org.springframework.core.convert.support.StringToNumberConverterFactory@21e8bf76
19:40:17,800 DEBUG GenericConversionService:576 - Matched converter java.lang.String -> java.lang.Number : org.springframework.core.convert.support.StringToNumberConverterFactory@21e8bf76
19:40:17,802 DEBUG GenericConversionService:254 - Caching under ConverterCacheKey [sourceType = java.lang.String, targetType = @org.springframework.web.bind.annotation.RequestParam int]
19:40:17,804 DEBUG GenericConversionService:147 - Yes, I can convert
19:40:17,806 DEBUG GenericConversionService:159 - Converting value '1380' of java.lang.String to @org.springframework.web.bind.annotation.RequestParam int
19:40:17,807 DEBUG GenericConversionService:246 - Matched cached converter java.lang.String -> java.lang.Number : org.springframework.core.convert.support.StringToNumberConverterFactory@21e8bf76
19:40:17,809 DEBUG GenericConversionService:186 - Converted to 1380
19:40:17,812 DEBUG HandlerMethodInvoker:173 - Invoking request handler method: public com.zeater.guru.admin.command.ResultVO com.zeater.guru.admin.controller.RestaurantController.addRestaurant(java.lang.String,java.lang.String,java.lang.String,int,java.lang.String,java.lang.String,java.lang.String,java.lang.String,int,int,int,float,float,int,float,boolean,boolean,int,java.lang.String,java.lang.String,java.lang.String,int,int,int,int,int,int,int,int,int,int,int,int,int,int,org.springframework.ui.Model)
19:40:17,815 DEBUG AnnotationMethodHandlerExceptionResolver:132 - Resolving exception from handler [com.zeater.guru.admin.controller.RestaurantController@144683c2]: java.lang.IllegalArgumentException
19:40:17,818 DEBUG ResponseStatusExceptionResolver:132 - Resolving exception from handler [com.zeater.guru.admin.controller.RestaurantController@144683c2]: java.lang.IllegalArgumentException
19:40:17,820 DEBUG DefaultHandlerExceptionResolver:132 - Resolving exception from handler [com.zeater.guru.admin.controller.RestaurantController@144683c2]: java.lang.IllegalArgumentException
19:40:17,822 DEBUG DispatcherServlet:667 - Cleared thread-bound request context: org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestWrapper@188edd79
19:40:17,824 DEBUG DispatcherServlet:671 - Could not complete request
java.lang.IllegalArgumentException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)

Upvotes: 0

Views: 5708

Answers (1)

raniejade
raniejade

Reputation: 515

A complete stack trace will be much appreciated. The problem could be, spring could not convert one of your method parameter. You should refactor that method, it has too many parameters. You could create a dto that wraps all of the parameters(except for those provided by Spring).

EDIT:

Instead of using a lot of @RequestParam annotations. You could wrap all your method parameters in one wrapper object (DTO).

Example:

public class MyWrapperDTO {
    private String someStrValue;
    private String anotherStrValue;
    private int someIntValue;

    // getters/setters omitted
}

From here you could choose 2 options: using @ModelAttribute or @RequestBody

For the first one:

@Controller
public class Controller {
    @RequestMapping(value = "/mypage/view")
    public ModelAndView viewPage() {
        MyWrapperDTO dto = new MyWrapperDTO();
        // you could provide default values for your form here
        return new ModelAndView("/mypage").
            .addObject("form", dto);
    }

    @RequestMapping(value = "/mypage/save", method = RequestMethod.POST)
    @ResponseBody
    public void doSomething(@ModelAttribute("form") MyWrapperDTO dto) {
        // process your ajax call here
        // do something
    }
}

Your jsp should look like this:

<spring:url var="myAjaxCall" value="/mypage/save" />

<!-- you should use spring form tag -->
<form:form commandName="form" action="${myAjaxCall}">
   <form:input path="someStrValue"></form:input>
   <form:input path="anotherStrValue"></form:input>
   <form:input path="someIntValue"></form:input>
   <input type="submit" value="Submit"></input>
</form:form>

Second option (@RequestBody), assuming you are sending a JSON object (this involves some jquery):

@Controller
public class Controller {
    @RequestMapping(value = "/mypage/view")
    public ModelAndView viewPage() {
        MyWrapperDTO dto = new MyWrapperDTO();
        // you could provide default values for your form here
        return new ModelAndView("/mypage").
            .addObject("form", dto);
    }

    @RequestMapping(value = "/mypage/save", method = RequestMethod.POST)
    @ResponseBody
    public void doSomething(@RequestBody MyWrapperDTO dto) {
        // process your ajax call here
        // do something
    }
}

for your jsp:

<spring:url var="myAjaxCall" value="/mypage/save" />

<script type="text/javascript">
   $(document).ready(function() {
      $(document).on('click', '#submit', function() {
          // not the best way to do it
          var data = {
            'someStrValue': $('#someStrValue').val(),
            'anotherStrValue': $('#anotherStrValue').val(),
            'someIntValue': $('#someIntValue').val()
          };

          // use jQuery.ajax
          jQuery.ajax({
              'data': JSON.stringify(data),
              'type': 'POST',
              'ur': '${myAjaxCall}',
              'success': function(data) {
                   // yehey!
               }
          });
      });
    });
</script>

<!-- you should use spring form tag -->
<form:form commandName="form">
   <form:input id="someStrValue" path="someStrValue"></form:input>
   <form:input id="anotherStrValue" path="anotherStrValue"></form:input>
   <form:input id="someIntValue" path="someIntValue"></form:input>
   <button id="submit" type="button">Submit</button>
</form:form>

Spring will handle conversion/binding to your wrapper object. The second option requires you to have jackson in your classpath.

Upvotes: 1

Related Questions