Swagger with Spring Rest : api-docs does not generate the json

296 views
Skip to first unread message

SmithTech

unread,
Mar 24, 2016, 4:25:36 AM3/24/16
to Swagger
Hi,
 
I am trying to integrate swagger2 with my Spring MVC. But when I try to access http://myhost/myapp/api-docs, I am not able to get the json. Could you please help me to figure out whats going wrong with my code.
 
My Pom.xml (swagger related)
 

<dependency>

       <groupId>io.springfox</groupId>

      <artifactId>springfox-swagger2</artifactId>

      <version>2.2.2</version>

</dependency>

<dependency>

      <groupId>io.springfox</groupId>

      <artifactId>springfox-swagger-ui</artifactId>

      <version>2.2.2</version>

</dependency>

My web.xml

<servlet>

   <servlet-name>mvc-dispatcher</servlet-name>

   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

   <load-on-startup>1</load-on-startup>

</servlet>

    <servlet-mapping>

    <servlet-name>mvc-dispatcher</servlet-name>

    <url-pattern>/*</url-pattern>

</servlet-mapping>


My mvc-dispatcher.xml

<context:component-scan base-package="com.smith.spring.ws" />

<context:component-scan base-package="com.smith.spring.swagger" />

<mvc:annotation-driven/>

<mvc:resources location="classpath:/META-INF/resources/swagger-ui.html" mapping="swagger-ui.html" />

<mvc:resources location="classpath:/META-INF/resources/webjars/" mapping="/webjars/**" /> 


SwaggerConfig.java


@Configuration 
@EnableSwagger2 
public class SwaggerConfig  { 
    @Autowired 
    private TypeResolver typeResolver; 
    @Bean 
    public Docket api()    { 
        return new Docket(DocumentationType.SWAGGER_2) 
            .select() 
            .apis(RequestHandlerSelectors.basePackage("com.smith.spring.ws")) 
            .paths(PathSelectors.any()) 
            .build() 
            .pathMapping("/") 
            .directModelSubstitute(LocalDate.class, String.class) 
            .genericModelSubstitutes(ResponseEntity.class) 
            .alternateTypeRules( 
                newRule( 
                    typeResolver.resolve( 
                        DeferredResult.class, 
                        typeResolver.resolve(ResponseEntity.class, WildcardType.class)), 
                    typeResolver.resolve(WildcardType.class))) 
            .useDefaultResponseMessages(false) 
            .globalResponseMessage( 
                RequestMethod.GET, 
                newArrayList( 
                    new ResponseMessageBuilder() 
                        .code(500) 
                        .message("500 message") 
                        .responseModel(new ModelRef("Error")) 
                        .build())) 
            .enableUrlTemplating(false); 
    }


My Controller


RestController
@RequestMapping("/person")
@Api(value="Person Rest Service")
public class PersonController {
 @RequestMapping(value="/getPerson",method=RequestMethod.GET)
 @ApiOperation(value = "Fetch a person")
 public @ResponseBody Person getPersons()
 {
  Person person = new Person();
  person.setFirstname("Smitha");
  person.setLastname("Rajiv");
  person.setAge(37);
  person.setDepartment("Infosys-INDD");
  return person;
 }



I am not sure if I am missing anything here. But when I try to access http://localhost:8080/MyApp/api-docs, I am not able to see the json.


I would be really grateful if someone can throw light on this.


thanks

Smith


 

 
 

Reply all
Reply to author
Forward
0 new messages