@RequestMapping在Spring Version 1.5.2.RELEASE中不起作用

时间:2017-04-12 17:31:19

标签: java spring maven spring-mvc spring-boot

我正在尝试运行一个spring boot项目。当我选择版本1.5.2时,requestMapping网址不起作用。它总是给我404错误。

有人可以确认我是否缺少任何标签或版本中的任何设置更改。或者我错过的任何其他变化。

的pom.xml

 <?xml version="1.0" encoding="UTF-8"?>
<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>com.prizy</groupId>
    <artifactId>ProductApi</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>ProductApi</name>
    <description>Project for Product Managment</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.derby</groupId>
            <artifactId>derby</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

控制器:

package com.prizy.controller;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.prizy.model.Product;
import com.prizy.service.ProductService;

@RestController
public class ProductController {

    @Autowired
    private ProductService productService;

    @RequestMapping("/products")
    public List<Product> getAllProducts() {
        return productService.getAllProducts();
    }

    @RequestMapping("/hi")
    public String getString() {
        return "Hi";
    }

    @RequestMapping("/products/{productId}")
    public Product getProduct(@PathVariable int productId){     
        return productService.getTopic(productId);
    }

    @RequestMapping(method=RequestMethod.POST, value="/products")
    public void addProduct(@RequestBody Product product) {
        productService.addProduct(product);
    }

    @RequestMapping(method=RequestMethod.PUT, value="/products/{productId}")
    public void updateProduct(@RequestBody Product product, @PathVariable int productId) {
        productService.updateProduct(product,productId);
    }

    @RequestMapping(method=RequestMethod.DELETE, value="/products/{productId}")
    public void deleteProduct(@PathVariable int productId) {
        productService.deleteProduct(productId);
    }

}

主类是

package com.prizy;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@EnableAutoConfiguration
@SpringBootApplication
public class ProductApiApplication {

    public static void main(String[] args) {
        SpringApplication.run(ProductApiApplication.class, args);
    }
}

Project Structure

注意:问题已更新,但有更多信息。

2 个答案:

答案 0 :(得分:1)

检查web.xml文件是否可用到src&gt; main&gt; webapp&gt; WEB-INF&gt; web.xml。如果没有可用的文件,请使用以下代码。

 <servlet>
    <servlet-name>myServlet</servlet-name>
 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:ApplicationContext.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

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

<filter>
    <filter-name>encoding-filter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>encoding-filter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<welcome-file-list>
    <welcome-file>/index.jsp</welcome-file>
</welcome-file-list>

答案 1 :(得分:0)

在尝试完所有场景后,我终于将Spring启动版本更改为1.3.2.RELEASE

这样做之后它的工作正常。 我仍然试图得到答案,为什么它不支持1.5.2版本。

但是要解决此错误,我在下面更改了我的POM.xml文件

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

在此更改之后,我也可以在日志中看到请求映射网址:

2017-04-19 11:10:49.785  INFO 4236 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello]}" onto public java.lang.String com.epic.controllers.TestController.sayHello()
2017-04-19 11:10:49.786  INFO 4236 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/helloss]}" onto public java.lang.String com.epic.TestControllerTest.sayHello()
2017-04-19 11:10:49.789  INFO 4236 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2

请注意,同时我更改了地图网址名称,因此可能与问题不同。