How to add swagger in existing servlet project - servlets

I want to add swagger to an existing servlet project and I have done my web.xml as shown
<servlet>
<servlet-name>DefaultServletConfig</servlet-name>
<servlet-class>io.swagger.servlet.config.DefaultServletConfig</servlet-class>
<init-param>
<param-name>swagger.resource.package</param-name>
<param-value>bsnl.inbound</param-value>
</init-param>
<init-param>
<param-name>swagger.api.basepath</param-name>
<param-value>http://localhost:8080/comm</param-value>
</init-param>
<init-param>
<param-name>api.version</param-name>
<param-value>1.0.0</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet>
<servlet-name>ApiDeclarationServlet</servlet-name>
<servlet-class>io.swagger.servlet.listing.ApiDeclarationServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ApiDeclarationServlet</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
It is working for only one class in a package and only for the GET method, it is not calling the post method. But I want to configure for multiple packages.
If anyone having any idea, please help me.

Related

<context-param> in java based configuration

Below is the web.xml of my project.
<web-app> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/config/applicationContext-service.xml /WEB-INF/config/applicationContext-dao.xml </param-value> </context-param> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/config/servlet-context.xml ***/WEB-INF/config/applicationContext.xml*** </param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> </web-app>
If I want to convert my project into java based configuration, I know that webapplicationcontext can be configured by using AbstractAnnotationConfigDispatcherServletInitializer or webapplicationinitializer.
But how can i configure context-param using java configuration ?
What you can do is you can use servlet context to store the param name and param values by using setattribute and getattribute methods of servlet context by which you can acess them any where throughout your application and check this for more clarification.
https://www.javatpoint.com/servletcontext

/swagger-resources/configuration/ui 404 error

