相关疑难解决方法(0)

如何自定义Spring Boot隐式使用的Jackson JSON映射器?

我正在使用Spring Boot(1.2.1),其方式与构建RESTful Web服务教程的方式类似:

@RestController
public class EventController {

    @RequestMapping("/events/all")
    EventList events() {
        return proxyService.getAllEvents();
    }

}
Run Code Online (Sandbox Code Playgroud)

所以上面,Spring MVC暗中使用Jackson将我的EventList对象序列化为JSON.

但我想对JSON格式进行一些简单的自定义,例如:

setSerializationInclusion(JsonInclude.Include.NON_NULL)
Run Code Online (Sandbox Code Playgroud)

问题是,自定义隐式JSON映射器的最简单方法什么?

我在这篇博客文章中尝试了这种方法,创建了一个CustomObjectMapper等等,但是第3步"在Spring上下文中注册类"失败了:

org.springframework.beans.factory.BeanCreationException: 
  Error creating bean with name 'jacksonFix': Injection of autowired dependencies failed; 
  nested exception is org.springframework.beans.factory.BeanCreationException: 
  Could not autowire method: public void com.acme.project.JacksonFix.setAnnotationMethodHandlerAdapter(org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter); 
  nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: 
  No qualifying bean of type [org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter]   
  found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. …
Run Code Online (Sandbox Code Playgroud)

java spring spring-mvc jackson spring-boot

89
推荐指数
8
解决办法
13万
查看次数

Springboot swagger url 显示 WhiteLabel 错误页面

这是我的代码:我从 application.properties 文件 SwaggerConfig.java 获取所有值

@Configuration
@EnableSwagger2
@Profile("!prod")
@PropertySource(value = { "classpath:application.properties" })
public class SwaggerConfig {

    @Value("${swagger.api.title}")
    private String title;

    @Value("${swagger.api.description}")
    private String description;

    @Value("${swagger.api.termsOfServiceUrl}")
    private String termsOfServiceUrl;

    @Value("${swagger.api.version}")
    private String version;

    @Value("${swagger.api.controller.basepackage}")
    private String basePackage;


    @Bean
    public Docket postMatchApi() {
        return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.basePackage(basePackage))
                .paths(PathSelectors.ant("/**")).build().apiInfo(metaData());
    }

    private ApiInfo metaData() {
        return new ApiInfoBuilder().title(title).description(description).termsOfServiceUrl(termsOfServiceUrl)
                .version(version).build();
    }
Run Code Online (Sandbox Code Playgroud)

这是我的 springboot 初始化程序:

@SpringBootApplication
@ComponentScan(basePackages = { "com.example.demo" })
@ComponentScan(basePackageClasses = {AppInitializer.class, SwaggerConfig.class})
@EnableAsync
@EnableRetry
public class AppInitializer{

    public static void main(String[] args) { …
Run Code Online (Sandbox Code Playgroud)

java spring gradle spring-boot

7
推荐指数
6
解决办法
2万
查看次数

Swagger UI没有列出任何控制器/端点,尽管我能够在v2 / api-docs端点下看到json

我无法使Swagger UI与我的项目一起使用。Swagger UI很好用,但是它没有列出我的任何REST控制器。

我正在使用SPRING 4.2.6.RELEASE和Swagger 2.5.0。我的其余服务已部署到Tomcat 7.0.54。

当Tomcat 7.0.54出现时,它能够获取迅速的端点。我能够找到提取json消息的端点v2 / api-docs。我也可以点击swagger-ui,但没有列出任何控制器。下拉列表为空,如下所示

在此处输入图片说明

**我目前面临的问题是

  1. 我无法获取/ swagger-resources / configuration / ui,而当我启动swagger UI时却遇到404(未找到)错误,而该UI试图获取/ swagger-resources / configuration / ui。我为swagger-resources安装了资源处理程序,但这似乎无济于事。您能否让我知道可能会丢失什么?
  2. 我应该在扩展的WAR中看到META-INF下的resources文件夹吗?META-INF内应该有任何与springfox相关的文件/文件夹吗?**

Swagger的Maven依赖项
io.springfox springfox-swagger2 2.5.0
io.springfox springfox-swagger-ui 2.5.0

以下是我的SwaggerCongifuration

@EnableSwagger2
public class SwaggerConfiguration {

@Bean
public Docket api() {
    List<SecurityContext> security = new ArrayList<SecurityContext>();
    security.add(securityContext());
    return new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.any())
            .paths(PathSelectors.any())
            .build()
            .pathMapping("/").securityContexts(security);
}

private SecurityContext securityContext() {
    return SecurityContext.builder()
            .forPaths(PathSelectors.regex("/"))
            .build();
 }
}
Run Code Online (Sandbox Code Playgroud)

下面是我的WebConfig.xml

@EnableWebMvc
@Configuration
@Import(SwaggerConfiguration.class)
@ComponentScan("com.bank.direct.services")

public class WebConfig extends WebMvcConfigurerAdapter {


@Override …
Run Code Online (Sandbox Code Playgroud)

rest swagger swagger-ui spring-4 swagger-2.0

6
推荐指数
2
解决办法
6670
查看次数