Maven - Spring RequestMapping 404错误

时间:2015-06-21 20:10:03

标签: java spring hibernate maven spring-mvc

我已经在SO中看到了很多这样的问题,我已经尝试过他们的解决方案,但他们没有为我工作,所以我会发布这个。基本上我只是设置一个项目并尝试让我的RequestMapping工作,只是为了返回一个字符串。所以这是我的代码

的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>ve.com</groupId>
  <artifactId>mavenDWP</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>

<properties>  
  <spring.version>4.1.6.RELEASE</spring.version>  
</properties> 

<dependencies>
  <!--  Spring -->
  <dependency>
<groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>${spring.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>${spring.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>${spring.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>${spring.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-jpa</artifactId>
    <version>1.8.0.RELEASE</version>
</dependency>

<!--  CGLIB is required to process @Configuration classes -->
<dependency>
    <groupId>cglib</groupId>
    <artifactId>cglib</artifactId>
    <version>2.2.2</version>
</dependency>

<!-- Servlet API, JSTL --> 
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>javax.servlet.jsp-api</artifactId>
    <version>2.3.1</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>jstl</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>
<dependency>
    <groupId>cglib</groupId>
    <artifactId>cglib</artifactId>
    <version>2.2.2</version>
</dependency>

<!-- Jackson -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.5.4</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.5.4</version>
</dependency>
<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-core-asl</artifactId>
    <version>1.9.13</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-hibernate4</artifactId>
    <version>2.5.4</version>
</dependency>

<!-- MySQL -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.35</version>
</dependency>

<!-- Hibernate -->
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>4.3.10.Final</version>
</dependency>

<!-- JSONDoc -->
<dependency>
    <groupId>org.jsondoc</groupId>
    <artifactId>jsondoc-core</artifactId>
    <version>1.1.15</version>
</dependency>
    <dependency>
    <groupId>org.jsondoc</groupId>
    <artifactId>jsondoc-springmvc</artifactId>
    <version>1.1.15</version>
</dependency>
    <dependency>
    <groupId>org.jsondoc</groupId>
    <artifactId>jsondoc-ui-webjar</artifactId>
    <version>1.1.15</version>
</dependency>


</dependencies>

<build>
<plugins>
  <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.1</version>
    <configuration>
      <source>1.8</source>
      <target>1.8</target>
    </configuration>
  </plugin>
  <plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.4</version>
  </plugin>
</plugins>

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<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_3_0.xsd" version="3.0">
  <display-name>AngularSpring</display-name>
  <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>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
</web-app>

调度-servlet.xml中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="ve.com" use-default-filters="false">
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>

<mvc:annotation-driven>
    <mvc:message-converters>
        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
            <property name="objectMapper">
                <bean class="ve.com.HibernateAwareObjectMapper" />
            </property>
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>

<bean id="documentationController" class="org.jsondoc.springmvc.controller.JSONDocController">
    <constructor-arg name="version" value="1.0"/>
    <constructor-arg name="basePath" value="http://localhost:4848/mavenDWP/api"/>
    <constructor-arg name="packages">
    <list>
    <value>ve.com</value>
    </list>
    </constructor-arg>
</bean>

LinkController

package ve.com.controller;

import org.jsondoc.core.annotation.ApiMethod;
import org.jsondoc.core.annotation.ApiResponseObject;  
import org.jsondoc.core.annotation.ApiVersion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@Controller
@ApiVersion(since="1.0")
@RequestMapping("/show") 
public class LinkController {

    @Autowired
    private StockService stockService;

    @RequestMapping(method = RequestMethod.GET, value="/hello")
    public String showHello() {
        return "hello";
    }        
}  

好吧基本上这是我的代码..我仍然浏览http://localhost:4848/mavenDWP/api/show/hello,我只是得到404错误。有什么想法吗?

2 个答案:

答案 0 :(得分:1)

添加@ResponseBody注释。每当你返回一些不是类的构建的东西,比如ModelAndView,你需要使用这个注释。见docs

作为建议,即使您要向客户端发送少量数据,也要尝试使用Map。地图将转换为JSON,并且可以轻松加入。

答案 1 :(得分:0)

一切顺利,请尝试使用jsut删除web.xml中 servlet-class 和org.springframework.web.servlet.DispatcherServlet之间的空格

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
祝你好运