Hi All,

    We are having *User *as model object, and pojo class having following 
fields with getters/setters methods:

    private long id;    
    private String name;    
    private int age;    
    private double salary;

  Using Spring MVC with swagger integration, when I open swagger 
(swagger-ui.html)

  it is displaying json object as below:  

[
  {
    "age": 0,
    "id": 0,
    "name": "string",
    "salary": 0
  }
]

  As per requirement, we'd like to change the property name to appear as "User 
Name" in place of "name" field. The field description 
need be read from .properties file. To implement this change I add following 
code

package com.api.util;
import java.util.HashSet;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.OperationBuilderPlugin;
import springfox.documentation.spi.service.contexts.OperationContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger.common.SwaggerPluginSupport;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket api(){
        return new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.basePackage("com.api.controller"))
            .paths(PathSelectors.any())
            .build().apiInfo(apiInfo());
       
    }
    
    private ApiInfo apiInfo() {
        @SuppressWarnings("deprecation")
                ApiInfo apiInfo = new ApiInfo(
          "Connect Rest API",
          "This API provides  Connect Report data",
          "TOS",
          "Terms of service",
          "Connect Launch",
          "@Use restricted",
          "swagger-ui.html");
        return apiInfo;
    }
    
    @Bean
    public ApiOperationBuilderPlugin apiPlugin() {
        return new ApiOperationBuilderPlugin();
    }

    @Order(SwaggerPluginSupport.SWAGGER_PLUGIN_ORDER)
    public static class ApiOperationBuilderPlugin implements 
OperationBuilderPlugin {

        @Autowired
        protected MessageSource translator;

        public boolean supports(DocumentationType delimiter) {
            return true;
        }

        public void apply(OperationContext context) {
                System.out.println("***** Inside apply OperationContext *****");
            Set<ResponseMessage> messages = 
context.operationBuilder().build().getResponseMessages();
            System.out.println("Inside apply OperationContext 
messages:"+messages);
            
            Set<ResponseMessage> translated = new HashSet<ResponseMessage>();
            for (ResponseMessage untranslated : messages) {
                System.out.println("Inside apply OperationContext 
untranslated:"+untranslated);
                String translation = 
translator.getMessage(untranslated.getMessage(), null, 
untranslated.getMessage(),
                        null);
                System.out.println("Inside apply OperationContext 
untranslated.getMessage:"+untranslated.getMessage());
                //translated.add(new ResponseMessage(untranslated.getCode(), 
translation,
                //        untranslated.getResponseModel(), 
untranslated.getHeaders()));
            }
            context.operationBuilder().responseMessages(translated);
        }

    }


}


 Added System.out lines to see whether it shows anything related to User 
object, but it is showing below lines in console

***** Inside apply OperationContext *****
Inside apply OperationContext 
messages:[springfox.documentation.service.ResponseMessage@c8, 
springfox.documentation.service.ResponseMessage@193, 
springfox.documentation.service.ResponseMessage@191, 
springfox.documentation.service.ResponseMessage@194]
Inside apply OperationContext 
untranslated:springfox.documentation.service.ResponseMessage@c8
Inside apply OperationContext untranslated.getMessage:OK
Inside apply OperationContext 
untranslated:springfox.documentation.service.ResponseMessage@193
Inside apply OperationContext untranslated.getMessage:Forbidden
Inside apply OperationContext 
untranslated:springfox.documentation.service.ResponseMessage@191
Inside apply OperationContext untranslated.getMessage:Unauthorized
Inside apply OperationContext 
untranslated:springfox.documentation.service.ResponseMessage@194



Controller is having below lines:

@RequestMapping(value = "/user/{id}", method = RequestMethod.GET, produces = 
MediaType.APPLICATION_JSON_VALUE)
        public ResponseEntity<User> getUser(@PathVariable("id") long id) {
                System.out.println("Fetching User with id " + id);
                User user = userService.findById(id);
                if (user == null) {
                        System.out.println("User with id " + id + " not 
found");
                        return new ResponseEntity<User>(HttpStatus.NOT_FOUND
);
                }
                return new ResponseEntity<User>(user, HttpStatus.OK);
        }


Can someone please let me know, how to resolve this issue.


Thanks in advance.

Regards,
Sharath Karnati.




-- 
You received this message because you are subscribed to the Google Groups 
"Swagger" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to