简体   繁体   中英

Generate Link To Spring Data Rest Controller

I created a REST API with Spring Data Rest that forks fine. It must be possible to clone Project s via the API, so I added a custom @RestController to implement that via POST /projects/{id}/clone .

@RestController
@RequestMapping(value = "/projects", produces = "application/hal+json")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ProjectCloneController {

    private final ProjectRepo projectRepo;

    @PostMapping("/{id}/clone")
    public EntityModel<Project> clone(@PathVariable String id) {
        Optional<Project> origOpt = projectRepo.findById(id);
        Project original = origOpt.get();
        Project clone = createClone(original);

        EntityModel<Project> result = EntityModel.of(clone);
        result.add(linkTo(ProjectRepo.class).slash(clone.getId()).withSelfRel());
        
        return result;
    }

I am stuck at the point where I need to add a Link to the EntityModel that points to an endpoint provided by Spring Data Rest. It will need to support a different base path, and act correctly to X headers as well.

Unfortunately, the line above ( linkTo and slash ) just generates http://localhost:8080/636f4aaac9143f1da03bac0e which misses the name of the resource.

Check org.springframework.data.rest.webmvc.support.RepositoryEntityLinks.linkFor

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM