Hello,
Maybe four years later the answer comes a little later, but better late than never.
If you have the correct swag file (not just a snippet) as shown below
openapi: "3.0.0" : paths: /example: get: operationId: showIt :
and you start code generation, in this explanation for jaxs-jersey-server without any specific configuration values ββfor code generation (which you can download from the Swagger editor ), you get most of the java sentences, such as:
io.swagger.api. ExampleApi io.swagger.api. ExampleApiService io.swagger.api.factories.ExampleApiServicefactory io.swagger.api.impl. ExampleApiServiceImpl
In the implementation of the REST endpoint ExampleApiServiceImpl, you see more or less something like the following:
package io.swagger.api.impl; : import ... ; : @javax.annotation.Generated(...) public class ExampleApiServiceImpl extends ExampleApiService {
You are now sharing a magical comment
// do some magic!
perhaps through
String className = this.getClass().getSimpleName(); System.out.println("Entered REST endpoint: path=|" + className.substring(0, className.length() - 14) + "| operationId=|showId|");
you should see a message in the log if you call the endpoint from the browser after you mvn clean package jetty:run . But this is not a good idea, as you understand, because after the next generation your changes have disappeared.
In this context, you should never change the manually generated code, because it MUST be so well documented that the future colleague (who may even be you in a few months or years), even in the half-sleepy Sundays on Monday night, changes again after the next code generation. But my more than 20 years of experience working with various code generators says only one thing about it : forget about it! For the same reason, it is not practical to prevent further generation after the first generation, because this should also be documented in detail. Otherwise, the debugging hour compared to the debugging hour can lead to troubleshooting why the new feature does not work.
But all this is not necessary.
In the generated io.swagger.api.ExampleApi class , you will find a constructor similar to the following (ok, this is state 2019-05-17. I don't know if it was the same (or similar) four years ago)
package io.swagger.api; : import ... ; : @Path("/example") @javax.annotation.Generated(...) public class ExampleApi { private final ExampleApiService delegate; public ExampleApi(@Context ServletConfig servletContext) {
An important piece of code is servletContext.getInitParameter("...") . If you are now ExampleApi.implementation in the servlet configuration, a key named ExampleApi.implementation with the fully qualified name of the java class, which is obtained from ExampleApiService you have implemented your own endpoint code that is safe for rewriting in future generations of code.
To complete the example, this specification will be made in the web.xml (additionally generated, sorry, you cannot have everything). This file contains something like:
<servlet> <servlet-name>jersey</servlet-name> <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> ... <load-on-startup>1</load-on-startup> </servlet>
In this XML snippet, you should insert the following after periods (which indicate other servlet configuration parameters):
<init-param> <param-name>ExampleApi.implementation</param-name> <param-value>my.swagger.api.MyExample</param-value> </init-param>
You look good
whoever you are now!