Spring Security 使spring数据rest将引用目标的uri视为字符串

ergxz8rk  于 2021-09-30  发布在  Java
关注(0)|答案(0)|浏览(229)

我有两门课 HouseService 多对多关系:

@Data
@Entity
public class House implements Serializable
{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String title = "";

    @ManyToMany(targetEntity = Service.class, cascade = {CascadeType.PERSIST, CascadeType.REFRESH})
    @JoinTable(...)
    private Set<Service> services;
}
@Data
@Entity
public class Service implements Serializable
{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name= "";

    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    @ManyToMany(mappedBy = "services", fetch = FetchType.LAZY)
    private Set<House> houses;
}

如果我想创建一个包含服务1和服务2的房子,我会发布到 /houses 与请求体类似:

{
    "title": "title1",
    "services": [
        "/services/1",
        "/services/2"
    ]
}

SpringDataREST自动生成的端点可以处理服务的uri并在数据库中创建关系。但当我导入SpringBootStarter安全性时,它抛出 HttpMessageNotReadableException :

2021-06-09 02:49:46.683  WARN 2192 --- [io-17698-exec-6] .m.m.a.ExceptionHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: Could not read payload!; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `com.a.project.entity.Service` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('/services/1')
 at [Source: UNKNOWN; line: -1, column: -1] (through reference chain: com.isep.project.entity.House["services"]->java.util.HashSet[0])]

spring似乎将请求体中的服务视为 String ,我必须这样写:

{
    "title": "title1",
    "services": [
        {"id":1},
        {"id":2}
    ]
}

我有点困惑,为什么会发生这种情况,我应该怎么做才能继续允许spring在导入Spring Security 时将服务作为uri来处理?
版本:
java:使用1.8和15进行了尝试
spring:使用2.4.5和2.5.0进行了尝试

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题