java - 返回包含信息的 ResponseEntity

标签 java spring spring-boot

我对 Spring 有点陌生。我做了以下方法:

public ResponseEntity<Borders> updateBorder(@Valid @RequestBody Borders borders) throws URISyntaxException {
    log.debug("REST request to update Borders : {}", borders);
    Boolean ok = deviceService.updateBorder(borders);
    return new ResponseEntity(ok ? HttpStatus.CREATED : HttpStatus.BAD_REQUEST);
}

我的方法现在返回带有 HttpStatus.CREATEDHttpStatus.BAD_REQUESTResponseEntity。当在我的软件中实现前端时,我想知道如果方法也可以返回带有 HttpStatus 的 String ,那会非常方便。就像“边框已更新!”或“无法更新边框,因为... pla pla”。

向前端返回更具体的内容的最佳方式是什么?

最佳答案

您可以返回类似这样的内容,但您的方法返回类型应该是 ResponseEntity < String >,如果您愿意,您还可以传递 header 。

http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/http/ResponseEntity.html

return new ResponseEntity("your message", ok ? HttpStatus.CREATED : HttpStatus.BAD_REQUEST);

关于java - 返回包含信息的 ResponseEntity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40170931/

相关文章:

java - 整个文档和片段都用一个 XSD 吗?

java - 是什么让 Jasper Reports 抛出 java.io.IOException 读取字体数据?

json - 406 Spring MVC Json,根据请求 "accept"headers Not Acceptable

java - 来自命令行的 Weka

java - 对于没有 XML 的 Spring MVC 中的双调度程序配置,URL 映射无法按预期工作

spring-boot - 使用 keycloak - JWT token 保护 Spring Boot 服务

java - Spring Boot 中的 JSON Java 8 LocalDateTime 格式

java - 无法在 Spring Boot 应用程序中配置 ViewResolver

java - 我们应该什么时候关闭数据库连接

Java 排列枚举数组