question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

After making changes in ObjectMapper, Swagger Ui prompts "Unable to infer base url"

See original GitHub issue

In spring boot application after making these changes in objectMapper swagger stops working changes done are below:

@bean
public Jackson2ObjectMapperBuilder jacksonBuilder() {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
builder.featuresToEnable(DeserializationFeature.UNWRAP_ROOT_VALUE);
builder.featuresToEnable(SerializationFeature.WRAP_ROOT_VALUE);
return builder;
}

If I remove these above changes swagger starts working but I need the changes, therefore can not remove. I am using version 2.8.0 of springfox-swagger2. After hitting myost:myport/mycontext/swagger-ui.html call to following endpoints is made:

{"UiConfiguration":{"apisSorter":"alpha","jsonEditor":false,"showRequestHeaders":false,"deepLinking":true,"displayOperationId":false,"defaultModelsExpandDepth":1,"defaultModelExpandDepth":1,"defaultModelRendering":"example","displayRequestDuration":false,"docExpansion":"none","filter":false,"operationsSorter":"alpha","showExtensions":false,"tagsSorter":"alpha"}}
{"SecurityConfiguration":{}}
{"List":[{"name":"default","url":"/v2/api-docs","swaggerVersion":"2.0","location":"/v2/api-docs"}]}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
vinucommented, May 14, 2020

Please try with a controller advice like this

@Slf4j
@ControllerAdvice
public class SwaggerResponseAdvice implements ResponseBodyAdvice<Object> {

    @Autowired
    private HttpServletRequest httpServletRequest;

    private final ObjectMapper objectMapper;

    public SwaggerResponseAdvice() {
        objectMapper = new ObjectMapper();
    }

    @Override
    public boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> aClass) {
        if (httpServletRequest.getRequestURI().equalsIgnoreCase("/v2/api-docs")) {
            return true;
        }
        return httpServletRequest.getRequestURI().startsWith("/swagger-resources");
    }

    @Override
    public Object beforeBodyWrite(Object o, MethodParameter methodParameter, MediaType mediaType, Class<? extends HttpMessageConverter<?>> aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
        try {
            serverHttpResponse.getBody().write(objectMapper.writeValueAsBytes(o));
        } catch (IOException e) {
            log.warn("Error writing from advice", e);
        }
        return null;
    }
}
0reactions
stale[bot]commented, Nov 28, 2020

This issue has been automatically closed because it has not had recent activity. Please re-open a new issue if this is still an issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why does springfox-swagger2 UI tell me "Unable to infer base ...
For me the problem was that Spring Security was not allowing access to some resources needed by swagger-ui. The solution was to allow ......
Read more >
[SpringFox Error] Unable to infer base url - Taogen's Blog
I want to use Swagger with Spring Boot, but the Swagger-UI can't work. Following it's my code. pom.xml. 1 2 3 4 5...
Read more >
Unable to infer base url. This is common when using dynamic ...
[Solved]-Unable to infer base url. This is common when using dynamic servlet registration or when the API is behind an API Gateway swagger...
Read more >
Swagger UI Configuration with OKTA - Okta Support
I get a popup with the following text: Unable to infer base url. This is common when using dynamic servlet registration or when...
Read more >
Table of Contents - Micronaut Documentation
The following changes and improvements were added since the last RC: ... Micronaut now includes the ability to generate Swagger (OpenAPI) YAML at...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found