Java 将Spring数据Rest JPA实体保存在自定义控制器中,并返回HAL表示

Java 将Spring数据Rest JPA实体保存在自定义控制器中,并返回HAL表示,java,spring,spring-data-rest,Java,Spring,Spring Data Rest,在我的SpringBootWeb应用程序中,我有一个JPA实体Medium,它记录有关上载文件的信息 我有一个基本的Spring数据Rest存储库来处理一般操作: @RepositoryRestResource(path = "/media") public interface MediumRepository extends CrudRepository<Medium, Long> { } 它将返回实体的基本JSON表示,而不是HATEOAS。我认为我可能应该使用持久化Entit

在我的SpringBootWeb应用程序中,我有一个JPA实体
Medium
,它记录有关上载文件的信息

我有一个基本的Spring数据Rest存储库来处理一般操作:

@RepositoryRestResource(path = "/media")
public interface MediumRepository extends CrudRepository<Medium, Long> {
}
它将返回实体的基本JSON表示,而不是HATEOAS。我认为我可能应该使用
持久化EntityResourcesAssembler

因此,我当前的控制器代码是:

@RestController
@RequestMapping("/upload")
public class MediaEndpoint {

    @Autowired
    private MediumRepository mediumRepository;

    @RequestMapping(method = POST)
    public PersistentEntityResource uploadMedium(
            @RequestPart MultipartFile data,
            PersistentEntityResourceAssembler persistentEntityResourceAssembler) {

        Medium medium = new Medium();
        // setup of the medium instance
        Medium savedMedium = mediumRepository.save(medium);
        return persistentEntityResourceAssembler.toResource(savedMedium);
    }
}
但是,我无法将
PersistentEntityResourcesEmbler
注入到方法中-我正在

Failed to instantiate [org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler]: No default constructor found; nested exception is java.lang.NoSuchMethodException: org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler.<init>()
实例化[org.springframework.data.rest.webmvc.PersistentEntityResourcesAssembler]失败:未找到默认构造函数;嵌套异常是java.lang.NoSuchMethodException:org.springframework.data.rest.webmvc.PersistentEntityResourcesAssembler。()
如何实现此功能?

尝试使用而不是@RestController。

接下来,我将控制器更改为
@RepositoryRestController
,但我遇到了一个异常

Circular view path [upload]: would dispatch back to the current handler URL [/upload] again.
Check your ViewResolver setup! (Hint: This may be the result of an unspecified view,
due to default view name generation.)
我发现
RepositoryRestController
s没有用
@ResponseBody
注释,应该返回
ResponseEntity
,因此我将代码更改为以下内容:

@RepositoryRestController
@RequestMapping("/upload")
public class MediaEndpoint {

    @Autowired
    private MediumRepository mediumRepository;

    @RequestMapping(method = POST)
    public ResponseEntity<PersistentEntityResource> uploadMedium(
            @RequestPart MultipartFile data,
            PersistentEntityResourceAssembler persistentEntityResourceAssembler) {

        Medium medium = new Medium();
        // setup of the medium instance
        Medium savedMedium = mediumRepository.save(medium);
        return ResponseEntity.ok(persistentEntityResourceAssembler.toResource(savedMedium));
    }
}
@RepositoryRestController
@请求映射(“/upload”)
公共类MediaEndpoint{
@自动连线
私有媒体存储库媒体存储库;
@请求映射(方法=POST)
公众响应上传媒体(
@RequestPart多部分文件数据,
PersistentEntityResourcesAssembler PersistentEntityResourcesAssembler){
介质=新介质();
//介质实例的设置
Medium savedMedium=mediumRepository.save(Medium);
返回ResponseEntity.ok(PersistentEntityResourcesAssembler.toResource(savedMedium));
}
}
这给了我一个很好的JSON响应,带有HATEOAS元数据


或者,用
@ResponseBody
注释方法或控制器的工作方式相同。

这促使我朝着正确的方向前进,谢谢。我不得不做一些其他的调整,看看我自己的答案
@RepositoryRestController
@RequestMapping("/upload")
public class MediaEndpoint {

    @Autowired
    private MediumRepository mediumRepository;

    @RequestMapping(method = POST)
    public ResponseEntity<PersistentEntityResource> uploadMedium(
            @RequestPart MultipartFile data,
            PersistentEntityResourceAssembler persistentEntityResourceAssembler) {

        Medium medium = new Medium();
        // setup of the medium instance
        Medium savedMedium = mediumRepository.save(medium);
        return ResponseEntity.ok(persistentEntityResourceAssembler.toResource(savedMedium));
    }
}