Spring Boot HATEOAS 在 Tomcat 上部署时链接错误 [英] Spring Boot HATEOAS links wrong when deployed on Tomcat

查看:39
本文介绍了Spring Boot HATEOAS 在 Tomcat 上部署时链接错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 Spring Boot 为 AngularJS 应用程序开发了一个 REST API.我正在运行 Java 8 并部署到 Ubuntu 上的 Tomcat 8,运行 Oracle 的 JDK8.当我从 Eclipse 中运行时,或者如果我将应用程序作为 JAR 运行时,一切正常.如果我点击 localhost:9000/api 我会得到如下输出:

I've developed a REST API for an AngularJS application using Spring Boot. I'm running Java 8 and deploying to Tomcat 8 on Ubuntu, running Oracle's JDK8. Everything works just fine when I run from within Eclipse, or if I run the application as a JAR. If I hit localhost:9000/api I'll get output such as:

{
    "expenses" : {
        "href" : "http://localhost:9000/api/expenses"
    },
    "expenseTypess" : {
        "href" : "http://localhost:9000/api/expensetypes"
    },
    ....
}

如果我将应用程序打包为 WAR 并将其部署在 Tomcat 上的/myapplication,我将丢失应用程序根目录后的尾部斜杠.调用 192.168.1.170:8080/myapplication/api 返回:

If I package up the application as a WAR and deploy it on Tomcat at /myapplication, I lose the trailing slash after the application root. Calls to 192.168.1.170:8080/myapplication/api returns:

{
    "expenses" : {
        "href" : "http://192.168.1.170:8080/myapplicationapi/expenses"
    },
    "expenseTypess" : {
        "href" : "http://192.168.1.170:8080/myapplicationapi/expensetypes"
    },
    ....
}    

...这显然会导致一切爆炸,因为没有一个 API 链接是正确的.如果我手动调用正确的 URI,那么返回的资源也有损坏的链接.

...which obviously causes everything to explode since none of the API links are correct. If I manually invoke the correct URI, then the returned resources also have corrupted links.

有人看到并克服了吗?

推荐答案

嗯,答案很简单.太简单了,我都懒得去想了.我在 application.properties 中为 API 定义了一个基本 URI:

Well, the answer was really simple. So simple, I neglected to even think about it. I had defined a base URI for the API in application.properties as:

spring.data.rest.base-uri=api

更改为:

spring.data.rest.base-uri=/api

解决了问题:/

这篇关于Spring Boot HATEOAS 在 Tomcat 上部署时链接错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