Spring 4 + @ControllerAdvice: Jsonp support for ResponseBody causes Deployment Exception?

1.7k views Asked by At

I am adding JSONP support to a REST Service in SPRING4 + JDK 8 + STS 3.6.4

Versions: Spring 4.1.6.RELEASE

My implementation is based on these links: http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#mvc-ann-jsonp

The REST service returns ResponseEntity or ResponseBody and use case is to return data in JSONP format.

Added a ControllerAdvice

@ControllerAdvice
public class JsonpCallbackAdvice extends AbstractJsonpResponseBodyAdvice {

    public JsonpCallbackAdvice(){
       super("Callback");
    }
}

Here is the Controller of the REST Service

@Controller
public class AcctController {
 ...
   @RequestMapping(value = "/act/{actNum}", method = RequestMethod.GET)
public ResponseEntity<Account> getAccount(@PathVariable("actNum") Integer accountNum) throws Exception {
...
return new ResponseEntity<account>();
}

Here is the relevant web application context configuration

...
 <context:component-scan base-package="com.controllers" />
<bean name="jsonMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
...

The controller and ControllerAdvice are in same package.

When deployment of the project is initiated following exception is observed

 Caused by: java.lang.ClassCastException: [Ljava.lang.String; cannot be cast to java.lang.String
at org.springframework.context.annotation.AnnotationBeanNameGenerator.determineBeanNameFromAnnotation(AnnotationBeanNameGenerator.java:91)
at org.springframework.context.annotation.AnnotationBeanNameGenerator.generateBeanName(AnnotationBeanNameGenerator.java:69)
at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.doScan(ClassPathBeanDefinitionScanner.java:246)
at org.springframework.context.annotation.ComponentScanBeanDefinitionParser.parse(ComponentScanBeanDefinitionParser.java:84)
at org.springframework.beans.factory.xml.NamespaceHandlerSupport.parse(NamespaceHandlerSupport.java:74)
at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1427)
at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1417)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:174)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:144)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:100)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:510)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:392)

This exception was not happening before the ControllerAdvice was added. Also, it does not happens when @ControllerAdvice annotation is removed or the component scan excludes scanning package of the ControllerAdvice class

I tried with Spring version 4.2.0.RC1, and the exception still happens.

Kindly help with resolution of this exception, since not much help is available online. It seems like a bug in SPRING 4, however am not sure.

0

There are 0 answers