How to use swagger-ui with service deployed in tomcat running on eclipse - rest

I have a REST api running at http://localhost:8080/MyService and can see the JSON produced by swagger at http://localhost:8080/MyService/api-docs. In the Servers project in eclipse I have modified web.xml to include the Cors filter:
<filter>
<filter-name>CorsFilter</filter-name>
<filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CorsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
but when I point swagger-ui (loaded in browser from file system) at http://localhost:8080/MyService/api-docs, I get the following error:
Can't read from server. It may not have the appropriate
access-control-origin settings.

Adding the Swagger dist directly to the webapps folder did the trick

Related

Tomcat duplicate httpHeaderSecurity running from Eclipse

I am running Tomcat v8.0 from inside Eclipse. I am trying to open url from the same site in an iFrame. Originally I was getting a message that said X-Frame-Options was set to DENY. So I added the following in the Tomcat
<filter>
<filter-name>httpHeaderSecurity</filter-name>
<filter-class>org.apache.catalina.filters.HttpHeaderSecurityFilter</filter-class>
<async-supported>true</async-supported>
<dispatcher>REQUEST</dispatcher>
<init-param>
<param-name>antiClickJackingOption</param-name>
<param-value>SAMEORIGIN</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>httpHeaderSecurity</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
But now when I try to load the iFrame, it tells me "Refused to display 'http://localhost:8080/hub/warehouses' in a frame because it set multiple 'X-Frame-Options' headers with conflicting values ('SAMEORIGIN, DENY'). Falling back to 'deny'."
I cannot find any place in the workspace that has the default DENY.
How can I resolve this?

Why WildFly ignores filters defined in web.xml?

I have an application running on WildFly-10.1.0.Final. I deployed it correctly, but some entries of my web.xml are ignored.
This is a part of my web.xml:
<filter>
<filter-name>ExtProjectFilter</filter-name>
<filter-class>com.raylloy.filter.ExtProjectFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>ExtProjectFilter</filter-name>
<url-pattern>*.jsf</url-pattern>
</filter-mapping>
When I use a jboss-5.1.0.GA this works fine, a new instance of my filter is created. But on wildfly-10.1.0.Final, my filter is ignored.
Is there a way to fix it? Is there something I need to add in standalone.xml?
Thanks in advance!

HTTP Status 404 when running simple RESTful web service in Java using Jersey

I am getting HTTP Status 404 when running simple RESTful web service in Java using Jersey. I am following the tutorial REST with Java (JAX-RS) using Jersey. I have copied all the jars that I downloaded from Jersey download site to WEB-INF/lib folder of my project(please see the screenshot for jars).
When I run the application from eclipse development environment Eclipse Console shows that Tomcat was started successfully. My web-app is deployed and I can see index.html coming up. But hitting http://localhost:8080/com.kj.rest.jersey.first/ gives Http Status 404.
My Environment:
Spring Tool Suite as my eclipse dev environment
Jersey 2.22.2 jars
Apache Tomcat v8.0
Please note I am not using Maven in my project and I also looked at other similar questions here but none of them solved my issue.
What am I missing, where should I look for the issue, which logs?
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<!-- Register resources and providers under com.vogella.jersey.first package. -->
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.*******</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
The mistake that I was making was to not specify the service name(specified by #Path annotation) in the URL. After doing that it worked.
So essentially the URL to hit should be http://localhost:8080/com.kj.rest.jersey.first/rest/path_from_rest_class and I missed the path_from_rest_class earlier.

Struts 2 Hello World

I have a problem at this example
I work with eclipse for Java EE and Apache Tomcat 8.
My project structure:
The web.xml code:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Hello World Struts 2</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
The other files code is the same as the struts website.
When I run the index.jsp file I get the following error:
**HTTP Status 404 - /helloworld/index.jsp
type Status report
message /helloworld/index.jsp
description The requested resource is not available.**
Can someone spot the reason why can't I run it?
First of all the project is created by using Maven configuration, and to access the Struts action you should use url
Step 6 - Build the WAR File and Run The Application
Execute mvn clean package to create the war file.
Copy the war file to your Servlet container. After your Servlet
container successfully deploys the war file go to this URL
http://localhost:8080/helloworld/index.action where you should see
the following:
(source: apache.org)
Web application context is where the application was deployed. In the docs url it's /helloworld, on the image it's /Hello_World_Struts2_Ant. Use it as a part of the url. It doesn't matter which app context did you use during deployment but url depends on it. If you want to change the web app context you should read Java - How to change context root of a dynamic web project in Eclipse. After the context you use action name with .action extension to execute action.
Don't use URLs like localhost:8080/helloworld/index.jsp because you might not get the resource because it's already handled by the web server.

Cache control in JBoss 7

In JBoss 5, I have cache control configured in web.xml.
<filter>
<filter-name>CacheControlFilter</filter-name>
<filter-class>org.jboss.web.tomcat.filters.ReplyHeaderFilter</filter-class>
<init-param>
<param-name>Cache-Control</param-name>
<param-value>max-age=3600</param-value>
</init-param>
<filter-mapping>
<filter-name>CacheControlFilter</filter-name>
<url-pattern>/images/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>CacheControlFilter</filter-name>
<url-pattern>*.js</url-pattern>
</filter-mapping>
But when I try to deploy the war to JBoss 7. It failed because the filter is gone. Is there an equivalent class in JBoss 7 to replace org.jboss.web.tomcat.filters.ReplyHeaderFilter?
Thanks
In other word, how cache control is handled in JBoss 7?
I downloaded the jbossweb-service.jar from
http://www.java2s.com/Code/Jar/j/Downloadjbosswebservicejar.htm
But I'm still wondering why it's not in JBoss 7.