I'm running in springfox v2.7.0 in my java spring based application. We have 3 dispatch serverlets configured in web.xml:
1)common springmvc servlet [Active]
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/config/common/springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>`
2 ) springmvc-v1 servlet [inactive]: which is disabled by commenting out it in web.xml and not in use.
<servlet>
<servlet-name>springmvc-v1</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
com.test.mywebservices.v1.config.WebConfig
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
3)springmvc-v2 servlet [Active]: for this I am implimenting swagger.
<servlet>
<servlet-name>springmvc-v2</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
com.test.mywebservices.v2.config.WebConfig
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc-v2</servlet-name>
<url-pattern>/v2/*</url-pattern>
</servlet-mapping>
And it works fine, and i can get to the swagger-resources from here:
https://localhost:9002/mywebservices/v2/swagger-resources
And i can view the api from here:
https://localhost:9002/mywebservices/v2/v2/api-docs
And Swagger-UI is accessible from here:
https://localhost:9002/mywebservices/swagger-ui.html
The problem is when i load the https://localhost:9002/mywebservices/swagger-ui.html page it is trying to access the swagger-resources from here:
https://localhost:9002/mywebservices/swagger-resources
And they are not available. But it is available at https://localhost:9002/mywebservices/v2/swagger-resources
How can i fix this so that the swagger-ui.html :
I think Swagger-ui.html should be available at: https://localhost:9002/mywebservices/v2/swagger-ui.html so that springfox.js will get the right baseUrl.
https://localhost:9002/mywebservices/swagger-ui.html >>> pages loads but gives popup to enter the Base URL
https://localhost:9002/mywebservices/swagger-resources/configuration/ui >>> gives 404 hence popup appears
https://localhost:9002/mywebservices/webjars/springfox-swagger-ui/springfox.js >>> Able to access.This is working fine.
Please suggest the solution.
Thanks in advance.
I was facing the same problem.
The solution was to map on the MVC config of your app, manually the Controller managing the mapping of swagger-resources that exists on swagger-common.jar. The package value is "springfox.documentation.swagger.web". The configuration, depending on your app, should look similar that the one shown below:
#Configuration
#EnableWebMvc
#ComponentScan(basePackages = { ApplicationPackages.REST_CONTROLLER_BASE_PACKAGE,
"springfox.documentation.swagger.web" })
public class MvcConfig extends WebMvcConfigurerAdapter {
}

HDIV parameter do not exist

I am trying to integrate HDIV with my web application using HDIV struts 1.jar
I encounter the below error when trying to load the login page.
(self-tuning)'][][](org.hdiv.logs.Logger) - HDIV_PARAMETER_DOES_NOT_EXIST;/abc/def/index.do;_HDIV_STATE_;;;127.0.0.1;127.0.0.1;anonymous;
Possible checks I did:
tried with addition of struts--el tag lib with HDIV--el tag lib
tried with filter mapping pattern to all url
Not sure where I am going wrong. Many searches do not provide me a proper solution. Any guidance is highly appreciated. Thank you
Here are my config files.
hdiv-config.xml. I named it as struts-config-hdiv.xml
<hdiv:config excludedExtensions="css,png,gif,jpg,html,js" protectedExtensions=".*.do">
</hdiv:config>
<hdiv:config>
<hdiv:startPages>/index.html</hdiv:startPages>
<hdiv:startPages>/fsm/efsm/index.do</hdiv:startPages>
</hdiv:config>
<hdiv:config errorPage="/fsm/jsp/tiles/common/ErrorPage.jsp">
<hdiv:startPages>/index.html</hdiv:startPages>
<hdiv:startPages>/fsm/efsm/index.do</hdiv:startPages>
</hdiv:config>
<hdiv:editableValidations registerDefaults="true">
<hdiv:validationRule url=".*"></hdiv:validationRule>
</hdiv:editableValidations>
<hdiv:config showErrorPageOnEditableValidation="true">
</hdiv:config>
<hdiv:config maxPagesPerSession="5">
</hdiv:config>
<hdiv:config avoidValidationInUrlsWithoutParams="true">
</hdiv:config>
`
web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/struts-config/struts-config-hdiv.xml</param-value>
</context-param>
<filter>
<filter-name>ValidatorFilter</filter-name>
<filter-class>org.hdiv.filter.ValidatorFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>ValidatorFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.hdiv.listener.InitListener</listener-class>
</listener>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>
org.apache.struts.action.ActionServlet
</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>
/WEB-INF/struts-config/struts-config-default.xml
</param-value>
</init-param>
<init-param>
<param-name>config/hdiv</param-name>
<param-value>
/WEB-INF/struts-config/struts-config-hdiv.xml
</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>validating</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<taglib>
<taglib-uri>/WEB-INF/tld/struts-html.tld</taglib-uri>
<taglib-location>/WEB-INF/tld/hdiv-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/tld/struts-nested.tld</taglib-uri>
<taglib-location>/WEB-INF/tld/hdiv-nested.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/tld/struts-logic.tld</taglib-uri>
<taglib-location>/WEB-INF/tld/hdiv-logic.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/tld/struts-html-el.tld</taglib-uri>
<taglib-location>/WEB-INF/tld/hdiv-html-el.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/tld/struts-logic-el.tld</taglib-uri>
<taglib-location>/WEB-INF/tld/hdiv-logic-el.tld</taglib-location>
</taglib>
`
pom.xml
<dependency>
<groupId>org.hdiv</groupId>
<artifactId>hdiv-config</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>org.hdiv</groupId>
<artifactId>hdiv-struts-1</artifactId>
<version>3.3.0</version>
</dependency>
According to your post I see two possible solutions:
You must process the reported URL at server side using the custom tags offered by Struts 1 to render linksr. In that way Hdiv will include the additional hdiv parameter.
If the URL is a start page (the entry point to the application) where the user starts the browsing (in that case you are not rendering the link) you must define a start page within Hdiv configuration.
<hdiv:config>
<hdiv:startPages>XXX the URL XX</hdiv:startPages>
Regards,
Roberto Velasco (Hdiv Security)

Sniffy filter setup together with encoding filter?

I'm trying to add Sniffy profiler into JSF project.
According to the documentation web.xml needs to be updated with following filter:
<filter>
<filter-name>sniffer</filter-name>
<filter-class>io.sniffy.servlet.SnifferFilter</filter-class>
<init-param>
<param-name>inject-html</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>enabled</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>sniffer</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
But web.xml already contains one filter:
<filter>
<filter-name>Character Encoding Filter</filter-name>
<filter-class>org.primefaces.titan.filter.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Character Encoding Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
When sniffer filter is added (and works correctly) Character Encoding Filter stops working (characters are garbled).
How should web.xml looks like to have both filters working?
Sniffy developer here. It is a bug in Sniffy - I plan to fix it in upcoming 3.1 release.
As a workaround you can add following to your JVM arguments:
-Dfile.encoding=UTF-8

Filtering static content Jersey

I'm trying to serve static content (a HTML form that calls a Jersey REST resource) from the same webapp as the servlet that handles the requests to the resource. As I understand I can filter requests to static content away from the Jersey servlet. My web.xml is as follows, but at the moment I am unable to access the static content nor the resource...both were working separately.
<filter>
<filter-name>my-filter</filter-name>
<filter-class>com.sun.jersey.spi.container.servlet.ServletContainer</filter-class>
<init-param>
<param-name>com.sun.jersey.config.property.WebPageContentRegex</param-name>
<param-value>/*.html</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>my-filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>my-service</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.mydomain.ws.myservice</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>my-service</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
FWIW your original problem was probably because the param-value for the WebContentRegex was not a regular expression. Ok techincally it was, but it is not matching what you probably want. You should try something like /.*.html instead.
I setup my services such that the rest service are under their own subpath separate from the static content:
<servlet-mapping>
<servlet-name>ServletAdaptor</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>

Resources