【问题标题】:@RequestMapping Not working in Spring Version 1.5.2.RELEASE@RequestMapping 在 Spring 版本 1.5.2.RELEASE 中不起作用
【发布时间】:2017-04-12 17:31:19
【问题描述】:

我正在尝试运行一个 Spring Boot 项目。当我选择 1.5.2 版时,requestMapping url 不起作用。它总是给我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);
    }
}

注意:问题已更新,包含更多信息。

【问题讨论】:

  • 您要访问的网址是什么?
  • package com.prizy.controller: 你的主班在哪里?
  • 你能把你的主类和包名一起发布吗?
  • 这个类的其他方法有用吗?不应该使用get... 方法名称吗?您是否尝试过更改方法的名称或将/hi 定位到另一个方法?

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


【解决方案1】:

检查 web.xml 文件是否可用到 src>main>webapp>WEB-INF>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>

【讨论】:

  • 他正在使用没有 web.xml 的 Spring Boot。
【解决方案2】:

在尝试了所有场景后,我终于将 Spring boot 版本更改为 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>

更改后,我也可以在日志中看到请求映射 url:

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

请注意,同时我更改了映射 url 名称,因此它可能与问题不同。

【讨论】:

    猜你喜欢
    • 2019-02-05
    • 2012-05-02
    • 2020-07-17
    • 1970-01-01
    • 2020-07-11
    • 1970-01-01
    • 2019-07-28
    • 2021-06-12
    • 1970-01-01
    相关资源
    最近更新 更多