Servlet mapping for specific URL not working in Jersey 2 - java

I am trying to use Jersey 2.x and have a servlet call "myapp", configuration on web.xml is as follows:
<servlet>
<servlet-name>myapp</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>
com.private.myapp.resource
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
and have a servlet mapping as follows
<servlet-mapping>
<servlet-name>myapp</servlet-name>
<url-pattern>/instance/create</url-pattern>
<url-pattern>/instance/list</url-pattern>
</servlet-mapping>
when I request to $SERVER_ROOT/instance/create or $SERVER_ROOT/instance/list its return 404
but when I change servlet mapping as follows
<servlet-mapping>
<servlet-name>myapp</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
then requesting to $SERVER_ROOT/instance/create or $SERVER_ROOT/instance/list response as expected
can anyone tell what I am missing?

Related

Jersey with liferay, Servlet to portlet conversion

I have a webservlet with Jersey rest API configured. Now I have to convert the servlet to a liferay portlet. How to convert? Like what portlet-class should I specify in my portlet.xml? The below is the web.xml of my servlet.
<servlet>
<servlet-name>charts</servlet-name>
<!--<servlet-class>javax.servlet.http.HttpServlet</servlet-class>-->
<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.charts.api.service</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>charts</servlet-name>
<url-pattern>/charts</url-pattern>
<url-pattern>/charts/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
How to configure my portlet.xml and use rest service with my portal? I have to deploy the portlet in liferay jboss server as well.
Why don't you use a delegate servlet in liferay?
You can create a liferay portlet and in web.xml define your delegate servlet.
Here you've got a definition example:
<servlet>
<servlet-name>buscador</servlet-name>
<servlet-class>com.liferay.portal.kernel.servlet.PortalDelegateServlet</servlet-class>
<init-param>
<param-name>servlet-class</param-name>
<param-value>com.dummy.servlet.BuscadorServlet</param-value>
</init-param>
<init-param>
<param-name>sub-context</param-name>
<param-value>buscador</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
It will listens on http://yourliferay/delegate/buscador
Hope it helps

How does Java servlet handle a URL that ends with '/'?

Usually, these two requests:
localhost:8080/test
localhost:8080/test/
have no difference.
However, when I add a servlet-mapping config:
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
then
localhost:8080/test/
Does not work, it would return 404 error, I don't understand that I had added a config like this:
<servlet-mapping>
<servlet-name>rest</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>rest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/rest-servlet.xml,
/WEB-INF/interceptor-servlet.xml,
/WEB-INF/controller-servlet.xml,
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
So, why would the request not go to this servlet, what is the magic in *.html? The others like *.txt, *.jpeg would not cause this problem.
remove
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
leave
<servlet-mapping>
<servlet-name>rest</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
and let Spring distinguish between rest and html requests.

Add multiple endpoint / service classes using apache cxf

I am trying to implement rest webservice using apache cxf (non-spring). I have configured my web.xml and added one end-point address, it works fine but now i want to add one more end-point address or one more service class and I am unable to do it because the second one overrides the first one.
My web.xml like this
<servlet>
<display-name>CXFNonSpringJaxrsServlet</display-name>
<servlet-name>CXFNonSpringJaxrsServlet</servlet-name>
<servlet-class>org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet</servlet-class>
<init-param>
<param-name>jaxrs.serviceClasses</param-name>
<param-value>abc</param-value>
</init-param>
<init-param>
<param-name>jaxrs.address</param-name>
<param-value>/abc</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<display-name>CXFNonSpringJaxrsServlet</display-name>
<servlet-name>CXFNonSpringJaxrsServlet</servlet-name>
<servlet-class>org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet</servlet-class>
<init-param>
<param-name>jaxrs.serviceClasses</param-name>
<param-value>xyz</param-value>
</init-param>
<init-param>
<param-name>jaxrs.address</param-name>
<param-value>/xyz</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
You can do like this to have multiple endpoints:
web.xml
<servlet>
<servlet-name>s1</servlet-name>
<servlet-class>
org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet
</servlet-class>
<init-param>
<param-name>jaxrs.serviceClasses</param-name>
<!-- Multiple resource classes separated with space -->
<param-value>
com.gsdev.Resource1 com.gsdev.Resource2
com.ttdev.bs.BookSelectionsResource
</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>s1</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
Resource classes will be like:
#Path("endpoint1/")
public class Resource1
#Path("endpoint2/")
public class Resource2
Now you have different endpoints as
http://host:port/webapp/services/endpoint1/
http://host:port/webapp/services/endpoint2/

weblogic server not serving html, js and css files

I have deployed a sample spring java application to weblogic server (11g, 10.3.6). I have index.html at the root and I set that as the welcome-file in web.xml. But when I try to access the application, I am getting 'Error 404 -- Not Found'. Also, I noticed same issue with js and css files.
index.jsp works fine at the same location.
Here is my web.xml.
<display-name>hello</display-name>
<welcome-file-list>
<welcome-file>/index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
The reason for this is the root level mapping(/) for your Spring DispatcherServlet. All the requests are forward to Spring servlet:
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
I workaround this situation by using *.do for my Spring controllers and update the servlet mapping as mentioned here:
<servlet-mapping>
<servlet-name>appServlet/servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
But this will require you to update the 'calls to your Spring controller' with a .do at the end of the URLs.

Jetty Proxy Servlet returns a 403 error

I have setup my application like this using jetty
Web.xml
<servlet>
<servlet-name>GoogleProxy</servlet-name>
<servlet-class>org.mortbay.proxy.AsyncProxyServlet$Transparent</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<param-name>ProxyTo</param-name><param-value>http://www.google.com</param-value>
</init-param>
<init-param>
<param-name>Prefix</param-name><param-value>/google</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>GoogleProxy</servlet-name>
<url-pattern>/google/*</url-pattern>
</servlet-mapping>
However when I access my app (The url is like this localhost:8080/myapp/google/images) it returns an error 403. I am using jetty 6.1.13

Categories