【问题标题】:Spring boot - configure context-pathSpring Boot - 配置上下文路径
【发布时间】:2018-09-04 09:13:48
【问题描述】:

我已经按照here 的解释配置了application.properties‬

server.servlet.context-path=/test

在我的@RestController 中,我有一个简单的@RequestMapping

@RequestMapping(value = "/products", method = RequestMethod.GET)

但是在向http://localhost:8080/test/products 发送请求时,我仍然收到 tomcat - 404 响应。

完整代码:

package com.siemens;

import java.util.HashMap;
import java.util.Map;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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;


@RestController
public class ProductServiceController {
   private static Map<String, Product> productRepo = new HashMap<>();
   static {
      Product honey = new Product();
      honey.setId("1");
      honey.setName("Honey");
      productRepo.put(honey.getId(), honey);

      Product almond = new Product();
      almond.setId("2");
      almond.setName("Almond");
      productRepo.put(almond.getId(), almond);
   }



   @RequestMapping(value = "/products", method = RequestMethod.GET)
   public ResponseEntity<Object> getProduct() {
      return new ResponseEntity<>(productRepo.values(), HttpStatus.OK);
   }
}

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.example</groupId>
    <artifactId>siemens</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>siemens</name>
    <description>Demo project for siemens</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.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.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

@SpringBootApplication:

@SpringBootApplication
public class SiemensApplication {

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

在tomcat 9上运行,如果需要更多代码或配置,请在下方评论,谢谢。

【问题讨论】:

  • 需要在ProductServiceController上添加@RequestMapping("/")
  • remove remove server.servlet.context-path=/test from application.properties
  • 请粘贴:@SpringBootApplication 文件
  • 添加@SpringBootApplication 文件。

标签: java spring-boot tomcat9


【解决方案1】:

任何请求都应该“知道”如何到达这个控制器。我建议试试这个:

@RestController
@RequestMapping("psc")
public class ProductServiceController {


    @RequestMapping(value = "products", method = RequestMethod.GET)
    public ResponseEntity<Object> getProduct() {
    return new ResponseEntity<>(productRepo.values(), HttpStatus.OK);
    }

这样您就可以向以下网址发送 GET 请求: http://localhost:8080/test/psc/products

  1. 添加@RequestMapping注解来指定这个类(控制器)的路径
  2. 去掉每个 @RequestMapping 路径值上的“/”(只是一个装饰性建议)

【讨论】:

  • 为什么将@RestController 改为Controller?他只需要查看@RequestMapping
  • 我在旧版本中遇到了一些问题,其中 (@)RequestMapping 和 (@)RestController 会导致歧义,因为 (@)RestController 应该由系统隐式配置,而 (@)RequestMapping 的路径是显式配置的。 (@)Controller 解决了这个问题。
  • @RestController = @Controller + @ResponseBody 所以没有任何隐式系统配置。
  • 是的,它确实可以这样正常工作。我已经编辑了我原来的答案。
猜你喜欢
  • 2016-11-15
  • 1970-01-01
  • 2019-08-22
  • 2018-08-17
  • 2018-03-06
  • 1970-01-01
  • 2014-10-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多