【问题标题】:Spring Boot RESTful Web ServiceSpring Boot RESTful Web 服务
【发布时间】:2018-10-25 10:12:32
【问题描述】:

我正在使用 eclipse 和 spring 框架按照本教程 Spring Boot RESTful Web Service Tutorial - Java REST API 制作一个简单的 Rest 服务。在我的台式电脑上一切正常,但后来我尝试复制项目 并在笔记本电脑上用相同版本的eclipse打开...

问题是当我启动应用程序时,我无法打开 localhost:8080,并且我没有得到“Whitelabel Error Page”.... 只是 404。当我尝试调用 rest 控制器方法时也是如此。

当我查看控制台时,在电脑上我看到了这个:

但我在笔记本电脑上的日志略有不同:

我也会在这里发布课程,所以任何人都可以告诉我可能是什么原因?

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

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

    }

package com.start.contract.controllers;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.start.contract.datamodel.Contract;

@RestController
@RequestMapping(ContractController.CONTRACT_BASE_URI)
public class ContractController {

    public static final String CONTRACT_BASE_URI = "svc/v1/contracts";

    @RequestMapping(value = "{contractNumber}")
    public Contract getContract(@PathVariable final int contractNumber)
    {

        Contract contract = new Contract();
        contract.setName("Djomla");
        contract.setId(contractNumber);
        return contract;
    }

}

package com.start.contract.datamodel;

public class Contract {

    private String name;
    private int id;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("Contract [name=").append(name).append(", id=").append(id).append("]");
        return builder.toString();
    }

}

【问题讨论】:

  • 所以你是说日志告诉你tomcat从8080开始,浏览器说它无法连接到localhost:8080??
  • 查看您笔记本电脑的日志,它在启动时没有创建映射,所以您收到 404。很奇怪的问题。

标签: java spring rest tomcat request


【解决方案1】:

在 manu 之后,对我有用的是更改 pom.xml 设置, 我将 Spring Boot 版本降低到

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.13.RELEASE</version>
</parent>

而不是

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.2.RELEASE</version>
</parent>

...现在它也可以在我的笔记本电脑上使用...我只是仍然不知道这会是什么问题吗?

【讨论】:

    【解决方案2】:

    将此添加到 application.properties 文件中。

    server.error.whitelabel.enabled=true
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-24
      • 1970-01-01
      • 2016-10-21
      • 2019-05-28
      • 1970-01-01
      • 2015-03-05
      相关资源
      最近更新 更多