Tomcat 404:请求的资源不可用

时间:2014-08-10 10:42:10

标签: java spring tomcat

我正在尝试创建简单的RESTful Web服务。我的war文件构建成功,它在tomcat上启动而没有错误。

但是,当我打电话给某些服务时,例如http://localhost:8080/fxmonitor/service/account/, 我给出错误: 类型状态报告 信息 description请求的资源不可用。

在tomcat日志中: 10.08.2014 14:31:07 org.springframework.web.servlet.DispatcherServlet noHandlerFound 警告:在DispatcherServlet中找不到带有URI [/ fxmonitor / service / account / Hello world1FUUUUU]的HTTP请求的映射,其名称为' FxMonitor'

我的web.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         id="WebApp_ID" version="2.5">

    <display-name>Fx pamm monitor</display-name>
    <description>Some desc</description>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:context.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>FxMonitor</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:app-context.xml</param-value>
        </init-param>
        <!--<load-on-startup>1</load-on-startup>-->
    </servlet>
    <servlet-mapping>
        <servlet-name>FxMonitor</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

</web-app>

APP-context.xml中:

    <?xml version="1.0" encoding="UTF-8"?>
    <!--
        - DispatcherServlet application context for PetClinic's web tier.
    -->
    <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:mvc="http://www.springframework.org/schema/mvc"
           xsi:schemaLocation="http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc.xsd
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd">

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

        <mvc:annotation-driven />
        <!--<mvc:resources mapping="/resources/**" location="/resources/"/>-->

    </beans>

<?xml version="1.0" encoding="UTF-8"?>
<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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="dataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource"
          p:driverClassName="com.mysql.jdbc.Driver"
          p:url="jdbc:mysql://localhost:3306/fxmonitor"
          p:username="root" p:password="0000" />

    <bean id="entityManagerFactory"
          class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="fxpammunit" />
        <property name="dataSource" ref="dataSource" />
        <property name="jpaPropertyMap">
            <map>
                <entry key="eclipselink.weaving" value="false"/>
            </map>
        </property>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
        <property name="dataSource" ref="dataSource" />
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager" />
    <context:annotation-config />
    <context:component-scan base-package="com.pammonitor"/>
    <context:component-scan base-package="com.pammonitor.dao.impl" />

</beans>

的AccountController:

package com.pammonitor.web;

/**
 * Created by user on 10.08.14.
 */

import com.pammonitor.dao.AccountDAO;
import com.pammonitor.entity.AccountEntity;
import com.pammonitor.model.AccountModel;
import org.eclipse.persistence.annotations.ChangeTracking;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;

@Controller
@RequestMapping("/service/account/")
public class AccountController {

    @RequestMapping(method= RequestMethod.GET)
    public String  getAccount(){
        return "Hello world1FUUUUU";
    }

    public AccountDAO getAccountDAO() {
        return accountDAO;
    }

    public void setAccountDAO(AccountDAO accountDAO) {
        this.accountDAO = accountDAO;
    }
}

1 个答案:

答案 0 :(得分:0)

所以我通过向方法添加@ResponseBody来解决了这个问题:

@RequestMapping(method= RequestMethod.GET)
    public @ResponseBody String  getAccount(){
        return "Hello world1FUUUUU";
    }