Use of "reference" element in #ApiResponse annotation in swagger to refer response samples - spring-boot

I need to show the response example value with respect to each code in Swagger UI, I'm using Spring Boot + springfox-swagger2

It seems that the "reference" attribute is not been read by during the generation of the swagger file (see https://github.com/springfox/springfox/issues/1705)

Related

Get Jackson ObjectMapper in Quarkus

I am writing a custom OpenApiConfigurator that adds some examples to my api dynamically.
When I add the examples using the value field of io.smallrye.openapi.api.models.examples.ExampleImpl, which is an object, the example is null in swagger-ui. It only works when I added the actual json.
To add the actual json I have to generate it from my response dto using Jackson. But how can I access the quarkus object mapper, for which I have some customisations using ObjectMapperCustomizer, if in the OpenApiConfigurator CDI is not available?
It's actually possible to access the CDI container statically with Arc.container().instance(ObjectMapper::class.java).get()
That solved it for me.

Swagger on Spring Boot Actuator

I'm documenting my API with Swagger and I'm applying the following annotation to all of my REST Controllers:
#Api(value = "Some value", description = "Some Description", tags = {"Tag Tag Tag"})
However, I also want to include the endpoints provided by Spring Boot Actuator (/health, /info) on Swagger (and they are being included), however I can't seem to find a way to change their default description, tags, and title, among other swagger properties.
Is there a way to do this? Thanks in advance.

generate sample example in swagger UI (in Spring boot project)

I am using a spring boot application and have configured using Swagger UI.
I want to know whether we could pre-populate the example value with sample value so we can hit the "Try it out!" button without having to type in some sample values to get a response.
It must be present there.
Is there a way we can do this using annotations or a separate file which Swagger uses?
I am using a spring boot project with springfox-swagger2:2.7.0 and springfox-swagger-ui:2.7.0 with dependencies added using gradle.
Since the #ApiParam properties example and examples are not working (see this issue on GitHub), support for adding examples is limited/non existing.
What you can do for simple parameters (#RequestParam) is to add the #ApiParam annotation with the defaultValue property, like this:
#GetMapping
public List<Foo> findAll(
#RequestParam(required = false)
#ApiParam(defaultValue = "foo") // Put the default value here
String input) {
// ...
}
However, there is no support yet for doing this with #RequestBody parameters.
A possible workaround for #RequestBody parameters is by clicking on the code box at the right side of the Swagger tester, where it says Example value. If you click on it, it will insert that example into the field itself.
Here is a workaround for providing an example:
Inject html into swagger
#ApiParam(
name="whatever",
value="whatever",
defaultValue="none</p><p>Example: xyz</p>"
)
They don't protect against it in the latest version 2.9.2.

How can I retrieve io.swagger.models.Swagger object in a jersey+swagger-based system?

I want to get io.swagger.models.Swagger object in my system, which is a restful backend based on jersey and swagger.
I saw in class ApiListingResource there is such a statement
Swagger swagger = (Swagger) context.getAttribute("swagger");
, which can retrieve the swagger object from servlet context.
Can I do the same in my own code? This seems not a contract that the attribute name will always be "swagger". So I dare not.
Is there any reliable way to retrieve the object?
You can rely on the context (yes, it is set to the name swagger, or with your own logic by extending BeanConfig

Jersey 2: What replaces FEATURE_FILTER_FORWARD_ON_404?

What is the Jersey 2 equivalent of ServletContainer.FEATURE_FILTER_FORWARD_ON_404 from Jersey 1?
ServletContainer.FEATURE_FILTER_FORWARD_ON_404 is defined as:
If true and a 404 response with no entity body is returned from either the runtime or the application then the runtime forwards the request to the next filter in the filter chain
Please explain why you are downvoting. I can't improve the question/answer if you don't provide an explanation of what is wrong.
The properties you should be using are ServletProperties.FILTER_FORWARD_ON_404 and ServletProperties.FILTER_STATIC_CONTENT_REGEX.
ServletProperties.FILTER_FORWARD_ON_404 is defined as:
If set to true and a 404 response with no entity body is returned from either the runtime or the application then the runtime forwards the request to the next filter in the filter chain.
ServletProperties.FILTER_STATIC_CONTENT_REGEX is defined as:
If set the regular expression is used to match an incoming servlet path URI to some web page content such as static resources or JSPs to be handled by the underlying servlet engine.
#gili is correct. For a complete running example of the jersey 2.x config check out my simple toy project on github:
https://github.com/depsypher/flapjack
Essentially you'll have to run the Jersey ServletContainer as a filter and provide the jersey.config.servlet.filter.forwardOn404 property as an init param.
Here's an example of the setup using Spring Boot; the web.xml equivalent should be pretty obvious:
FilterRegistrationBean filter = new FilterRegistrationBean(new ServletContainer());
filter.addInitParameter("jersey.config.servlet.filter.forwardOn404", "true");

Resources