java Spring Data REST:“没有从字符串值反序列化的字符串参数构造函数/工厂方法”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40986738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 05:35:36  来源:igfitidea点击:

Spring Data REST: "no String-argument constructor/factory method to deserialize from String value"

javaspringjava-8spring-data-restlombok

提问by Jan Nielsen

When I use Lombok in my Spring Data REST application to define complex types like:

当我在 Spring Data REST 应用程序中使用 Lombok 来定义复杂类型时,例如:

@NoArgsConstructor
@AllArgsConstructor
@Data

@Entity
@Table(name = "BOOK")
public class Book {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(nullable = false)
    private Long id;

    private String title;

    @ManyToOne(cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.REFRESH})
    private Person author;

    // ...
}

with a Spring Data REST controllers like:

使用 Spring Data REST 控制器,例如:

@RepositoryRestController
public class BookRepositoryRestController {

    private final BookRepository repository;

    @Autowired
    public BookRepositoryRestController(BookRepository repository) {
        this.repository = repository;
    }

    @RequestMapping(method = RequestMethod.POST,value = "/books")
    public @ResponseBody PersistentEntityResource post(
        @RequestBody Book book,
        PersistentEntityResourceAssembler assembler) {

        Book entity = processPost(book);

        return assembler.toResource(entity);
    }

    private Book processPost(Book book) {
        // ...
        return this.repository.save(book);
    }
}

I get an ugly error:

我收到一个丑陋的错误:

no String-argument constructor/factory method to deserialize from String value

from Spring Data REST's use of Hymanson with a Book POST like:

来自 Spring Data REST 使用 Hymanson 与 Book POST 类似:

curl -X POST 
     -H 'content-type: application/json' 
     -d '{"title":"Skip Like a Pro", "author": "/people/123"}'
     http://localhost:8080/api/books/

The de-serialization error happens when Hymanson tries to resolve the /people/123local URI which should resolve to a single, unique Person. If I remove my @RepositoryRestController, everything works fine. Any idea what's wrong with my REST controller definition?

当 Hymanson 尝试解析/people/123应该解析为单个唯一Person. 如果我删除 my @RepositoryRestController,一切正常。知道我的 REST 控制器定义有什么问题吗?

回答by Jan Nielsen

In the @RepositoryRestController, change the type of the @RequestBodyargument from Bookto Resource<Book>:

在 中@RepositoryRestController,将@RequestBody参数的类型从更改BookResource<Book>

import org.springframework.hateoas.Resource;

    // ...

    @RequestMapping(method = RequestMethod.POST,value = "/books")
    public @ResponseBody PersistentEntityResource post(
        @RequestBody Resource<Book> bookResource,             // Resource<Book>
        PersistentEntityResourceAssembler assembler) {

        Book book = bookResource.getContent()
        // ...
    }

and in the Bookentity definition modify the AllArgsConstructorannotation to be: @AllArgsConstructor(suppressConstructorProperties = true).

并在Book实体定义中将AllArgsConstructor注释修改为:@AllArgsConstructor(suppressConstructorProperties = true)

See Spring Data REST #687for more information.

有关更多信息,请参阅Spring Data REST #687