IllegalArgumentException by Spring controller - spring

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)

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.

Related

springboot fails while running bootRun for gradle project

Getting below error after upgrading springboot multi module project to springboot 2 with gradle 5.x. I do not see any project specific exception in stack trace. Basically, the project structure is like below:
application:
module1
build.gradle
module2
build.gradle
When I put a break point at SpringApplication.java of spring-boot class, it's breaking on below piece of code
protected void bindToSpringApplication(ConfigurableEnvironment environment) {
try {
Binder.get(environment).bind("spring.main", Bindable.ofInstance(this));
}
catch (Exception ex) {
throw new IllegalStateException("Cannot bind to SpringApplication", ex);
}
}
And the exception is:
org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.String] to type [org.springframework.boot.SpringApplication]
ConversionService converters =
#org.springframework.format.annotation.DateTimeFormat java.lang.Long -> java.lang.String: org.springframework.format.datetime.joda.JodaDateTimeFormatAnnotationFormatterFactory#cb03411,#org.springframework.format.annotation.NumberFormat java.lang.Long -> java.lang.String: org.springframework.format.number.NumberFormatAnnotationFormatterFactory#431f1eaf
#org.springframework.format.annotation.DateTimeFormat java.time.LocalDate -> java.lang.String: org.springframework.format.datetime.standard.Jsr310DateTimeFormatAnnotationFormatterFactory#3d6a6bee,java.time.LocalDate -> java.lang.String : org.springframework.format.datetime.standard.TemporalAccessorPrinter#36ddaebf
#org.springframework.format.annotation.DateTimeFormat java.time.LocalDateTime -> java.lang.String: org.springframework.format.datetime.standard.Jsr310DateTimeFormatAnnotationFormatterFactory#3d6a6bee,java.time.LocalDateTime -> java.lang.String : org.springframework.format.datetime.standard.TemporalAccessorPrinter#3cff0139
#org.springframework.format.annotation.DateTimeFormat java.time.LocalTime -> java.lang.String: org.springframework.format.datetime.standard.Jsr310DateTimeFormatAnnotationFormatterFactory#3d6a6bee,java.time.LocalTime -> java.lang.String : org.springframework.format.datetime.standard.TemporalAccessorPrinter#9d7ccfe
#org.springframework.format.annotation.DateTimeFormat java.time.OffsetTime -> java.lang.String: org.springframework.format.datetime.standard.Jsr310DateTimeFormatAnnotationFormatterFactory#3d6a6bee,java.time.OffsetTime -> java.lang.String : org.springframework.format.datetime.standard.TemporalAccessorPrinter#15f8701f
#org.springframework.format.annotation.DateTimeFormat java.time.ZonedDateTime -> java.lang.String: org.springframework.format.datetime.standard.Jsr310DateTimeFormatAnnotationFormatterFactory#3d6a6bee,java.time.ZonedDateTime -> java.lang.String : org.springframework.format.datetime.standard.TemporalAccessorPrinter#4b2e3e8f
#org.springframework.format.annotation.DateTimeFormat java.util.Calendar -> java.lang.String: org.springframework.format.datetime.joda.JodaDateTimeFormatAnnotationFormatterFactory#cb03411
#org.springframework.format.annotation.DateTimeFormat java.util.Date -> java.lang.String: org.springframework.format.datetime.joda.JodaDateTimeFormatAnnotationFormatterFactory#cb03411
#org.springframework.format.annotation.DateTimeFormat org.joda.time.LocalDate -> java.lang.String: org.springframework.format.datetime.joda.JodaDateTimeFormatAnnotationFormatterFactory#cb03411,org.joda.time.LocalDate -> java.lang.String : org.springframework.format.datetime.joda.ReadablePartialPrinter#2cec704c
#org.springframework.format.annotation.DateTimeFormat org.joda.time.LocalDateTime -> java.lang.String: org.springframework.format.datetime.joda.JodaDateTimeFormatAnnotationFormatterFactory#cb03411,org.joda.time.LocalDateTime -> java.lang.String : org.springframework.format.datetime.joda.ReadablePartialPrinter#240f6c41
#org.springframework.format.annotation.DateTimeFormat org.joda.time.LocalTime -> java.lang.String: org.springframework.format.datetime.joda.JodaDateTimeFormatAnnotationFormatterFactory#cb03411,org.joda.time.LocalTime -> java.lang.String : org.springframework.format.datetime.joda.ReadablePartialPrinter#2416498e
#org.springframework.format.annotation.DateTimeFormat org.joda.time.ReadableInstant -> java.lang.String: org.springframework.format.datetime.joda.JodaDateTimeFormatAnnotationFormatterFactory#cb03411,org.joda.time.ReadableInstant -> java.lang.String : org.springframework.format.datetime.joda.ReadableInstantPrinter#2015b2cd
#org.springframework.format.annotation.NumberFormat java.lang.Byte -> java.lang.String: org.springframework.format.number.NumberFormatAnnotationFormatterFactory#431f1eaf
#org.springframework.format.annotation.NumberFormat java.lang.Double -> java.lang.String: org.springframework.format.number.NumberFormatAnnotationFormatterFactory#431f1eaf
#org.springframework.format.annotation.NumberFormat java.lang.Float -> java.lang.String: org.springframework.format.number.NumberFormatAnnotationFormatterFactory#431f1eaf
#org.springframework.format.annotation.NumberFormat java.lang.Integer -> java.lang.String: org.springframework.format.number.NumberFormatAnnotationFormatterFactory#431f1eaf
#org.springframework.format.annotation.NumberFormat java.lang.Short -> java.lang.String: org.springframework.format.number.NumberFormatAnnotationFormatterFactory#431f1eaf
#org.springframework.format.annotation.NumberFormat java.math.BigDecimal -> java.lang.String: org.springframework.format.number.NumberFormatAnnotationFormatterFactory#431f1eaf
#org.springframework.format.annotation.NumberFormat java.math.BigInteger -> java.lang.String: org.springframework.format.number.NumberFormatAnnotationFormatterFactory#431f1eaf
[C -> java.lang.String : org.springframework.boot.convert.CharArrayFormatter#5e746d37
java.lang.Boolean -> java.lang.String : org.springframework.core.convert.support.ObjectToStringConverter#2e23c180
java.lang.Character -> java.lang.Number : org.springframework.core.convert.support.CharacterToNumberFactory#173f73e7
java.lang.Character -> java.lang.String : org.springframework.core.convert.support.ObjectToStringConverter#46d567cb
java.lang.Enum -> java.lang.Integer : org.springframework.core.convert.support.EnumToIntegerConverter#396e6d9
java.lang.Enum -> java.lang.String : org.springframework.core.convert.support.EnumToStringConverter#4efc25fc
java.lang.Integer -> java.lang.Enum : org.springframework.core.convert.support.IntegerToEnumConverterFactory#7ee3d262
java.lang.Long -> java.time.Instant : org.springframework.format.datetime.standard.DateTimeConverters$LongToInstantConverter#732c9b5c
java.lang.Long -> java.util.Calendar : org.springframework.format.datetime.DateFormatterRegistrar$LongToCalendarConverter#2d23faef,java.lang.Long -> java.util.Calendar : org.springframework.format.datetime.DateFormatterRegistrar$LongToCalendarConverter#7cb8437d
java.lang.Long -> java.util.Date : org.springframework.format.datetime.DateFormatterRegistrar$LongToDateConverter#1aac188d,java.lang.Long -> java.util.Date : org.springframework.format.datetime.DateFormatterRegistrar$LongToDateConverter#7026b7ee
java.lang.Long -> org.joda.time.ReadableInstant : org.springframework.format.datetime.joda.JodaTimeConverters$LongToReadableInstantConverter#6b0615ae
java.lang.Number -> java.lang.Character : org.springframework.core.convert.support.NumberToCharacterConverter#363a3d15
java.lang.Number -> java.lang.Number : org.springframework.core.convert.support.NumberToNumberConverterFactory#162b3d47
java.lang.Number -> java.lang.String : org.springframework.core.convert.support.ObjectToStringConverter#3402b4c9
java.lang.String -> #org.springframework.format.annotation.DateTimeFormat java.lang.Long: org.springframework.format.datetime.joda.JodaDateTimeFormatAnnotationFormatterFactory#cb03411,java.lang.String -> #org.springframework.format.annotation.NumberFormat java.lang.Long: org.springframework.format.number.NumberFormatAnnotationFormatterFactory#431f1eaf
java.lang.String -> #org.springframework.format.annotation.DateTimeFormat java.time.LocalDate: org.springframework.format.datetime.standard.Jsr310DateTimeFormatAnnotationFormatterFactory#3d6a6bee,java.lang.String -> java.time.LocalDate: org.springframework.format.datetime.standard.TemporalAccessorParser#213c3543
java.lang.String -> #org.springframework.format.annotation.DateTimeFormat java.time.LocalDateTime: org.springframework.format.datetime.standard.Jsr310DateTimeFormatAnnotationFormatterFactory#3d6a6bee,java.lang.String -> java.time.LocalDateTime: org.springframework.format.datetime.standard.TemporalAccessorParser#7c29adc8
java.lang.String -> #org.springframework.format.annotation.DateTimeFormat java.time.LocalTime: org.springframework.format.datetime.standard.Jsr310DateTimeFormatAnnotationFormatterFactory#3d6a6bee,java.lang.String -> java.time.LocalTime: org.springframework.format.datetime.standard.TemporalAccessorParser#670ce331
java.lang.String -> #org.springframework.format.annotation.DateTimeFormat java.time.OffsetTime: org.springframework.format.datetime.standard.Jsr310DateTimeFormatAnnotationFormatterFactory#3d6a6bee,java.lang.String -> java.time.OffsetTime: org.springframework.format.datetime.standard.TemporalAccessorParser#514cd540
java.lang.String -> #org.springframework.format.annotation.DateTimeFormat java.time.ZonedDateTime: org.springframework.format.datetime.standard.Jsr310DateTimeFormatAnnotationFormatterFactory#3d6a6bee,java.lang.String -> java.time.ZonedDateTime: org.springframework.format.datetime.standard.TemporalAccessorParser#4bbb49b0
java.lang.String -> #org.springframework.format.annotation.DateTimeFormat java.util.Calendar: org.springframework.format.datetime.joda.JodaDateTimeFormatAnnotationFormatterFactory#cb03411
java.lang.String -> #org.springframework.format.annotation.DateTimeFormat java.util.Date: org.springframework.format.datetime.joda.JodaDateTimeFormatAnnotationFormatterFactory#cb03411
java.lang.String -> #org.springframework.format.annotation.DateTimeFormat org.joda.time.LocalDate: org.springframework.format.datetime.joda.JodaDateTimeFormatAnnotationFormatterFactory#cb03411,java.lang.String -> org.joda.time.LocalDate: org.springframework.format.datetime.joda.LocalDateParser#771cbb1a
java.lang.String -> #org.springframework.format.annotation.DateTimeFormat org.joda.time.LocalDateTime: org.springframework.format.datetime.joda.JodaDateTimeFormatAnnotationFormatterFactory#cb03411,java.lang.String -> org.joda.time.LocalDateTime: org.springframework.format.datetime.joda.LocalDateTimeParser#3659d7b1
java.lang.String -> #org.springframework.format.annotation.DateTimeFormat org.joda.time.LocalTime: org.springframework.format.datetime.joda.JodaDateTimeFormatAnnotationFormatterFactory#cb03411,java.lang.String -> org.joda.time.LocalTime: org.springframework.format.datetime.joda.LocalTimeParser#6b2e0f78
java.lang.String -> #org.springframework.format.annotation.DateTimeFormat org.joda.time.ReadableInstant: org.springframework.format.datetime.joda.JodaDateTimeFormatAnnotationFormatterFactory#cb03411,java.lang.String -> org.joda.time.ReadableInstant: org.springframework.format.datetime.joda.DateTimeParser#3bdb2c78
java.lang.String -> #org.springframework.format.annotation.NumberFormat java.lang.Byte: org.springframework.format.number.NumberFormatAnnotationFormatterFactory#431f1eaf
java.lang.String -> #org.springframework.format.annotation.NumberFormat java.lang.Double: org.springframework.format.number.NumberFormatAnnotationFormatterFactory#431f1eaf
java.lang.String -> #org.springframework.format.annotation.NumberFormat java.lang.Float: org.springframework.format.number.NumberFormatAnnotationFormatterFactory#431f1eaf
java.lang.String -> #org.springframework.format.annotation.NumberFormat java.lang.Integer: org.springframework.format.number.NumberFormatAnnotationFormatterFactory#431f1eaf
java.lang.String -> #org.springframework.format.annotation.NumberFormat java.lang.Short: org.springframework.format.number.NumberFormatAnnotationFormatterFactory#431f1eaf
java.lang.String -> #org.springframework.format.annotation.NumberFormat java.math.BigDecimal: org.springframework.format.number.NumberFormatAnnotationFormatterFactory#431f1eaf
java.lang.String -> #org.springframework.format.annotation.NumberFormat java.math.BigInteger: org.springframework.format.number.NumberFormatAnnotationFormatterFactory#431f1eaf
java.lang.String -> [C: org.springframework.boot.convert.CharArrayFormatter#5e746d37
java.lang.String -> java.lang.Boolean : org.springframework.core.convert.support.StringToBooleanConverter#43a51d00
java.lang.String -> java.lang.Character : org.springframework.core.convert.support.StringToCharacterConverter#95bb2a2
java.lang.String -> java.lang.Enum : org.springframework.boot.convert.StringToEnumIgnoringCaseConverterFactory#499683c4,java.lang.String -> java.lang.Enum : org.springframework.core.convert.support.StringToEnumConverterFactory#25da615a
java.lang.String -> java.lang.Number : org.springframework.core.convert.support.StringToNumberConverterFactory#783115d9
java.lang.String -> java.net.InetAddress: org.springframework.boot.convert.InetAddressFormatter#6e1b9411
java.lang.String -> java.nio.charset.Charset : org.springframework.core.convert.support.StringToCharsetConverter#3a5c2626
java.lang.String -> java.time.Instant: org.springframework.format.datetime.standard.InstantFormatter#11d4dbd6
java.lang.String -> java.time.Month: org.springframework.format.datetime.standard.MonthFormatter#186cb891
java.lang.String -> java.time.MonthDay: org.springframework.format.datetime.standard.MonthDayFormatter#440eaa07
java.lang.String -> java.time.OffsetDateTime: org.springframework.boot.convert.IsoOffsetFormatter#f096f37,java.lang.String -> #org.springframework.format.annotation.DateTimeFormat java.time.OffsetDateTime: org.springframework.format.datetime.standard.Jsr310DateTimeFormatAnnotationFormatterFactory#3d6a6bee,java.lang.String -> java.time.OffsetDateTime: org.springframework.format.datetime.standard.TemporalAccessorParser#41f4fe5
java.lang.String -> java.time.Period: org.springframework.format.datetime.standard.PeriodFormatter#6f4ade6e
java.lang.String -> java.time.Year: org.springframework.format.datetime.standard.YearFormatter#86733
java.lang.String -> java.time.YearMonth: org.springframework.format.datetime.standard.YearMonthFormatter#3ec2ecea
java.lang.String -> java.util.Currency : org.springframework.core.convert.support.StringToCurrencyConverter#7fb33394
java.lang.String -> java.util.Locale : org.springframework.core.convert.support.StringToLocaleConverter#75308740
java.lang.String -> java.util.Properties : org.springframework.core.convert.support.StringToPropertiesConverter#1a891add
java.lang.String -> java.util.TimeZone : org.springframework.core.convert.support.StringToTimeZoneConverter#6009bea
java.lang.String -> java.util.UUID : org.springframework.core.convert.support.StringToUUIDConverter#373f7450
java.lang.String -> org.joda.time.Duration: org.springframework.format.datetime.joda.DurationFormatter#1c758545
java.lang.String -> org.joda.time.MonthDay: org.springframework.format.datetime.joda.MonthDayFormatter#73a19967
java.lang.String -> org.joda.time.Period: org.springframework.format.datetime.joda.PeriodFormatter#64693226
java.lang.String -> org.joda.time.YearMonth: org.springframework.format.datetime.joda.YearMonthFormatter#117bcfdc
java.net.InetAddress -> java.lang.String : org.springframework.boot.convert.InetAddressFormatter#6e1b9411
java.nio.charset.Charset -> java.lang.String : org.springframework.core.convert.support.ObjectToStringConverter#e48bf9a
java.time.Instant -> java.lang.Long : org.springframework.format.datetime.standard.DateTimeConverters$InstantToLongConverter#38320e34
java.time.Instant -> java.lang.String : org.springframework.format.datetime.standard.InstantFormatter#11d4dbd6
java.time.LocalDateTime -> java.time.LocalDate : org.springframework.format.datetime.standard.DateTimeConverters$LocalDateTimeToLocalDateConverter#62a4417
java.time.LocalDateTime -> java.time.LocalTime : org.springframework.format.datetime.standard.DateTimeConverters$LocalDateTimeToLocalTimeConverter#43f61afb
java.time.Month -> java.lang.String : org.springframework.format.datetime.standard.MonthFormatter#186cb891
java.time.MonthDay -> java.lang.String : org.springframework.format.datetime.standard.MonthDayFormatter#440eaa07
java.time.OffsetDateTime -> java.lang.String : org.springframework.boot.convert.IsoOffsetFormatter#f096f37,#org.springframework.format.annotation.DateTimeFormat java.time.OffsetDateTime -> java.lang.String: org.springframework.format.datetime.standard.Jsr310DateTimeFormatAnnotationFormatterFactory#3d6a6bee,java.time.OffsetDateTime -> java.lang.String : org.springframework.format.datetime.standard.TemporalAccessorPrinter#3effd4f3
java.time.OffsetDateTime -> java.time.Instant : org.springframework.format.datetime.standard.DateTimeConverters$OffsetDateTimeToInstantConverter#367b22e5
java.time.OffsetDateTime -> java.time.LocalDate : org.springframework.format.datetime.standard.DateTimeConverters$OffsetDateTimeToLocalDateConverter#3e5d4f6b
java.time.OffsetDateTime -> java.time.LocalDateTime : org.springframework.format.datetime.standard.DateTimeConverters$OffsetDateTimeToLocalDateTimeConverter#5ae95707
java.time.OffsetDateTime -> java.time.LocalTime : org.springframework.format.datetime.standard.DateTimeConverters$OffsetDateTimeToLocalTimeConverter#39c1fe0b
java.time.OffsetDateTime -> java.time.ZonedDateTime : org.springframework.format.datetime.standard.DateTimeConverters$OffsetDateTimeToZonedDateTimeConverter#62891fc8
java.time.Period -> java.lang.String : org.springframework.format.datetime.standard.PeriodFormatter#6f4ade6e
java.time.Year -> java.lang.String : org.springframework.format.datetime.standard.YearFormatter#86733
java.time.YearMonth -> java.lang.String : org.springframework.format.datetime.standard.YearMonthFormatter#3ec2ecea
java.time.ZoneId -> java.util.TimeZone : org.springframework.core.convert.support.ZoneIdToTimeZoneConverter#7bc6d27a
java.time.ZonedDateTime -> java.time.Instant : org.springframework.format.datetime.standard.DateTimeConverters$ZonedDateTimeToInstantConverter#1ac45389
java.time.ZonedDateTime -> java.time.LocalDate : org.springframework.format.datetime.standard.DateTimeConverters$ZonedDateTimeToLocalDateConverter#713064e8
java.time.ZonedDateTime -> java.time.LocalDateTime : org.springframework.format.datetime.standard.DateTimeConverters$ZonedDateTimeToLocalDateTimeConverter#1bf39d06
java.time.ZonedDateTime -> java.time.LocalTime : org.springframework.format.datetime.standard.DateTimeConverters$ZonedDateTimeToLocalTimeConverter#4fad6218
java.time.ZonedDateTime -> java.time.OffsetDateTime : org.springframework.format.datetime.standard.DateTimeConverters$ZonedDateTimeToOffsetDateTimeConverter#68217d41
java.time.ZonedDateTime -> java.util.Calendar : org.springframework.core.convert.support.ZonedDateTimeToCalendarConverter#75769ab0
java.util.Calendar -> java.lang.Long : org.springframework.format.datetime.DateFormatterRegistrar$CalendarToLongConverter#5a00eb1e,java.util.Calendar -> java.lang.Long : org.springframework.format.datetime.DateFormatterRegistrar$CalendarToLongConverter#36fcf6c0
java.util.Calendar -> java.time.Instant : org.springframework.format.datetime.standard.DateTimeConverters$CalendarToInstantConverter#4c48fe92
java.util.Calendar -> java.time.LocalDate : org.springframework.format.datetime.standard.DateTimeConverters$CalendarToLocalDateConverter#3ae0b770
java.util.Calendar -> java.time.LocalDateTime : org.springframework.format.datetime.standard.DateTimeConverters$CalendarToLocalDateTimeConverter#30e6a763
java.util.Calendar -> java.time.LocalTime : org.springframework.format.datetime.standard.DateTimeConverters$CalendarToLocalTimeConverter#55e3d6c3
java.util.Calendar -> java.time.OffsetDateTime : org.springframework.format.datetime.standard.DateTimeConverters$CalendarToOffsetDateTimeConverter#10bd9df0
java.util.Calendar -> java.time.ZonedDateTime : org.springframework.format.datetime.standard.DateTimeConverters$CalendarToZonedDateTimeConverter#fca387
java.util.Calendar -> java.util.Date : org.springframework.format.datetime.DateFormatterRegistrar$CalendarToDateConverter#6dd82486,java.util.Calendar -> java.util.Date : org.springframework.format.datetime.DateFormatterRegistrar$CalendarToDateConverter#56078cea
java.util.Calendar -> org.joda.time.ReadableInstant : org.springframework.format.datetime.joda.JodaTimeConverters$CalendarToReadableInstantConverter#49c8f6e8
java.util.Currency -> java.lang.String : org.springframework.core.convert.support.ObjectToStringConverter#41bf79da
java.util.Date -> java.lang.Long : org.springframework.format.datetime.DateFormatterRegistrar$DateToLongConverter#4c59e45e,java.util.Date -> java.lang.Long : org.springframework.format.datetime.DateFormatterRegistrar$DateToLongConverter#58ec7116
java.util.Date -> java.util.Calendar : org.springframework.format.datetime.DateFormatterRegistrar$DateToCalendarConverter#63bde6c2,java.util.Date -> java.util.Calendar : org.springframework.format.datetime.DateFormatterRegistrar$DateToCalendarConverter#6ea04618
java.util.Date -> org.joda.time.ReadableInstant : org.springframework.format.datetime.joda.JodaTimeConverters$DateToReadableInstantConverter#2f98635e
java.util.Locale -> java.lang.String : org.springframework.core.convert.support.ObjectToStringConverter#5acc9fdf
java.util.Properties -> java.lang.String : org.springframework.core.convert.support.PropertiesToStringConverter#5176d279
java.util.UUID -> java.lang.String : org.springframework.core.convert.support.ObjectToStringConverter#d74bac4
org.joda.time.DateTime -> java.lang.Long : org.springframework.format.datetime.joda.JodaTimeConverters$DateTimeToLongConverter#7afbf561
org.joda.time.DateTime -> java.util.Calendar : org.springframework.format.datetime.joda.JodaTimeConverters$DateTimeToCalendarConverter#7c9bdee9
org.joda.time.DateTime -> java.util.Date : org.springframework.format.datetime.joda.JodaTimeConverters$DateTimeToDateConverter#4f453e63
org.joda.time.DateTime -> org.joda.time.DateMidnight : org.springframework.format.datetime.joda.JodaTimeConverters$DateTimeToDateMidnightConverter#624a24f6
org.joda.time.DateTime -> org.joda.time.Instant : org.springframework.format.datetime.joda.JodaTimeConverters$DateTimeToInstantConverter#338cc75f
org.joda.time.DateTime -> org.joda.time.LocalDate : org.springframework.format.datetime.joda.JodaTimeConverters$DateTimeToLocalDateConverter#7fc7c4a
org.joda.time.DateTime -> org.joda.time.LocalDateTime : org.springframework.format.datetime.joda.JodaTimeConverters$DateTimeToLocalDateTimeConverter#53a5e217
org.joda.time.DateTime -> org.joda.time.LocalTime : org.springframework.format.datetime.joda.JodaTimeConverters$DateTimeToLocalTimeConverter#7aa9e414
org.joda.time.DateTime -> org.joda.time.MutableDateTime : org.springframework.format.datetime.joda.JodaTimeConverters$DateTimeToMutableDateTimeConverter#6b00ad9
org.joda.time.Duration -> java.lang.String : org.springframework.format.datetime.joda.DurationFormatter#1c758545
org.joda.time.LocalDateTime -> org.joda.time.LocalDate : org.springframework.format.datetime.joda.JodaTimeConverters$LocalDateTimeToLocalDateConverter#4e73b552
org.joda.time.LocalDateTime -> org.joda.time.LocalTime : org.springframework.format.datetime.joda.JodaTimeConverters$LocalDateTimeToLocalTimeConverter#221dad51
org.joda.time.MonthDay -> java.lang.String : org.springframework.format.datetime.joda.MonthDayFormatter#73a19967
org.joda.time.Period -> java.lang.String : org.springframework.format.datetime.joda.PeriodFormatter#64693226
org.joda.time.YearMonth -> java.lang.String : org.springframework.format.datetime.joda.YearMonthFormatter#117bcfdc
org.springframework.boot.convert.ArrayToDelimitedStringConverter#2c768ada,org.springframework.core.convert.support.ArrayToStringConverter#c1fca2a
org.springframework.boot.convert.CollectionToDelimitedStringConverter#5d5b5fa7,org.springframework.core.convert.support.CollectionToStringConverter#2a32fb6
org.springframework.boot.convert.DelimitedStringToArrayConverter#5533dc72,org.springframework.core.convert.support.StringToArrayConverter#7c447c76
org.springframework.boot.convert.DelimitedStringToCollectionConverter#6107165,org.springframework.core.convert.support.StringToCollectionConverter#164a62bf
org.springframework.boot.convert.DurationToNumberConverter#5ec46cdd
org.springframework.boot.convert.DurationToStringConverter#39e43310,java.time.Duration -> java.lang.String : org.springframework.format.datetime.standard.DurationFormatter#eb507b9
org.springframework.boot.convert.NumberToDataSizeConverter#112d1c8e
org.springframework.boot.convert.NumberToDurationConverter#21d1b321
org.springframework.boot.convert.StringToDataSizeConverter#2324bfe7
org.springframework.boot.convert.StringToDurationConverter#390877d2,java.lang.String -> java.time.Duration: org.springframework.format.datetime.standard.DurationFormatter#eb507b9
org.springframework.core.convert.support.ArrayToArrayConverter#74aa9c72
org.springframework.core.convert.support.ArrayToCollectionConverter#5ff90645
org.springframework.core.convert.support.ArrayToObjectConverter#64fc097e
org.springframework.core.convert.support.ByteBufferConverter#4aaae508
org.springframework.core.convert.support.ByteBufferConverter#4aaae508
org.springframework.core.convert.support.ByteBufferConverter#4aaae508
org.springframework.core.convert.support.ByteBufferConverter#4aaae508
org.springframework.core.convert.support.CollectionToArrayConverter#387bf2d9
org.springframework.core.convert.support.CollectionToCollectionConverter#5c20aab9
org.springframework.core.convert.support.CollectionToObjectConverter#11ebb1b6
org.springframework.core.convert.support.FallbackObjectToStringConverter#27ace0b1
org.springframework.core.convert.support.IdToEntityConverter#6869a3b3,org.springframework.core.convert.support.ObjectToObjectConverter#6ab4ba9f
org.springframework.core.convert.support.MapToMapConverter#4b7c4456
org.springframework.core.convert.support.ObjectToArrayConverter#1640c151
org.springframework.core.convert.support.ObjectToCollectionConverter#aaee2a2
org.springframework.core.convert.support.ObjectToOptionalConverter#664e5dee
org.springframework.core.convert.support.ObjectToOptionalConverter#664e5dee
org.springframework.core.convert.support.ObjectToOptionalConverter#664e5dee
org.springframework.core.convert.support.StreamConverter#f3021cb
org.springframework.core.convert.support.StreamConverter#f3021cb
org.springframework.core.convert.support.StreamConverter#f3021cb
org.springframework.core.convert.support.StreamConverter#f3021cb

#Autowired fails for JdbcTemplate when DAO has #Repository

It may seem an obvious mistake, but I am not able to find it. Following is my code and configuration:
Application Class
#SpringBootApplication
public class Application {
public static void main(final String[] args) {
SpringApplication.run(Application.class, args);
}
}
SQL Config Class
#Configuration
public class PostgreSQLConfig {
#Bean
public DataSource getDBDataSource() {
final DriverManagerDataSource driverManagerDataSource
= new DriverManagerDataSource(
"jdbc:postgresql://localhost:5432/applicationdb",
"username",
"password"
);
driverManagerDataSource.setDriverClassName("org.postgresql.Driver");
return driverManagerDataSource;
}
#Bean
public JdbcTemplate getJdbcTemplate(final DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
}
#Repository DAO CLASS
#Repository
public class CustomDAO {
#Autowired
private JdbcTemplate jdbcTemplate;
.
.
#Component DAO Class
#Component
public class AnotherDAO {
#Autowired
private JdbcTemplate jdbcTemplate;
.
.
The JdbcTemplate object gets populated for #Component annotated class. But the object is null for #Repository annotated class. I started spring boot with logging set to DEBUG, and couldn't find any error while creating JdbcTemplate bean. From logs, I can infer the method is called and the bean is created. The JdbcTemplate also has reference to DataSource.
If the #Repository annotation is changed to #Component the code works fine.
When using spring jdbc, can we annotate a DAO class with
#Repository?
Why the dependency injection works with #Component but fails for
#Repository?
As per the suggestion from M. Deinum, I have made following changes.
Removed PostgreSQLConfig class
Added application.properties file
application.properties
spring.datasource.url=jdbc:postgresql://localhost:5432/applicationdb
spring.datasource.user=username
spring.datasource.password=password
spring.datasource.driver-class-name=org.postgresql.Driver
Properties are loaded by spring application. Following are log messages:
2017-01-06 15:03:02 [main] DEBUG o.s.c.e.PropertySourcesPropertyResolver - Found key 'spring.datasource.driver-class-name' in [applicationConfigurationProperties] with type [String]
2017-01-06 15:03:02 [main] DEBUG o.s.c.e.PropertySourcesPropertyResolver - Found key 'spring.datasource.password' in [applicationConfigurationProperties] with type [String]
2017-01-06 15:03:02 [main] DEBUG o.s.c.e.PropertySourcesPropertyResolver - Found key 'spring.datasource.user' in [applicationConfigurationProperties] with type [String]
2017-01-06 15:03:02 [main] DEBUG o.s.c.e.PropertySourcesPropertyResolver - Found key 'spring.datasource.url' in [applicationConfigurationProperties] with type [String]
I receive an exception. Following are the details:
2017-01-06 15:03:05 [main] DEBUG o.s.b.d.LoggingFailureAnalysisReporter - Application failed to start due to an exception
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.jdbc.core.JdbcTemplate' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1474)
DAO classes are marked with #Repository annotation.
build.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.springframework.boot:spring-boot-gradle-plugin:1.4.3.RELEASE'
}
}
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'idea'
apply plugin: 'application'
apply plugin: 'org.springframework.boot'
def applicationVersion = '0.0.1-SNAPSHOT'
def dependencyVersions = [
slf4j: '1.7.22',
logback: '1.1.8',
postgresql: '9.4.1212',
junit: '4.12'
]
group = 'com.appl'
version = applicationVersion
mainClassName = 'com.appl.Application'
war {
baseName = 'my-custom-appl'
version = applicationVersion
}
war.dependsOn test
repositories {
mavenCentral()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
configurations.all {
exclude group: 'commons-logging', module: 'commons-logging'
}
dependencies {
compile 'org.springframework.boot:spring-boot-starter-web'
compile 'org.springframework.boot:spring-boot-starter-jdbc'
compile 'org.springframework:spring-jdbc'
compile "org.slf4j:jcl-over-slf4j:${dependencyVersions.slf4j}"
compile "org.postgresql:postgresql:${dependencyVersions.postgresql}"
compile "ch.qos.logback:logback-classic:${dependencyVersions.logback}"
testCompile "junit:junit:${dependencyVersions.junit}"
testCompile 'org.springframework.boot:spring-boot-starter-test'
}
I found the (what is happening part of) solution to the problem. Methods in DAO class were marked final, and #Repository doesn't works well with final method. From logs, I can infer that #Repository tries to create a proxy for DAO class, which fails.
When #Repository is replaced with #Component the code runs fine. Again from logs, I can see that #Component doesn't create a proxy and the final status of the method is ignored.
Following are the debug logs of Spring:
When method is final and DAO Class is marked as #Repository
2017-01-08 09:19:03 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Autowiring by type from bean name 'customDAO' via constructor to bean named 'getJdbcTemplate'
2017-01-08 09:19:03 [main] DEBUG c.j.l.d.CustomDAO - Received JdbcTemplate: org.springframework.jdbc.core.JdbcTemplate#3dedb4a6
2017-01-08 09:19:03 [main] INFO c.j.l.d.CustomDAO - Constructor Called
2017-01-08 09:19:03 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Eagerly caching bean 'customDAO' to allow for resolving potential circular references
2017-01-08 09:19:03 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
2017-01-08 09:19:03 [main] DEBUG o.s.aop.framework.CglibAopProxy - Creating CGLIB proxy: target source is SingletonTargetSource for target object [com.j.l.data.CustomDAO#19542407]
2017-01-08 09:19:03 [main] INFO o.s.aop.framework.CglibAopProxy - Unable to proxy method [public final int com.j.l.data.CustomDAO.persistEvent(java.util.List)] because it is final: All calls to this method via a proxy will NOT be routed to the target instance.
2017-01-08 09:19:03 [main] DEBUG o.s.aop.framework.CglibAopProxy - Found 'equals' method: public boolean java.lang.Object.equals(java.lang.Object)
2017-01-08 09:19:03 [main] DEBUG o.s.aop.framework.CglibAopProxy - Unable to apply any optimisations to advised method: public java.lang.String java.lang.Object.toString()
2017-01-08 09:19:03 [main] DEBUG o.s.aop.framework.CglibAopProxy - Found 'hashCode' method: public native int java.lang.Object.hashCode()
2017-01-08 09:19:03 [main] DEBUG o.s.aop.framework.CglibAopProxy - Unable to apply any optimisations to advised method: protected native java.lang.Object java.lang.Object.clone() throws java.lang.CloneNotSupportedException
2017-01-08 09:19:03 [main] DEBUG o.s.aop.framework.CglibAopProxy - Method is declared on Advised interface: public abstract int org.springframework.aop.framework.Advised.indexOf(org.springframework.aop.Advisor)
2017-01-08 09:19:03 [main] DEBUG o.s.aop.framework.CglibAopProxy - Method is declared on Advised interface: public abstract int org.springframework.aop.framework.Advised.indexOf(org.aopalliance.aop.Advice)
2017-01-08 09:19:03 [main] DEBUG o.s.aop.framework.CglibAopProxy - Method is declared on Advised interface: public abstract boolean org.springframework.aop.framework.Advised.isFrozen()
2017-01-08 09:19:03 [main] DEBUG o.s.aop.framework.CglibAopProxy - Method is declared on Advised interface: public abstract void org.springframework.aop.framework.Advised.setTargetSource(org.springframework.aop.TargetSource)
2017-01-08 09:19:03 [main] DEBUG o.s.aop.framework.CglibAopProxy - Method is declared on Advised interface: public abstract void org.springframework.aop.framework.Advised.addAdvisor(org.springframework.aop.Advisor) throws org.springframework.aop.framework.AopConfigException
2017-01-08 09:19:03 [main] DEBUG o.s.aop.framework.CglibAopProxy - Method is declared on Advised interface: public abstract void org.springframework.aop.framework.Advised.addAdvisor(int,org.springframework.aop.Advisor) throws org.springframework.aop.framework.AopConfigException
2017-01-08 09:19:03 [main] DEBUG o.s.aop.framework.CglibAopProxy - Method is declared on Advised interface: public abstract void org.springframework.aop.framework.Advised.setExposeProxy(boolean)
2017-01-08 09:19:03 [main] DEBUG o.s.aop.framework.CglibAopProxy - Method is declared on Advised interface: public abstract boolean org.springframework.aop.framework.Advised.isExposeProxy()
2017-01-08 09:19:03 [main] DEBUG o.s.aop.framework.CglibAopProxy - Method is declared on Advised interface: public abstract boolean org.springframework.aop.framework.Advised.isProxyTargetClass()
2017-01-08 09:19:03 [main] DEBUG o.s.aop.framework.CglibAopProxy - Method is declared on Advised interface: public abstract org.springframework.aop.TargetSource org.springframework.aop.framework.Advised.getTargetSource()
2017-01-08 09:19:03 [main] DEBUG o.s.aop.framework.CglibAopProxy - Method is declared on Advised interface: public abstract void org.springframework.aop.framework.Advised.setPreFiltered(boolean)
2017-01-08 09:19:03 [main] DEBUG o.s.aop.framework.CglibAopProxy - Method is declared on Advised interface: public abstract void org.springframework.aop.framework.Advised.addAdvice(int,org.aopalliance.aop.Advice) throws org.springframework.aop.framework.AopConfigException
2017-01-08 09:19:03 [main] DEBUG o.s.aop.framework.CglibAopProxy - Method is declared on Advised interface: public abstract void org.springframework.aop.framework.Advised.addAdvice(org.aopalliance.aop.Advice) throws org.springframework.aop.framework.AopConfigException
2017-01-08 09:19:03 [main] DEBUG o.s.aop.framework.CglibAopProxy - Method is declared on Advised interface: public abstract boolean org.springframework.aop.framework.Advised.isPreFiltered()
2017-01-08 09:19:03 [main] DEBUG o.s.aop.framework.CglibAopProxy - Method is declared on Advised interface: public abstract java.lang.Class[] org.springframework.aop.framework.Advised.getProxiedInterfaces()
2017-01-08 09:19:03 [main] DEBUG o.s.aop.framework.CglibAopProxy - Method is declared on Advised interface: public abstract boolean org.springframework.aop.framework.Advised.isInterfaceProxied(java.lang.Class)
2017-01-08 09:19:03 [main] DEBUG o.s.aop.framework.CglibAopProxy - Method is declared on Advised interface: public abstract org.springframework.aop.Advisor[] org.springframework.aop.framework.Advised.getAdvisors()
2017-01-08 09:19:03 [main] DEBUG o.s.aop.framework.CglibAopProxy - Method is declared on Advised interface: public abstract void org.springframework.aop.framework.Advised.removeAdvisor(int) throws org.springframework.aop.framework.AopConfigException
2017-01-08 09:19:03 [main] DEBUG o.s.aop.framework.CglibAopProxy - Method is declared on Advised interface: public abstract boolean org.springframework.aop.framework.Advised.removeAdvisor(org.springframework.aop.Advisor)
2017-01-08 09:19:03 [main] DEBUG o.s.aop.framework.CglibAopProxy - Method is declared on Advised interface: public abstract boolean org.springframework.aop.framework.Advised.replaceAdvisor(org.springframework.aop.Advisor,org.springframework.aop.Advisor) throws org.springframework.aop.framework.AopConfigException
2017-01-08 09:19:03 [main] DEBUG o.s.aop.framework.CglibAopProxy - Method is declared on Advised interface: public abstract boolean org.springframework.aop.framework.Advised.removeAdvice(org.aopalliance.aop.Advice)
2017-01-08 09:19:03 [main] DEBUG o.s.aop.framework.CglibAopProxy - Method is declared on Advised interface: public abstract java.lang.String org.springframework.aop.framework.Advised.toProxyConfigString()
2017-01-08 09:19:03 [main] DEBUG o.s.aop.framework.CglibAopProxy - Method is declared on Advised interface: public abstract java.lang.Class org.springframework.aop.TargetClassAware.getTargetClass()
2017-01-08 09:19:03 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'customDAO'
When method is not final and DAO class is marked as #Repository
2017-01-08 09:19:03 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'getJdbcTemplate'
2017-01-08 09:19:03 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Autowiring by type from bean name 'anotherDAO' via constructor to bean named 'getJdbcTemplate'
2017-01-08 09:19:03 [main] DEBUG c.j.l.data.AnotherDAO - Received JdbcTemplate: org.springframework.jdbc.core.JdbcTemplate#3dedb4a6
2017-01-08 09:19:03 [main] INFO c.j.l.data.AnotherDAO - Constructor Called
2017-01-08 09:19:03 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Eagerly caching bean 'anotherDAO' to allow for resolving potential circular references
2017-01-08 09:19:03 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
2017-01-08 09:19:03 [main] DEBUG o.s.aop.framework.CglibAopProxy - Creating CGLIB proxy: target source is SingletonTargetSource for target object [com.j.l.data.AnotherDAO#907f2b7]
2017-01-08 09:19:03 [main] DEBUG o.s.aop.framework.CglibAopProxy - Unable to apply any optimisations to advised method: public void com.j.l.data.AnotherDAO.persistEvent(com.j.l.model.EmergencyUserResponse)
2017-01-08 09:19:03 [main] DEBUG o.s.aop.framework.CglibAopProxy - Found 'equals' method: public boolean java.lang.Object.equals(java.lang.Object)
2017-01-08 09:19:03 [main] DEBUG o.s.aop.framework.CglibAopProxy - Unable to apply any optimisations to advised method: public java.lang.String java.lang.Object.toString()
2017-01-08 09:19:03 [main] DEBUG o.s.aop.framework.CglibAopProxy - Found 'hashCode' method: public native int java.lang.Object.hashCode()
2017-01-08 09:19:03 [main] DEBUG o.s.aop.framework.CglibAopProxy - Unable to apply any optimisations to advised method: protected native java.lang.Object java.lang.Object.clone() throws java.lang.CloneNotSupportedException
2017-01-08 09:19:03 [main] DEBUG o.s.aop.framework.CglibAopProxy - Method is declared on Advised interface: public abstract int org.springframework.aop.framework.Advised.indexOf(org.springframework.aop.Advisor)
2017-01-08 09:19:03 [main] DEBUG o.s.aop.framework.CglibAopProxy - Method is declared on Advised interface: public abstract int org.springframework.aop.framework.Advised.indexOf(org.aopalliance.aop.Advice)
2017-01-08 09:19:03 [main] DEBUG o.s.aop.framework.CglibAopProxy - Method is declared on Advised interface: public abstract boolean org.springframework.aop.framework.Advised.isFrozen()
2017-01-08 09:19:03 [main] DEBUG o.s.aop.framework.CglibAopProxy - Method is declared on Advised interface: public abstract void org.springframework.aop.framework.Advised.setTargetSource(org.springframework.aop.TargetSource)
2017-01-08 09:19:03 [main] DEBUG o.s.aop.framework.CglibAopProxy - Method is declared on Advised interface: public abstract void org.springframework.aop.framework.Advised.addAdvisor(org.springframework.aop.Advisor) throws org.springframework.aop.framework.AopConfigException
2017-01-08 09:19:03 [main] DEBUG o.s.aop.framework.CglibAopProxy - Method is declared on Advised interface: public abstract void org.springframework.aop.framework.Advised.addAdvisor(int,org.springframework.aop.Advisor) throws org.springframework.aop.framework.AopConfigException
2017-01-08 09:19:03 [main] DEBUG o.s.aop.framework.CglibAopProxy - Method is declared on Advised interface: public abstract void org.springframework.aop.framework.Advised.setExposeProxy(boolean)
2017-01-08 09:19:03 [main] DEBUG o.s.aop.framework.CglibAopProxy - Method is declared on Advised interface: public abstract boolean org.springframework.aop.framework.Advised.isExposeProxy()
2017-01-08 09:19:03 [main] DEBUG o.s.aop.framework.CglibAopProxy - Method is declared on Advised interface: public abstract boolean org.springframework.aop.framework.Advised.isProxyTargetClass()
2017-01-08 09:19:03 [main] DEBUG o.s.aop.framework.CglibAopProxy - Method is declared on Advised interface: public abstract org.springframework.aop.TargetSource org.springframework.aop.framework.Advised.getTargetSource()
2017-01-08 09:19:03 [main] DEBUG o.s.aop.framework.CglibAopProxy - Method is declared on Advised interface: public abstract void org.springframework.aop.framework.Advised.setPreFiltered(boolean)
2017-01-08 09:19:03 [main] DEBUG o.s.aop.framework.CglibAopProxy - Method is declared on Advised interface: public abstract void org.springframework.aop.framework.Advised.addAdvice(int,org.aopalliance.aop.Advice) throws org.springframework.aop.framework.AopConfigException
2017-01-08 09:19:03 [main] DEBUG o.s.aop.framework.CglibAopProxy - Method is declared on Advised interface: public abstract void org.springframework.aop.framework.Advised.addAdvice(org.aopalliance.aop.Advice) throws org.springframework.aop.framework.AopConfigException
2017-01-08 09:19:03 [main] DEBUG o.s.aop.framework.CglibAopProxy - Method is declared on Advised interface: public abstract boolean org.springframework.aop.framework.Advised.isPreFiltered()
2017-01-08 09:19:03 [main] DEBUG o.s.aop.framework.CglibAopProxy - Method is declared on Advised interface: public abstract java.lang.Class[] org.springframework.aop.framework.Advised.getProxiedInterfaces()
2017-01-08 09:19:03 [main] DEBUG o.s.aop.framework.CglibAopProxy - Method is declared on Advised interface: public abstract boolean org.springframework.aop.framework.Advised.isInterfaceProxied(java.lang.Class)
2017-01-08 09:19:03 [main] DEBUG o.s.aop.framework.CglibAopProxy - Method is declared on Advised interface: public abstract org.springframework.aop.Advisor[] org.springframework.aop.framework.Advised.getAdvisors()
2017-01-08 09:19:03 [main] DEBUG o.s.aop.framework.CglibAopProxy - Method is declared on Advised interface: public abstract void org.springframework.aop.framework.Advised.removeAdvisor(int) throws org.springframework.aop.framework.AopConfigException
2017-01-08 09:19:03 [main] DEBUG o.s.aop.framework.CglibAopProxy - Method is declared on Advised interface: public abstract boolean org.springframework.aop.framework.Advised.removeAdvisor(org.springframework.aop.Advisor)
2017-01-08 09:19:03 [main] DEBUG o.s.aop.framework.CglibAopProxy - Method is declared on Advised interface: public abstract boolean org.springframework.aop.framework.Advised.replaceAdvisor(org.springframework.aop.Advisor,org.springframework.aop.Advisor) throws org.springframework.aop.framework.AopConfigException
2017-01-08 09:19:03 [main] DEBUG o.s.aop.framework.CglibAopProxy - Method is declared on Advised interface: public abstract boolean org.springframework.aop.framework.Advised.removeAdvice(org.aopalliance.aop.Advice)
2017-01-08 09:19:03 [main] DEBUG o.s.aop.framework.CglibAopProxy - Method is declared on Advised interface: public abstract java.lang.String org.springframework.aop.framework.Advised.toProxyConfigString()
2017-01-08 09:19:03 [main] DEBUG o.s.aop.framework.CglibAopProxy - Method is declared on Advised interface: public abstract java.lang.Class org.springframework.aop.TargetClassAware.getTargetClass()
2017-01-08 09:19:03 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'anotherDAO'
When method is final / not final and DAO class is marked as #Component
2017-01-08 09:13:21 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Autowiring by type from bean name 'customDAO' via constructor to bean named 'getJdbcTemplate'
2017-01-08 09:13:21 [main] DEBUG c.j.l.d.CustomDAO - Received JdbcTemplate: org.springframework.jdbc.core.JdbcTemplate#67fe380b
2017-01-08 09:13:21 [main] INFO c.j.l.d.CustomDAO - Constructor Called
2017-01-08 09:13:21 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Eagerly caching bean 'customDAO' to allow for resolving potential circular references
2017-01-08 09:13:21 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
2017-01-08 09:13:21 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'customDAO'
I am still unable to understand why #Repository creates a proxy and #Component skips it? Is there a way to mark method final and use #Repository on DAO class?
If required I can create a separate question for this.

java8 spring 4.2.5 can't convert to java.util.Optional

I have a problem with spring 4.2.5 and java8.
Trying to write simple rest:
#RequestMapping(value = "raw2", method = RequestMethod.GET)
ResponseEntity<String> getTest(#RequestParam Optional<String> p) {
return new ResponseEntity<>(HttpStatus.OK);
}
But keep getting errors:
2016-04-05 20:51:41,436 DEBUG [o.s.web.servlet.DispatcherServlet] DispatcherServlet with name 'dispatcher' processing GET request for [/internal/raw2]
2016-04-05 20:51:41,438 DEBUG [o.s.w.s.m.a.DefaultAnnotationHandlerMapping] Mapping [/internal/raw2] to HandlerExecutionChain with handler [com.test.con] and 1 interceptor
2016-04-05 20:51:41,440 DEBUG [o.s.web.servlet.DispatcherServlet] Last-Modified value for [/internal/raw2] is: -1
2016-04-05 20:51:41,453 DEBUG [org.springframework.beans.BeanUtils] No property editor [java.util.OptionalEditor] found for type java.util.Optional according to 'Editor' suffix convention
2016-04-05 20:51:41,453 DEBUG [o.s.w.s.m.a.AnnotationMethodHandlerExceptionResolver] Resolving exception from handler [com.test.con]: org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type [java.lang.String] to required type [java.util.Optional]; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [java.util.Optional]: no matching editors or conversion strategy found
2016-04-05 20:51:41,454 DEBUG [o.s.w.s.m.a.ResponseStatusExceptionResolver] Resolving exception from handler [com.test.con]: org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type [java.lang.String] to required type [java.util.Optional]; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [java.util.Optional]: no matching editors or conversion strategy found
2016-04-05 20:51:41,454 DEBUG [o.s.w.s.m.s.DefaultHandlerExceptionResolver] Resolving exception from handler [com.test.con]: org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type [java.lang.String] to required type [java.util.Optional]; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [java.util.Optional]: no matching editors or conversion strategy found
2016-04-05 20:51:41,454 WARN [o.s.w.s.m.s.DefaultHandlerExceptionResolver] Failed to convert request element: org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type [java.lang.String] to required type [java.util.Optional]; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [java.util.Optional]: no matching editors or conversion strategy found
Ensure you have added the <mvc:annotation-driven> tag in XML. This will make sure that convertors are available

HttpRequestMethodNotSupportedException: Request method 'POST' not supported

Creating a unit test with MockMvc I am running into:
HttpRequestMethodNotSupportedException: Request method 'POST' not supported
Which causes the test case to fail expecting a '200' but getting a '405'. Some of the additional things you may see in the Junit are for Spring Rest Docs and can be ignored. Such as the #Rule or the .andDo() on the mockMvc Call. I have followed the following documentation Spring Rest Docs - Path Parameter and cannot seem to get it working.
Here is the Controller:
#RestController("/transferObjects")
public class TransferObjectController {
#RequestMapping(method=RequestMethod.GET, produces="application/json")
public List<TransferObject> getTransferObjects(){
// do some magic
return null;
}
#RequestMapping(value = "/{name}", method=RequestMethod.POST)
#ResponseStatus(HttpStatus.OK)
public void addTransferObject(#PathVariable("name")String name){
// do magic
}
#RequestMapping(value = "/{name}", method=RequestMethod.DELETE)
#ResponseStatus(HttpStatus.OK)
public void deleteTransferObject(#PathVariable("name")String name){
// do magic
}
Here is the Junit Class:
public class TransferObjectControllerTest {
#Rule
public RestDocumentation restDocumentation = new RestDocumentation("target/generated-snippets");
private MockMvc mockMvc;
#Before
public void setUp() throws Exception {
this.mockMvc = MockMvcBuilders.standaloneSetup(new TransferObjectController())
.apply(documentationConfiguration(this.restDocumentation))
.build();
}
#Test
public void testAddTransferObject() throws Exception {
this.mockMvc.perform(post("/transferObjects/{name}", "hi"))
.andExpect(status().isOk()).andDo(document("transferObject",
pathParameters(
parameterWithName("name").description("The name of the new Transfer Object to be created."))));
}
And here is the console while running the test:
11:20:48.148 [main] INFO o.s.w.s.m.m.a.RequestMappingHandlerAdapter - Looking for #ControllerAdvice: org.springframework.test.web.servlet.setup.StubWebApplicationContext#5ab785fe
11:20:48.205 [main] DEBUG o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - Looking for exception mappings: org.springframework.test.web.servlet.setup.StubWebApplicationContext#5ab785fe
11:20:48.283 [main] DEBUG o.s.t.w.s.TestDispatcherServlet - Initializing servlet ''
11:20:48.307 [main] DEBUG o.s.w.c.s.StandardServletEnvironment - Adding [servletConfigInitParams] PropertySource with lowest search precedence
11:20:48.307 [main] DEBUG o.s.w.c.s.StandardServletEnvironment - Adding [servletContextInitParams] PropertySource with lowest search precedence
11:20:48.312 [main] DEBUG o.s.w.c.s.StandardServletEnvironment - Adding [systemProperties] PropertySource with lowest search precedence
11:20:48.312 [main] DEBUG o.s.w.c.s.StandardServletEnvironment - Adding [systemEnvironment] PropertySource with lowest search precedence
11:20:48.313 [main] DEBUG o.s.w.c.s.StandardServletEnvironment - Initialized StandardServletEnvironment with PropertySources [servletConfigInitParams,servletContextInitParams,systemProperties,systemEnvironment]
11:20:48.313 [main] INFO o.s.mock.web.MockServletContext - Initializing Spring FrameworkServlet ''
11:20:48.313 [main] INFO o.s.t.w.s.TestDispatcherServlet - FrameworkServlet '': initialization started
11:20:48.318 [main] DEBUG o.s.t.w.s.TestDispatcherServlet - Unable to locate MultipartResolver with name 'multipartResolver': no multipart request handling provided
11:20:48.318 [main] DEBUG o.s.t.w.s.TestDispatcherServlet - Using LocaleResolver [org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver#63238bf4]
11:20:48.318 [main] DEBUG o.s.t.w.s.TestDispatcherServlet - Using ThemeResolver [org.springframework.web.servlet.theme.FixedThemeResolver#32b97305]
11:20:48.319 [main] DEBUG o.s.t.w.s.TestDispatcherServlet - Using RequestToViewNameTranslator [org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator#2d2e6747]
11:20:48.319 [main] DEBUG o.s.t.w.s.TestDispatcherServlet - Using FlashMapManager [org.springframework.web.servlet.support.SessionFlashMapManager#417e7d7d]
11:20:48.319 [main] DEBUG o.s.t.w.s.TestDispatcherServlet - Published WebApplicationContext of servlet '' as ServletContext attribute with name [org.springframework.web.servlet.FrameworkServlet.CONTEXT.]
11:20:48.319 [main] INFO o.s.t.w.s.TestDispatcherServlet - FrameworkServlet '': initialization completed in 6 ms
11:20:48.319 [main] DEBUG o.s.t.w.s.TestDispatcherServlet - Servlet '' configured successfully
11:20:48.361 [main] DEBUG o.s.t.w.s.TestDispatcherServlet - DispatcherServlet with name '' processing POST request for [/transferObjects/hi]
11:20:48.364 [main] DEBUG o.s.t.w.s.s.StandaloneMockMvcBuilder$StaticRequestMappingHandlerMapping - Looking up handler method for path /transferObjects/hi
11:20:48.368 [main] DEBUG o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - Resolving exception from handler [null]: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported
11:20:48.369 [main] DEBUG o.s.w.s.m.a.ResponseStatusExceptionResolver - Resolving exception from handler [null]: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported
11:20:48.369 [main] DEBUG o.s.w.s.m.s.DefaultHandlerExceptionResolver - Resolving exception from handler [null]: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported
11:20:48.369 [main] WARN o.s.web.servlet.PageNotFound - Request method 'POST' not supported
11:20:48.370 [main] DEBUG o.s.t.w.s.TestDispatcherServlet - Null ModelAndView returned to DispatcherServlet with name '': assuming HandlerAdapter completed request handling
11:20:48.370 [main] DEBUG o.s.t.w.s.TestDispatcherServlet - Successfully completed request
It appears I was able to fix this problem.
Taking a closer look at the console while running the test I noticed:
Mapped "{[],methods=[GET],produces=[application/json]}" onto public java.util.List<common.to.TransferObject> sf.common.controller.TransferObjectController.getTransferObjects()
Mapped "{[/{name}],methods=[POST]}" onto public void sf.common.controller.TransferObjectController.addTransferObject(java.lang.String)
Mapped "{[/{name}],methods=[DELETE]}" onto public void sf.common.controller.TransferObjectController.deleteTransferObject(java.lang.String)
this shows that it is mapping the controller request mappings to the specific RequestMapping the method holds. #RestController("/transferObjects") is not actually defining the mapping as a parent. For me to do that I must include #RequestMapping("/transferObjects") underneath the #RestController.
So by changing the parent mapping I can now use the following test case:
#Test
public void testAddTransferObject() throws Exception {
this.mockMvc.perform(post("/transferObjects/{name}", "hi"))
.andExpect(status().isOk()).andDo(document("transferObject",
pathParameters(
parameterWithName("name").description("The name of the new Transfer Object to be created."))));
}

Why can't #Value inject a JNDI value from the Environment?

I'm having trouble injecting JNDI values from my Tomcat into spring java config using the #Value annotation whereas I have no trouble retreiving the values via the Environment class. We are using Java 1.7.0_17, Spring 4.0.3, and Tomcat 7.0.52 .
I have in my context.xml the following variables defined:
<Environment name="USER_NAME" value="initech" type="java.lang.String" />
<Environment name="USER_PASSWORD" value="1n3+3ch!" type="java.lang.String" />
In my Java configuration file I have the following code working:
#Bean
public Foo foo( Environment env ){
return new Foo( env.getProperty("USER_NAME"), env.getProperty("USER_PASSWORD") );
}
When I look into the server log I see:
12:50:45.214 [RMI TCP Connection(3)-127.0.0.1] DEBUG o.s.c.e.PropertySourcesPropertyResolver -> Searching for key 'USER_NAME' in [servletConfigInitParams]
12:50:45.214 [RMI TCP Connection(3)-127.0.0.1] DEBUG o.s.c.e.PropertySourcesPropertyResolver -> Searching for key 'USER_NAME' in [servletContextInitParams]
12:50:45.214 [RMI TCP Connection(3)-127.0.0.1] DEBUG o.s.c.e.PropertySourcesPropertyResolver -> Searching for key 'USER_NAME' in [jndiProperties]
12:50:45.214 [RMI TCP Connection(3)-127.0.0.1] DEBUG o.springframework.jndi.JndiTemplate -> Looking up JNDI object with name [java:comp/env/USER_NAME]
12:50:45.214 [RMI TCP Connection(3)-127.0.0.1] DEBUG o.s.jndi.JndiLocatorDelegate -> Located object with JNDI name [java:comp/env/USER_NAME]
12:50:45.214 [RMI TCP Connection(3)-127.0.0.1] DEBUG o.s.jndi.JndiPropertySource -> JNDI lookup for name [USER_NAME] returned: [initech]
12:50:45.214 [RMI TCP Connection(3)-127.0.0.1] DEBUG o.s.c.e.PropertySourcesPropertyResolver -> Found key 'USER_NAME' in [jndiProperties] with type [String] and value 'initech'
However, I would like to use #Value annotation to make the code cleaner if at all possible; something along the lines of
#Bean
public Foo foo(
#Value( "#{USER_NAME}" ) String userName,
#Value( "#{USER_PASSWORD}" ) String userPassword
){
return new Foo( userName, userPassword );
}
but this code results in the error:
nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 0): Property or field 'USER_NAME' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public?; nested exception is org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 0): Property or field 'USER_NAME' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public?; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'oauthTokenRequestClient' defined in class path resource [com/initech/set/gw/api/config/GatewayRepositoryConfig.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [java.net.URI]: : Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 0): Property or field 'USER_NAME' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public?; nested exception is org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 0): Property or field 'USER_NAME' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public?
org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:747)
org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:462)
I have tried "#{systemEnvironment.USER_NAME}, "#{systemProperties.USER_NAME}, "#{jndiProperties.USER_NAME}, and "#{java:comp/env/USER_NAME} as well: yet, none of them work.
PS- I've tried to simplify the question so if one of these should have worked let me know because I may have made an error in my posting.
I solved it using the selected answer from the question Java Spring: How to use #Value annotation to inject an Environment property?.
So my code looks like:
#Bean
public Foo foo(
#Value( "#{environment.USER_NAME}" ) String userName,
#Value( "#{environment.USER_PASSWORD}" ) String userPassword
){
return new Foo( userName, userPassword );
}
I'm assuming from the content of the most upvoted answer (ie "assuming that you have a PropertySourcesPlaceHolderConfigurer registered") to that quesiton that this means we are lacking a configured PropertySourcesPlaceHolderConfigurer.
If you are using Spring 4 you should change
#Value( "#{USER_PASSWORD}" ) String userPassword
To
#Value( "${USER_PASSWORD}" ) String userPassword
Try that and see what happens.
For Spring 3.1+ (tested on 4.2.5.RELEASE), you can use #Value like this:
#Value("${property.name}")
private String jndiProperty;
But you also need to define a PropertySourcesPlaceholderConfigurer bean in a static method and configure a JndiPropertySource (I have this in an #Configuration class):
#Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer(ConfigurableEnvironment env) {
PropertySourcesPlaceholderConfigurer placeholderConfigurer = new PropertySourcesPlaceholderConfigurer();
JndiPropertySource jndiPropertySource = new JndiPropertySource("java:comp");
env.getPropertySources().addFirst(jndiPropertySource);
return placeholderConfigurer;
}
Note the use of addFirst() which ensures property values from JNDI take precedence in case of a clash with other property sources, which may or may not be desired, in which case one can use other methods like addLast() etc.
This is the correct syntex
#Value("${PASSWORD}")
Have a look at this example:
http://www.programsji.com/spring/readpropertyfile

Resources