如何在 HAL 中描述 POST 链接?
我正在设计一个带有 HATEOAS 约束的 RESTful API,类似于维基百科的 HATEOAS 示例,但以 HAL JSON 表示(为了清楚起见,删除了方案、主机等):
GET /accounts/12345
{
"id" : 12345,
"balance" : 100.00
"_links" : {
"self" : {
"href" : "/accounts/12345"
},
"transfer" : {
"href" : "/accounts/12345/transfer{?amount,target}",
"templated" : true
}
}
}
Run Code Online (Sandbox Code Playgroud)
要执行“传输”操作,客户端可能会执行以下操作:
GET /accounts/12345/transfer?amount=100.00,target=54321
{
"id" : 34567,
"amount" : 100.00
"_links" : {
"self" : {
"href" : "/transfers/34567"
},
"source" : {
"href" : "/account/12345"
},
"target" : {
"href" : …Run Code Online (Sandbox Code Playgroud) 我正在根据Spring官方教程构建一个RESTful服务。我按照指示添加了依赖项,但是STS(Spring Tool Suite)无法弄清楚我的功能。
STS无法理解methodOn()或者lintTo()一直出错,请帮助我解决。
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)
控制器:
@GetMapping("/employees/{id}")
Resource<Employee> one(@PathVariable Long id) {
Employee employee = repository.findById(id)
.orElseThrow(() -> new EmployeeNotFoundException(id));
return new Resource<>(employee,
linkTo(methodOn(EmployeeController.class).one(id)).withSelfRel(),
linkTo(methodOn(EmployeeController.class).all()).withRel("employees"));
}
Run Code Online (Sandbox Code Playgroud) 问题是我在使用@RepositoryRestResource我的UserRepository扩展时遇到了异常JpaRepository。
原因是默认情况下findById只接受Long或输入Int,即使我有
@Id String id;而不是@Id Int id在我的实体定义中。
我尝试搜索 StackOverflow 和 Google,但没有找到任何解决方案。
错误信息如下:
"Failed to convert from type [java.lang.String] to type [java.lang.Integer] for value '3175433272470683'; nested exception is java.lang.NumberFormatException: For input string: \"3175433272470683\""
我想让它与
@Id String id;
有什么建议?
非常感谢提前。在这里提问是我的荣幸。
实体类:
@Entity // This tells Hibernate to make a table out of this class
@Table(name = "users")
public class XmppUser {
@Id
private java.lang.String username;
private String …Run Code Online (Sandbox Code Playgroud) 我添加spring hateoas到一个项目中,但无法启动该项目。
我添加了这些库:
implementation 'com.toedter:spring-hateoas-jsonapi:2.0.1'
implementation 'org.springframework.boot:spring-boot-starter-hateoas:3.0.2'
Run Code Online (Sandbox Code Playgroud)
我有这个logback-spring配置:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<springProperty scope="context" name="filename" source="app.logging.filename" defaultValue="application"/>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d{yyyy.MM.dd HH:mm:ss.SSS} [%thread] %highlight(%-5level) %cyan(%logger{36}:%L) - %msg%n</pattern>
</encoder>
</appender>
<appender name="DAILY_ROLLING_FILE_APPENDER_JSON" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logs/${filename}.json</file>
<!-- TBD: This encoder differs from the one used in the Dashboard project in order to generate JSON output -->
<encoder class="net.logstash.logback.encoder.LogstashEncoder"/>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>logs/archived/${filename}-%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>30</maxHistory>
<totalSizeCap>100MB</totalSizeCap>
</rollingPolicy>
</appender>
<!-- config for STDOUT and SAVE-TO-FILE -->
<springProfile name="local">
<root level="info">
<appender-ref …Run Code Online (Sandbox Code Playgroud) 我正在创建一个超媒体驱动的RESTful API,用于查询事务数据.目的是将结果分页.
每个API调用都将查询索引数据库表.由于内存考虑因素,我不想保留结果服务器端,因此我想根据rownum检索数据,具体取决于请求的页面.第一页WHERE rownum <= 10上的EG,第二页上的EG WHERE rownum BETWEEN 11 AND 20等
但是,有问题的数据库是从生产系统复制的,可能会将记录添加到已请求的结果集的区域中.请求EG页面 - >返回> 10行 - >在第5行插入事务.现在,第2页将包含已在第一页上显示的记录,因为结果基本上由rownum推送.
实现我的目标是创建一个超媒体驱动的RESTful API,从数据库提供分页事务数据,而不是在会话期间保持结果集,这将是一个好方法?
我已经实现了一个小示例项目来说明我遇到的问题。它位于这里:
https://github.com/jvillane/spring-boot-hateoas-rest
Run Code Online (Sandbox Code Playgroud)
我想要做的是创建同一个实体的几个@Projection:
https://github.com/jvillane/spring-boot-hateoas-rest
Run Code Online (Sandbox Code Playgroud)
并使用它们通过调用(带引号和不带引号)来获取或多或少的实体信息:
@Projection(name = "S", types = User.class)
public interface UserS {
String getName();
}
@Projection(name = "M", types = User.class)
public interface UserM {
String getName();
String getDni();
}
@Projection(name = "L", types = User.class)
public interface UserL {
String getName();
String getDni();
Country getCountry();
}
Run Code Online (Sandbox Code Playgroud)
但这对响应没有影响,就像使用默认方式显示实体信息一样。
我不知道我做错了什么。欢迎任何帮助。
我有一个简单的UserRepository暴露使用Spring Data REST.这是User实体类:
@Document(collection = User.COLLECTION_NAME)
@Setter
@Getter
public class User extends Entity {
public static final String COLLECTION_NAME = "users";
private String name;
private String email;
private String password;
private Set<UserRole> roles = new HashSet<>(0);
}
Run Code Online (Sandbox Code Playgroud)
我已经创建了一个UserProjection类,它看起来如下:
@JsonInclude(JsonInclude.Include.NON_NULL)
@Projection(types = User.class)
public interface UserProjection {
String getId();
String getName();
String getEmail();
Set<UserRole> getRoles();
}
Run Code Online (Sandbox Code Playgroud)
这是存储库类:
@RepositoryRestResource(collectionResourceRel = User.COLLECTION_NAME, path = RestPath.Users.ROOT,
excerptProjection = UserProjection.class)
public interface RestUserRepository extends MongoRepository<User, String> { …Run Code Online (Sandbox Code Playgroud) 我一直在开发一个云应用程序来处理 Spring Cloud 等问题。现在我被困在尝试使用 RestTemplate API 向 Spring Data Rest 后端发送 POST 或 PUT 请求,但我尝试的一切都以错误结束:HttpMessageNotReadableException:无法从 START_OBJECT 令牌中反序列化 java.lang.String 的实例,HttpMessageNotReadableException : 无法读取文档: 无法从 START_ARRAY 令牌中反序列化 java.lang.String 的实例,...来自内容类型为 application/xml;charset=UTF-8! 的请求,错误 400 null...你说出它的名字. 经过研究,我发现使用 RestTemplate(如果我没记错的话,级别 3 JSON 超媒体)实际上很难使用 HAL JSON,但我想知道这是否可能。
我想看看 RestTemplate 将 POST 和 PUT 发送到 Spring Data Rest 后端的一些工作(如果可能的话,请详细说明)示例。
编辑:我尝试过 postForEntity、postForLocation、exchange,但它以不同类型的错误结束。这些是我尝试过的一些片段(还有更多,只是我处理了它们)。
我的实体:
@Entity
public class Account implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@NotNull
@NotEmpty
private String username; …Run Code Online (Sandbox Code Playgroud) 谁能解释Swagger和HATEOAS之间的区别。我可以搜索很多次,但是没有朋友可以解释这两个方面的正确详细答案。
Spring HATEOAS方法有一个 Kotlin 变体linkTo,它采用控制器的具体化类型参数和主体的函数:
org.springframework.hateoas.server.mvc WebMvcLinkBuilderDslKt.class public inline fun <reified C> linkTo(\n func: C.() \xe2\x86\x92 Unit\n): WebMvcLinkBuilder\nRun Code Online (Sandbox Code Playgroud)\n但我不知道如何实际使用它,因为我还没有找到任何有用的文档,而且 API 也不是很直观。我尝试过这样的:
\nlinkTo<MyHandler> { findById(req) }.toUriComponentsBuilder().build(mapOf("id" to 1)).toURL()\nRun Code Online (Sandbox Code Playgroud)\nreq我认为如果链接应该指向另一个方法,那么使用周围方法的对象是错误的。结果就是http://localhost:8080没有任何路径或参数。
如何与 Kotlin DSL 建立链接?
\n由于我使用的是 Spring WebMvc.fn:是否有另一种方法来构建与此框架的链接?
\nspring-hateoas ×10
java ×4
spring-boot ×4
hateoas ×3
rest ×3
spring ×3
spring-mvc ×2
hibernate ×1
json ×1
kotlin ×1
logback ×1
resttemplate ×1
spring-data ×1
swagger ×1
web-services ×1