【问题标题】:Is there any way to return HTML page in response using spring boot rest controller?有没有办法使用spring boot rest控制器返回HTML页面作为响应?
【发布时间】:2021-05-25 17:34:35
【问题描述】:

我将 verify_success 作为字符串作为响应。

请仅提供@RestController的答案。

@RestController    
// here is method call. verify_success,verify_fail are html pages 
@PreAuthorize("permitAll()")
@Operation(summary = "email_Verification", tags = { "auth" })
@RequestMapping(value = "/verify", method = RequestMethod.GET)
public String verifyUser(@Param("code") String code) {
    if (emailService.verify(code)) {
        return "verify_success";
        //i want html response instead of string 
    } else {
        return "verify_fail";
        //i want html response instead of string 
    }
}

【问题讨论】:

  • 当您返回 html 而不是字符串时会发生什么?

标签: java html spring spring-boot spring-restcontroller


【解决方案1】:
@Controller
public class ControllerClass {

@PreAuthorize("permitAll()")
@Operation(summary = "email_Verification", tags = { "auth" })
@RequestMapping(value = "/verify", method = RequestMethod.GET)
public ModelAndView verifyUser() {
     if (emailService.verify(code)) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("verify_success.html");
        return modelAndView;
            } else {
ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("verify_fail.html");
        return modelAndView;
            }
        
    }
}## replace rest controler with controler ##

【讨论】:

  • 他严格要求没有 RestController 的解决方案。
猜你喜欢
  • 2020-09-05
  • 1970-01-01
  • 2022-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-17
  • 2021-02-17
  • 1970-01-01
相关资源
最近更新 更多