本文整理汇总了Java中org.springframework.hateoas.Resource类的典型用法代码示例。如果您正苦于以下问题:Java Resource类的具体用法?Java Resource怎么用?Java Resource使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Resource类属于org.springframework.hateoas包,在下文中一共展示了Resource类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: events
import org.springframework.hateoas.Resource; //导入依赖的package包/类
@GetMapping("events")
HttpEntity<Resources<?>> events(PagedResourcesAssembler<AbstractEvent<?>> assembler,
@SortDefault("publicationDate") Pageable pageable,
@RequestParam(required = false) @DateTimeFormat(iso = ISO.DATE_TIME) LocalDateTime since,
@RequestParam(required = false) String type) {
QAbstractEvent $ = QAbstractEvent.abstractEvent;
BooleanBuilder builder = new BooleanBuilder();
// Apply date
Optional.ofNullable(since).ifPresent(it -> builder.and($.publicationDate.after(it)));
// Apply type
Optional.ofNullable(type) //
.flatMap(events::findEventTypeByName) //
.ifPresent(it -> builder.and($.instanceOf(it)));
Page<AbstractEvent<?>> result = events.findAll(builder, pageable);
PagedResources<Resource<AbstractEvent<?>>> resource = assembler.toResource(result, event -> toResource(event));
resource
.add(links.linkTo(methodOn(EventController.class).events(assembler, pageable, since, type)).withRel("events"));
return ResponseEntity.ok(resource);
}
示例2: updateEmployee
import org.springframework.hateoas.Resource; //导入依赖的package包/类
@PutMapping("/employees/{id}")
ResponseEntity<?> updateEmployee(@RequestBody Employee employee, @PathVariable long id) {
Employee employeeToUpdate = employee;
employeeToUpdate.setId(id);
Employee updatedEmployee = repository.save(employeeToUpdate);
return new Resource<>(updatedEmployee,
linkTo(methodOn(EmployeeController.class).findOne(updatedEmployee.getId())).withSelfRel()
.andAffordance(afford(methodOn(EmployeeController.class).updateEmployee(null, updatedEmployee.getId())))
.andAffordance(afford(methodOn(EmployeeController.class).deleteEmployee(updatedEmployee.getId()))),
linkTo(methodOn(EmployeeController.class).findAll()).withRel("employees")
).getId()
.map(Link::getHref)
.map(href -> {
try {
return new URI(href);
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
})
.map(uri -> ResponseEntity.noContent().location(uri).build())
.orElse(ResponseEntity.badRequest().body("Unable to update " + employeeToUpdate));
}
示例3: newEmployee
import org.springframework.hateoas.Resource; //导入依赖的package包/类
@PostMapping("/employees")
ResponseEntity<?> newEmployee(@RequestBody Employee employee) {
Employee savedEmployee = repository.save(employee);
return new Resource<>(savedEmployee,
linkTo(methodOn(EmployeeController.class).findOne(savedEmployee.getId())).withSelfRel()
.andAffordance(afford(methodOn(EmployeeController.class).updateEmployee(null, savedEmployee.getId())))
.andAffordance(afford(methodOn(EmployeeController.class).deleteEmployee(savedEmployee.getId()))),
linkTo(methodOn(EmployeeController.class).findAll()).withRel("employees")
).getId()
.map(Link::getHref)
.map(href -> {
try {
return new URI(href);
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
})
.map(uri -> ResponseEntity.noContent().location(uri).build())
.orElse(ResponseEntity.badRequest().body("Unable to create " + employee));
}
示例4: addLinks
import org.springframework.hateoas.Resource; //导入依赖的package包/类
/**
* Define links to add to every {@link Resource}.
*
* @param resource
*/
@Override
protected void addLinks(Resource<Employee> resource) {
/**
* Add some custom links to the default ones provided.
*
* NOTE: To replace default links, don't invoke {@literal super.addLinks()}.
*/
super.addLinks(resource);
resource.getContent().getId()
.ifPresent(id -> {
// Add additional links
resource.add(linkTo(methodOn(ManagerController.class).findManager(id)).withRel("manager"));
resource.add(linkTo(methodOn(EmployeeController.class).findDetailedEmployee(id)).withRel("detailed"));
// Maintain a legacy link to support older clients not yet adjusted to the switch from "supervisor" to "manager".
resource.add(linkTo(methodOn(SupervisorController.class).findOne(id)).withRel("supervisor"));
});
}
示例5: appendEventResource
import org.springframework.hateoas.Resource; //导入依赖的package包/类
/**
* Appends an {@link AccountEvent} domain event to the event log of the {@link Account}
* aggregate with the specified accountId.
*
* @param accountId is the unique identifier for the {@link Account}
* @param event is the {@link AccountEvent} that attempts to alter the state of the {@link Account}
* @return a hypermedia resource for the newly appended {@link AccountEvent}
*/
private Resource<AccountEvent> appendEventResource(Long accountId, AccountEvent event) {
Assert.notNull(event, "Event body must be provided");
Account account = accountRepository.findOne(accountId);
Assert.notNull(account, "Account could not be found");
event.setAccountId(account.getIdentity());
eventService.apply(event);
return new Resource<>(event,
linkTo(AccountController.class)
.slash("accounts")
.slash(accountId)
.slash("events")
.slash(event.getEventId())
.withSelfRel(),
linkTo(AccountController.class)
.slash("accounts")
.slash(accountId)
.withRel("account")
);
}
示例6: getAccountResource
import org.springframework.hateoas.Resource; //导入依赖的package包/类
/**
* Get a hypermedia enriched {@link Account} entity.
*
* @param account is the {@link Account} to enrich with hypermedia links
* @return is a hypermedia enriched resource for the supplied {@link Account} entity
*/
private Resource<Account> getAccountResource(Account account) {
Assert.notNull(account, "Account must not be null");
if (!account.hasLink("commands")) {
// Add command link
account.add(linkBuilder("getCommands", account.getIdentity()).withRel("commands"));
}
if (!account.hasLink("events")) {
// Add get events link
account.add(linkBuilder("getAccountEvents", account.getIdentity()).withRel("events"));
}
return new Resource<>(account);
}
示例7: getCommandsResource
import org.springframework.hateoas.Resource; //导入依赖的package包/类
private ResourceSupport getCommandsResource(Long id) {
Account account = new Account();
account.setIdentity(id);
CommandResources commandResources = new CommandResources();
// Add activate command link
commandResources.add(linkTo(AccountController.class)
.slash("accounts")
.slash(id)
.slash("commands")
.slash("activate")
.withRel("activate"));
// Add suspend command link
commandResources.add(linkTo(AccountController.class)
.slash("accounts")
.slash(id)
.slash("commands")
.slash("suspend")
.withRel("suspend"));
return new Resource<>(commandResources);
}
示例8: getProjectResource
import org.springframework.hateoas.Resource; //导入依赖的package包/类
/**
* Get a hypermedia enriched {@link Project} entity.
*
* @param project is the {@link Project} to enrich with hypermedia links
* @return is a hypermedia enriched resource for the supplied {@link Project} entity
*/
private Resource<Project> getProjectResource(Project project) {
Assert.notNull(project, "Project must not be null");
if (!project.hasLink("commands")) {
// Add command link
project.add(linkBuilder("getCommands", project.getIdentity()).withRel("commands"));
}
if (!project.hasLink("events")) {
// Add get events link
project.add(linkBuilder("getProjectEvents", project.getIdentity()).withRel("events"));
}
if (!project.hasLink("commits")) {
// Add get events link
project.add(linkBuilder("getProjectCommits", project.getIdentity()).withRel("commits"));
}
return new Resource<>(project);
}
示例9: getCommandsResource
import org.springframework.hateoas.Resource; //导入依赖的package包/类
private ResourceSupport getCommandsResource(Long id) {
Project project = new Project();
project.setIdentity(id);
CommandResources commandResources = new CommandResources();
// Add suspend command link
commandResources.add(linkTo(ProjectController.class)
.slash("projects")
.slash(id)
.slash("commands")
.slash("commit")
.withRel("commit"));
return new Resource<>(commandResources);
}
示例10: process
import org.springframework.hateoas.Resource; //导入依赖的package包/类
public Resource<SubmissionEnvelope> process(Resource<SubmissionEnvelope> resource) {
SubmissionEnvelope submissionEnvelope = resource.getContent();
// add subresource links for each type of metadata document in a submission envelope
resource.add(getAnalysesLink(submissionEnvelope));
resource.add(getAssaysLink(submissionEnvelope));
resource.add(getFilesLink(submissionEnvelope));
resource.add(getProjectsLink(submissionEnvelope));
resource.add(getProtocolsLink(submissionEnvelope));
resource.add(getSamplesLink(submissionEnvelope));
// add subresource links for events that occur in response to state transitions
submissionEnvelope.allowedStateTransitions().stream()
.map(submissionState -> getStateTransitionLink(submissionEnvelope, submissionState))
.filter(Optional::isPresent)
.map(Optional::get)
.forEach(resource::add);
return resource;
}
示例11: status
import org.springframework.hateoas.Resource; //导入依赖的package包/类
@Override
public Info status(String releaseName) {
ParameterizedTypeReference<Resource<Info>> typeReference =
new ParameterizedTypeReference<Resource<Info>>() { };
Map<String, String> uriVariables = new HashMap<String, String>();
uriVariables.put("releaseName", releaseName);
ResponseEntity<Resource<Info>> resourceResponseEntity =
restTemplate.exchange(baseUri + "/release/status/{releaseName}",
HttpMethod.GET,
null,
typeReference,
uriVariables);
return resourceResponseEntity.getBody().getContent();
}
示例12: manifest
import org.springframework.hateoas.Resource; //导入依赖的package包/类
@Override
public String manifest(String releaseName) {
ParameterizedTypeReference<Resource<Manifest>> typeReference =
new ParameterizedTypeReference<Resource<Manifest>>() { };
Map<String, String> uriVariables = new HashMap<String, String>();
uriVariables.put("releaseName", releaseName);
ResponseEntity<Resource<Manifest>> resourceResponseEntity =
restTemplate.exchange(baseUri + "/release/manifest/{releaseName}",
HttpMethod.GET,
null,
typeReference,
uriVariables);
return resourceResponseEntity.getBody().getContent().getData();
}
示例13: install
import org.springframework.hateoas.Resource; //导入依赖的package包/类
public Release install(InstallRequest installRequest) {
ParameterizedTypeReference<Resource<Release>> typeReference =
new ParameterizedTypeReference<Resource<Release>>() { };
String url = String.format("%s/%s/%s", baseUri, "package", "install");
HttpEntity<InstallRequest> httpEntity = new HttpEntity<>(installRequest);
ResponseEntity<Resource<Release>> resourceResponseEntity =
restTemplate.exchange(url, HttpMethod.POST, httpEntity, typeReference);
return resourceResponseEntity.getBody().getContent();
}
示例14: deleteRepository
import org.springframework.hateoas.Resource; //导入依赖的package包/类
@Override
public void deleteRepository(String name) {
ParameterizedTypeReference<Resource<Repository>> typeReference = new ParameterizedTypeReference<Resource<Repository>>() {
};
Traverson.TraversalBuilder traversalBuilder = this.traverson.follow("repositories", "search", "findByName");
Map<String, Object> parameters = new HashMap<>();
parameters.put("name", name);
Resource<Repository> repositoryResource = traversalBuilder.withTemplateParameters(parameters)
.toObject(typeReference);
if (repositoryResource != null) {
this.restTemplate.delete(repositoryResource.getId().getHref());
}
else {
throw new IllegalStateException("The Repository with the " + name + " doesn't exist.");
}
}
示例15: upload
import org.springframework.hateoas.Resource; //导入依赖的package包/类
@Override
public PackageMetadata upload(UploadRequest uploadRequest) {
ParameterizedTypeReference<Resource<PackageMetadata>> typeReference =
new ParameterizedTypeReference<Resource<PackageMetadata>>() { };
String url = String.format("%s/%s/%s", baseUri, "package", "upload");
log.debug("Uploading package {}-{} to repository {}.", uploadRequest.getName(), uploadRequest.getVersion(),
uploadRequest.getRepoName());
HttpEntity<UploadRequest> httpEntity = new HttpEntity<>(uploadRequest);
ResponseEntity<Resource<PackageMetadata>> resourceResponseEntity =
restTemplate.exchange(url, HttpMethod.POST, httpEntity, typeReference);
PackageMetadata packageMetadata = resourceResponseEntity.getBody().getContent();
return packageMetadata;
}