【问题标题】:Deploy Spring MVC 3 Rest on WebSphere Application Server 8在 WebSphere Application Server 8 上部署 Spring MVC 3 Rest
【发布时间】:2015-12-21 09:09:04
【问题描述】:

我正在尝试在 WebSphere Application Server 8.0 上部署 Spring 3 MVC Rest Application,但没有成功。

这是我的控制器类

`

@Controller
public class BookingService {

    public BookingService() {
        System.out.println("test");
    }

    @RequestMapping(value="/test", method=RequestMethod.GET, produces="application/xml")
    @ResponseBody
    public String test() {
        return "<test>test</test>";
    }
}

`

web.xml - servlet 规范 3

`

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.0"
    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_3_0.xsd">

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/api/*</url-pattern>
    </servlet-mapping>

</web-app>

`

调度程序-servlet.xml

`

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <mvc:annotation-driven />

    <context:component-scan base-package="com.example" />

    <!-- <tx:annotation-driven /> -->

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> 

<bean id="viewResolver" class=
"org.springframework.web.servlet.view.BeanNameViewResolver" />

    <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
            <property name="mediaTypes">
                <map>
                    <entry key="html" value="text/html" />
                    <entry key="json" value="application/json" />
                    <entry key="json" value="application/xml" />
                </map>
            </property>
            <property name="viewResolvers">
                <list>
                    <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
                        <property name="viewClass"
                            value="org.springframework.web.servlet.view.JstlView" />
                        <property name="prefix" value="/WEB-INF/jsp/" />
                        <property name="suffix" value=".jsp" />
                    </bean>
                </list>
            </property>
            <property name="defaultViews">
                <list>
                    <bean
                        class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
                        <property name="prefixJson" value="true" />
                    </bean>
                </list>
            </property>
    </bean>

</beans>

`

我确认它确实适用于 tomcat。在 tomcat 日志中,正在创建映射。

INFO: Mapped "{[/test],methods=[GET],params=[],headers=[],consumes=[],produces=[application/xml],custom=[]}" onto public java.lang.String com.westjet.ens.services.BookingService.test()

http://localhost:8080/ens-das-web/api/test 执行 curl 会返回 HTTP 200。

但是在 was 日志中,这不会出现,并且执行 curl http://localhost:9080/my-service-web/api/test 返回 404 和以下响应。

` 错误 404:SRVE0295E:报告错误:404

`

在进行 curl 时,WAS 日志也会显示以下内容。

[9/23/15 15:53:21:390 MDT] 00000089 PageNotFound W org.springframework.web.servlet.DispatcherServlet noHandlerFound No mapping found for HTTP request with URI [/ens-das-web/api/test] in DispatcherServlet with name 'dispatcher'

WAS 日志:

[9/23/15 15:20:52:809 MDT] 00000049 WASSessionCor I SessionContextRegistry getSessionContext SESN0176I: Will create a new session context for application key default_host/ens-das-web [9/23/15 15:20:58:993 MDT] 00000049 webapp I com.ibm.ws.webcontainer.webapp.WebApp log SRVE0292I: Servlet Message - [ens-das-web-0_1_war#ens-das-web-0.1.war]:.No Spring WebApplicationInitializer types detected on classpath [9/23/15 15:20:59:176 MDT] 00000049 webapp I com.ibm.ws.webcontainer.webapp.WebApp log SRVE0292I: Servlet Message - [ens-das-web-0_1_war#ens-das-web-0.1.war]:.Initializing Spring FrameworkServlet 'dispatcher' [9/23/15 15:20:59:178 MDT] 00000049 DispatcherSer I org.springframework.web.servlet.FrameworkServlet initServletBean FrameworkServlet 'dispatcher': initialization started [9/23/15 15:20:59:229 MDT] 00000049 XmlWebApplica I org.springframework.context.support.AbstractApplicationContext prepareRefresh Refreshing WebApplicationContext for namespace 'dispatcher-servlet': startup date [Wed Sep 23 15:20:59 MDT 2015]; root of context hierarchy [9/23/15 15:20:59:313 MDT] 00000049 XmlBeanDefini I org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions Loading XML bean definitions from ServletContext resource [/WEB-INF/dispatcher-servlet.xml] [9/23/15 15:20:59:749 MDT] 00000049 AutowiredAnno I org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor <init> JSR-330 'javax.inject.Inject' annotation found and supported for autowiring [9/23/15 15:20:59:770 MDT] 00000049 DefaultListab I org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@3c2306c: defining beans [mvcContentNegotiationManager,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0,org.springframework.format.support.FormattingConversionServiceFactoryBean#0,org.springframework.validation.beanvalidation.LocalValidatorFactoryBean#0,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#0,org.springframework.web.servlet.handler.MappedInterceptor#0,org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver#0,org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver#0,org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver#0,org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0,org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter#0,viewResolver,org.springframework.web.servlet.view.ContentNegotiatingViewResolver#0,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy [9/23/15 15:21:00:615 MDT] 00000049 DispatcherSer I org.springframework.web.servlet.FrameworkServlet initServletBean FrameworkServlet 'dispatcher': initialization completed in 1436 ms [9/23/15 15:21:00:615 MDT] 00000049 servlet I com.ibm.ws.webcontainer.servlet.ServletWrapper init SRVE0242I: [ens-das-web-0_1_war] [/ens-das-web] [dispatcher]: Initialization successful. [9/23/15 15:21:00:616 MDT] 00000049 webcontainer I com.ibm.ws.webcontainer.VirtualHostImpl addWebApplication SRVE0250I: Web Module ENS Data Access Service has been bound to default_host[*:9080,*:80,*:9443,*:5060,*:5061,*:443].

任何帮助将不胜感激。

【问题讨论】:

  • WebSphere - 太糟糕了。只能假设是一些晦涩难懂的配置或部署问题导致您的应用无法运行。
  • 是的,不幸的是,生产是 WAS。
  • 哼,这很奇怪。我已经在我的 WAS 8.0 上尝试了您的设置,并且一切正常,控制器 BookingService 是否在包“com.example”中?
  • 是的,它在同一个包中。 Rest Service 在 Tomcat 中工作。只是 @RequestMapping 端点在 WAS 中不起作用。
  • 看来我的问题是我正在部署 WAR 文件。一旦我将 WAR 文件打包到 EAR 中,它就可以工作了。奇怪...

标签: java spring spring-mvc tomcat websphere


【解决方案1】:

我能够解决这个问题。由于某种原因,maven 没有清理 WEB-INF/classes 文件夹,并且我的 @Controller 类不可用。

【讨论】:

    猜你喜欢
    • 2011-10-04
    • 2012-01-09
    • 1970-01-01
    • 2012-05-25
    • 1970-01-01
    • 2018-08-18
    • 2018-09-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多