Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Spring Boot中有条件地返回JSON属性中的HTML页面_Spring_Spring Boot - Fatal编程技术网

在Spring Boot中有条件地返回JSON属性中的HTML页面

在Spring Boot中有条件地返回JSON属性中的HTML页面,spring,spring-boot,Spring,Spring Boot,因此,在编写Restful服务的场景中,基于请求数据,我必须返回JSON变量中的短字符串或HTML页面。让我们这样说: response { result : YourRequestedString } 或 响应{ 结果:。。。 } 返回内容的决定权在服务器端 那么,有没有一种方法可以在我使用相同的控制器方法时(直接或通过调用返回呈现页面的某个控制器方法)呈现我的Thymeleaf(或任何其他可能是纯HTML)模板。我可以发送回客户 不鼓励在RESTAPI中返回HTML页面。但如果您

因此,在编写Restful服务的场景中,基于请求数据,我必须返回JSON变量中的短字符串或HTML页面。让我们这样说:

response {
    result : YourRequestedString
}

响应{
结果:。。。
}
返回内容的决定权在服务器端


那么,有没有一种方法可以在我使用相同的控制器方法时(直接或通过调用返回呈现页面的某个控制器方法)呈现我的Thymeleaf(或任何其他可能是纯HTML)模板。我可以发送回客户

不鼓励在RESTAPI中返回HTML页面。但如果您愿意,您可以从控制器返回
响应属性

@GetMapping("/mymethod")
public ResponseEntity<Response> myMethod() {
    ResponseEntity responseEntity = null;

    if(string) {
        responseEntity = new ResponseEntity(getString(), HttpStatus.OK);
    } else {
        responseEntity = new ResponseEntity(getHtml(), HttpStatus.OK);
    }

    return responseEntity;
}
@GetMapping(“/mymethod”)
公众反应我的方法(){
ResponseEntity ResponseEntity=null;
如果(字符串){
responseEntity=新的responseEntity(getString(),HttpStatus.OK);
}否则{
responseEntity=新的responseEntity(getHtml(),HttpStatus.OK);
}
返回响应性;
}

多亏@Leffchik,我才让它工作起来,这是我使用Thymeleaf的设置,它可以帮助其他人

设置htmlTemplateEngine

@Bean
public TemplateEngine htmlTemplateEngine() {
final SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.addTemplateResolver(htmlTemplateResolver());

return templateEngine;
}

private ITemplateResolver htmlTemplateResolver() {
final ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
templateResolver.setResolvablePatterns(Collections.singleton("html/*"));
templateResolver.setPrefix("/templates/");
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode(TemplateMode.HTML);
templateResolver.setCharacterEncoding("utf-8");
templateResolver.setCacheable(false);

return templateResolver;
}
这是src/main/resources/templates/html/hello.html中的html页面

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Getting Started: Serving Web Content</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <p th:text="'Hello, ' + ${name} + '!'" />
</body>
</html>

入门:提供Web内容

RestController是这样运行的

@RestController
public class TestController {

@Autowired
TemplateEngine htmlTemplateEngine;

@RequestMapping("/testHello")
public ResponseEntity<?> test(@RequestParam(value = "name", required = false, defaultValue = "World") String name) {
final org.thymeleaf.context.Context ctx = new org.thymeleaf.context.Context();

ctx.setVariable("name", name);

// Rendered template in String, You can now return in a JSON property
final String htmlContent = this.htmlTemplateEngine.process("html/hello.html", ctx);

return ResponseEntity.ok().body(htmlContent);
}

}
@RestController
公共类测试控制器{
@自动连线
模板引擎htmlTemplateEngine;
@请求映射(“/testHello”)
公共响应性测试(@RequestParam(value=“name”,required=false,defaultValue=“World”)字符串名){
final org.thymeleaf.context.context ctx=new org.thymeleaf.context.context();
ctx.setVariable(“名称”,名称);
//以字符串形式呈现的模板,现在可以返回JSON属性
最后一个字符串htmlContent=this.htmlTemplateEngine.process(“html/hello.html”,ctx);
返回ResponseEntity.ok().body(htmlContent);
}
}

希望有帮助

我已经在使用ResponseEntity,如何让getHTML()方法返回静态/模板文件夹中呈现的HTML模板。@IbrahimFaiq,检查。它显示了如何手动将html模板呈现为字符串。
@RestController
public class TestController {

@Autowired
TemplateEngine htmlTemplateEngine;

@RequestMapping("/testHello")
public ResponseEntity<?> test(@RequestParam(value = "name", required = false, defaultValue = "World") String name) {
final org.thymeleaf.context.Context ctx = new org.thymeleaf.context.Context();

ctx.setVariable("name", name);

// Rendered template in String, You can now return in a JSON property
final String htmlContent = this.htmlTemplateEngine.process("html/hello.html", ctx);

return ResponseEntity.ok().body(htmlContent);
}

}