wkennedy / swagger4spring-web

Swagger support for Spring MVC
89 stars 46 forks source link

Class Cast Exception with Generic Response type #6

Closed etzlers closed 11 years ago

etzlers commented 11 years ago

15:27:32.212 [http-bio-8080-exec-3] DEBUG o.s.web.servlet.DispatcherServlet - Could not complete request
java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.TypeVariableImpl cannot be cast to java.lang.Class
    at com.wordnik.swagger.core.ApiPropertiesReader$.getDataType(ApiPropertiesReader.scala:97) ~[swagger-core_2.9.1-1.2.2.jar:1.2.2]
    at com.wordnik.swagger.core.ApiPropertiesReader$.getDataType(ApiPropertiesReader.scala:83) ~[swagger-core_2.9.1-1.2.2.jar:1.2.2]

This is specifically referencing the line below in the ApiPropertiesReader class.

} else if (!returnType.getClass.isAssignableFrom(classOf[ParameterizedTypeImpl]) && returnType.asInstanceOf[Class[_]].isArray) {

This is an issue because generics are necessary for us, it would be great if this could be resolved quickly or if you could possibly offer some workaround as we are big fans of Swagger and would love to use it.

This is what the class the exception is occurring on looks like:

@JsonSerialize @JsonInclude(Include.NON_EMPTY) public class PageDTO< T > implements Serializable { private static final long serialVersionUID = 1L;

private List< T > content; private Long total;

public PageDTO() { content = new ArrayList< T >(); }

public PageDTO(T element) { content = new ArrayList< T >(); total = Long.valueOf(0); if(element != null) { content.add(element); total = Long.valueOf(1); } }

public PageDTO(List< T > content, Long total) { this.content = content; this.total = total; }

public List< T > getContent() { return content; }

public Long getTotal() { return total; }

public void setTotal(Long total) { this.total = total; }

public void setContent(List< T > content) { this.content = content; }

}

Sorry for the lack of code tags, I'm still new to the site and I couldn't get the < T > to display when I was using them.

I asked about this problem in issue #145 on Swagger-core but the person who was talking to me about it seemed to think it was related to this project so I've posted this here.

fehguy commented 11 years ago

Confirmed this is an issue in swagger-core. Take a look here for an update:

https://github.com/wordnik/swagger-core/issues/145#issuecomment-16867607

wkennedy commented 11 years ago

Until swagger4spring-web is updated to use Swagger 1.2.3, you can use this workaround.

<dependency>
    <groupId>com.wordnik</groupId>
    <artifactId>swagger-core_2.9.1</artifactId>
    <version>1.2.3-SNAPSHOT</version>
    <exclusions>
        <exclusion>
            <artifactId>jackson-annotations</artifactId>
            <groupId>com.fasterxml.jackson.core</groupId>
        </exclusion>
        <exclusion>
            <artifactId>jackson-core</artifactId>
            <groupId>com.fasterxml.jackson.core</groupId>
        </exclusion>
        <exclusion>
            <artifactId>jackson-databind</artifactId>
            <groupId>com.fasterxml.jackson.core</groupId>
        </exclusion>
        <exclusion>
            <artifactId>guava</artifactId>
            <groupId>com.google.guava</groupId>
        </exclusion>
        <exclusion>
            <artifactId>paranamer</artifactId>
            <groupId>com.thoughtworks.paranamer</groupId>
        </exclusion>
        <exclusion>
            <artifactId>slf4j-api</artifactId>
            <groupId>org.slf4j</groupId>
        </exclusion>
    </exclusions>
</dependency>

<dependency>
    <groupId>com.knappsack</groupId>
    <artifactId>swagger4spring-web</artifactId>
    <version>0.1.6</version>
    <exclusions>
        <exclusion>
            <artifactId>slf4j-log4j12</artifactId>
            <groupId>org.slf4j</groupId>
        </exclusion>
        <exclusion>
            <artifactId>junit</artifactId>
            <groupId>junit</groupId>
        </exclusion>
        <exclusion>
            <artifactId>slf4j-api</artifactId>
            <groupId>org.slf4j</groupId>
        </exclusion>
        <exclusion>
            <artifactId>swagger-annotations_2.9.1</artifactId>
            <groupId>com.wordnik</groupId>
        </exclusion>
        <exclusion>
            <artifactId>swagger-core_2.9.1</artifactId>
            <groupId>com.wordnik</groupId>
        </exclusion>
    </exclusions>

You will also need to add this to your pom.xml file to get the Swagger SNAPSHOT:

 <repositories>
    <repository>
      <id>sonatype snapshots</id>
      <name>sonatype snapshots</name>
      <layout>default</layout>
      <url>https://oss.sonatype.org/content/repositories/snapshots</url>
      <snapshots>
        <enabled>true</enabled>
      </snapshots>
     </repository>
  </repositories>

or you can change your Maven settings.xml like so:

 <profiles>
   <profile>
     <id>allow-snapshots</id>
     <activation><activeByDefault>true</activeByDefault></activation>
     <repositories>
        <repository>
         <id>snapshots-repo</id>
         <url>https://oss.sonatype.org/content/repositories/snapshots</url>
         <releases><enabled>false</enabled></releases>
         <snapshots><enabled>true</enabled></snapshots>
       </repository>
     </repositories>
   </profile>
 </profiles>
etzlers commented 11 years ago

No more exception! Thank you!!