首页 文章

将SpringBoot简单Web服务从Tomcat移动到Liberty无法识别 endpoints

提问于
浏览
1

在SpringInitializr的帮助下,我编写了一个简单的Web服务来识别一个 endpoints “/ hello” . 当我在Gradle上构建.war并运行它时,服务在(内置)Tomcat上启动并正确响应URL“localhost:8080 / hello” .

然后我将.war文件复制到Liberty ... / dropins /并启动Liberty服务器;在Liberty的 server.xml 中,默认端口是9080而不是8080.当我点击URL localhost:9080/hello 时,我收到错误 Context Root not Found . 当我点击url localhost:9080/UserSettingController/hello (其中Java项目名称是 UserSettingController 时,我不再得到 Context Root not Found 错误,而是404错误 .

为什么Tomcat会识别 endpoints 但不识别Liberty?我错过了一些Spring-ish连接吗?

该项目的代码是:

@SpringBootApplication
public class UserSettingControllerApplication {

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

////////////////////////////////////////////////////////////////////////////////

public class ServletInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(UserSettingControllerApplication.class);
    }
}

////////////////////////////////////////////////////////////////////////////////

@CrossOrigin
@RestController
public class RESTInterface {

    @RequestMapping(path = "/hello", method = { RequestMethod.GET }, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String hello() {
        return "Hello, dude!";
    }
}

////////////////////////////////////////////////////////////////////////////////

@Configuration
@EnableWebMvc
@EnableConfigurationProperties
@ComponentScan( { "com.ui.usersetting.restinterface" } )

public class UserSettingControllerConfiguration {}

1 回答

  • 0

    要通常调试Liberty问题,可以在 $SERVER_CONFIG_DIR/logs/ 找到日志文件(作为参考, server.xml$SERVER_CONFIG_DIR 内部正确) . 没有更多细节,除了要求您检查日志并报告您看到的错误之外,我无法提供更具体的答案 .

    但是,我确实在.war打包的spring应用程序中试用了你的代码,服务器启动正常,我能够在http://localhost:9080/demo-0.0.1-SNAPSHOT/hello ping hello endpoints . 另外,我使用了Liberty 18.0.0.1(最新的GA版本),所以可能也有帮助 .

    作为参考,这是我使用的server.xml:

    <server description="new server">
    
        <!-- Enable features -->
        <featureManager>
            <feature>jsp-2.3</feature>
            <feature>servlet-3.1</feature>
            <feature>jaxrs-2.0</feature>
        </featureManager>
    
        <!-- To access this server from a remote client add a host attribute to the following element, e.g. host="*" -->
        <httpEndpoint id="defaultHttpEndpoint"
                      httpPort="9080"
                      httpsPort="9443" />
    
        <!-- Automatically expand WAR files and EAR files -->
        <applicationManager autoExpand="true"/>
    </server>
    

    (我从默认的server.xml模板中更改的唯一内容是添加 servlet-3.1jaxrs-2.0 功能)

相关问题