猿问

添加自定义 @Controller 到 Maven 覆盖?

我正在研究mitreid-connect/OpenID-Connect-Java-Spring-Server的实现,它建议您使用 Maven Overlay 扩展和自定义项目。我已按照说明进行操作,然后将以下代码添加到webapp-overlay/src/main/java/controller/HealthcheckController.java


package controller;


import org.springframework.context.annotation.Primary;

import org.springframework.http.MediaType;

import org.springframework.stereotype.Controller;

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

import org.springframework.web.bind.annotation.RequestMethod;


@Controller

@Primary

public class HealthcheckController {


    @RequestMapping(value = "/healthcheckwebapp", method = RequestMethod.GET, produces = MediaType.TEXT_PLAIN_VALUE)

    public String getHealthCheck() {

        return "200 OK";

    }

}

但是,我在实际尝试到达路线时收到 404。难道我做错了什么?




喵喔喔
浏览 231回答 3
3回答

守着一只汪

在这种情况下你只需要将 @Controller 更改为 @RestController 在这种情况下你不需要响应主体,就像这样。import org.springframework.context.annotation.Primary;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@Primarypublic class Rest {    @RequestMapping("/healthcheckwebapp")    public String getHealthCheck() {        return "200 OK";    }}

慕仙森

在您的覆盖服务器-config.xml 上添加此 bean<context:component-scan base-package="controller">&nbsp; &nbsp; <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller.HealthcheckController " /></context:component-scan>并添加@ResponseBody注释getHealthCheck()

喵喵时光机

它发送一个 404,因为它正在寻找一个名为“200 ok”的模板,如果您想给出答案,请使用 @ResponseStatus(HttpStatus.OK) 并使用 return 重定向到另一个地方,如 return“/index”;为了避免 404 错误我希望这会有所帮助因为我没有完全理解你的问题
随时随地看视频慕课网APP

相关分类

Java
我要回答