spring - How to map a SpringServlet to root and also serve static content in root? -
i have jax-rs spring service i've added swagger to. swagger builds json description of service has served static file. i've included swagger ui view json description nicely. issue when mapping servlet path root not serve static swagger files under /swagger. service works fine. both work if map service path that's not root such /rest/*. i'd rather have nice path without "/rest/" in middle.
so when have <url-pattern>/</url-pattern>
service @ root <servicename>/
, going <servicename>/swagger/
returns 404 not found. if change <url-pattern>/rest/*</url-pattern>
service working @ <servicename>/rest/
, swagger visible @ <servicename>/swagger
. possible have work without needing service under <servicename>/rest/*
?
i'm on spring 3.0.7-release , jersey 1.8. edit: also, running on weblogic 10.3.6.0.
web.xml:
<?xml version="1.0" encoding="utf-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name>data access service</display-name> <listener> <listener-class>com.my.startup</listener-class> </listener> <listener> <listener-class>org.springframework.web.context.contextloaderlistener</listener-class> </listener> <context-param> <param-name>contextconfiglocation</param-name> <param-value>classpath:application-context.xml</param-value> </context-param> <context-param> <param-name>local-deployment</param-name> <param-value>true</param-value> </context-param> <servlet> <servlet-name>jersey-servlet</servlet-name> <servlet-class> com.sun.jersey.spi.spring.container.servlet.springservlet</servlet-class> <init-param> <param-name>com.sun.jersey.config.property.packages</param-name> <param-value>com.my.rs</param-value> </init-param> <init-param> <param-name>com.sun.jersey.api.json.pojomappingfeature</param-name> <param-value>true</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>jersey-servlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
application-context.xml:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <mvc:annotation-driven /> <mvc:resources mapping="/swagger/**" location="/swagger/" /> <!--context:annotation-config /--> <context:component-scan base-package="com.my" /> <tx:annotation-driven /> <bean id="dasdatasource" class="org.springframework.jndi.jndiobjectfactorybean"> <property name="jndiname" value="jdbc/dasexampledatasource" /> </bean> <bean id="entitymanagerfactory" class="org.springframework.orm.jpa.localcontainerentitymanagerfactorybean"> <property name="datasource" ref="dasdatasource" /> <property name="jpavendoradapter" ref="jpavendoradapter" /> <property name="jpadialect" ref="jpadialect" /> <property name="persistenceunitname" value="dasexampledatasource" /> <property name="loadtimeweaver"> <bean class="org.springframework.instrument.classloading.weblogic.weblogicloadtimeweaver" /> </property> </bean> <bean class="org.springframework.orm.jpa.support.persistenceannotationbeanpostprocessor" /> <bean id="jpavendoradapter" class="org.springframework.orm.jpa.vendor.eclipselinkjpavendoradapter" /> <bean id="jpadialect" class="org.springframework.orm.jpa.vendor.eclipselinkjpadialect" /> <bean id="transactionmanager" class="org.springframework.transaction.jta.jtatransactionmanager"/> <bean id="apilistingresourcejson" class="com.wordnik.swagger.jersey.listing.apilistingresourcejson" /> <bean id="apideclarationprovider" class="com.wordnik.swagger.jersey.listing.jerseyapideclarationprovider" scope="singleton" /> <bean id="resourcelistingprovider" class="com.wordnik.swagger.jersey.listing.jerseyresourcelistingprovider" scope="singleton" /> <bean id="beanconfig" class="com.wordnik.swagger.jaxrs.config.beanconfig"> <property name="title" value="swagger sample app"/> <property name="version" value="1.0.0" /> <property name="basepath" value="http://localhost:8001/myservice/v1"/> <property name="resourcepackage" value="com.my.rs,com.my.common.rs"/> <property name="scan" value="true"/> </bean> </beans>
i accomplished using filter definitions rather servlet definitions. additionally, there bug in jersey 1.8 caused mount point require trailing forward slash (https://java.net/jira/browse/jersey-1410) updated jersey 1.19. plan further update 2.x spring 4.x when features of project stable.
here web.xml allowed me have swagger ui view within same root directory service:
<?xml version="1.0" encoding="utf-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name>data access service</display-name> <listener> <listener-class>com.my.startup</listener-class> </listener> <listener> <listener-class>org.springframework.web.context.contextloaderlistener</listener-class> </listener> <context-param> <param-name>contextconfiglocation</param-name> <param-value>classpath:application-context.xml</param-value> </context-param> <context-param> <param-name>local-deployment</param-name> <param-value>true</param-value> </context-param> <filter> <filter-name>jersey</filter-name> <filter-class>com.sun.jersey.spi.spring.container.servlet.springservlet</filter-class> <init-param> <param-name>com.sun.jersey.config.property.packages</param-name> <param-value>com.my.rs</param-value> </init-param> <init-param> <param-name>com.sun.jersey.config.feature.redirect</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>com.sun.jersey.config.feature.filterforwardon404</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>com.sun.jersey.api.json.pojomappingfeature</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>jersey</filter-name> <url-pattern></url-pattern> </filter-mapping>
Comments
Post a Comment