如何将SpringServlet映射到root并在root中提供静态内容?

时间:2015-06-18 21:43:56

标签: spring rest servlets static

我有一个JAX-RS弹簧服务,我已经添加了swagger。 Swagger构建了一个必须作为静态文件服务的服务的json描述。我还包括swagger ui很好地查看json描述。我的问题是,当将servlet路径映射到root时,它将不会提供/ swagger下的静态swagger文件。服务工作得很好。如果我将服务映射到非root的路径(如/ rest / *),则两者都可以工作。我宁愿在中间没有“/ rest /”的好道路。

因此,当我<url-pattern>/</url-pattern>时,服务位于根<servicename>/并且转到<servicename>/swagger/,则返回404未找到。如果我更改为<url-pattern>/rest/*</url-pattern>,则该服务正在<servicename>/rest/处工作,并且<servicename>/swagger处可以看到招摇。是否可以让这项工作不需要服务在<servicename>/rest/*下?

我在Spring 3.0.7-RELEASE和JERSEY 1.8上。编辑:此外,这是在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>

应用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>

1 个答案:

答案 0 :(得分:0)

我通过使用过滤器定义而不是servlet定义来实现这一点。此外,JERSEY 1.8中存在一个错误,导致我的挂载点需要一个正斜杠(https://java.net/jira/browse/JERSEY-1410),因此我更新为JERSEY 1.19。当项目的功能稳定时,我计划进一步更新到2.x以及Spring 4.x.

这是web.xml,它允许我在与我的服务相同的根目录中拥有一个swagger ui视图:

<?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>