本文整理汇总了Java中org.springframework.hateoas.MediaTypes.HAL_JSON属性的典型用法代码示例。如果您正苦于以下问题:Java MediaTypes.HAL_JSON属性的具体用法?Java MediaTypes.HAL_JSON怎么用?Java MediaTypes.HAL_JSON使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类org.springframework.hateoas.MediaTypes
的用法示例。
在下文中一共展示了MediaTypes.HAL_JSON属性的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: newEmployee
/**
* Instead of putting the creation link from the remote service in the template (a security concern),
* have a local route for {@literal POST} requests. Gather up the information, and form a remote call,
* using {@link Traverson} to fetch the {@literal employees} {@link Link}.
*
* Once a new employee is created, redirect back to the root URL.
*
* @param employee
* @return
* @throws URISyntaxException
*/
@PostMapping("/employees")
public String newEmployee(@ModelAttribute Employee employee) throws URISyntaxException {
Traverson client = new Traverson(new URI(REMOTE_SERVICE_ROOT_URI), MediaTypes.HAL_JSON);
Link employeesLink = client
.follow("employees")
.asLink();
this.rest.postForEntity(employeesLink.expand().getHref(), employee, Employee.class);
return "redirect:/";
}
示例2: traversCustomersIT
@Test
public void traversCustomersIT() {
//Travers customers
try {
final Traverson traverson = new Traverson(new URI(BASE_URI), MediaTypes.HAL_JSON);
final PagedResources<Resource<Customer>> resources = traverson
.follow("customers")
.toObject(new TypeReferences.PagedResourcesType<Resource<Customer>>() {
});
assertThat(resources.getContent()).isNotNull();
assertThat(resources.getContent().size()).isEqualTo(3);
System.err.println(resources.getContent());
} catch (URISyntaxException e) {
fail(e.getMessage());
}
}
示例3: traversCustomersResourceIT
@Test
public void traversCustomersResourceIT() {
//Travers customers
try {
final Traverson traverson = new Traverson(new URI(BASE_URI), MediaTypes.HAL_JSON);
final PagedResources<Resource<Customer>> resources = traverson
.follow("customers")
.toObject(new TypeReferences.PagedResourcesType<Resource<Customer>>() {
});
assertThat(resources.getContent()).isNotNull();
assertThat(resources.getContent().size()).isEqualTo(3);
System.err.println(resources.getMetadata());
for (Resource<Customer> resource : resources) {
final List<Link> links = resource.getLinks();
System.err.println(links);
final Customer customer = resource.getContent();
System.err.println(customer);
}
} catch (URISyntaxException e) {
fail(e.getMessage());
}
}
示例4: traversCustomerFindByFirstNameIT
@Test
public void traversCustomerFindByFirstNameIT() {
//travers to findByFirstName
try {
final Map<String, Object> parameters = new HashMap<>();
parameters.put("firstName", "Donnie");
final Traverson traverson = new Traverson(new URI(BASE_URI), MediaTypes.HAL_JSON);
final PagedResources<Resource<Customer>> resources = traverson
.follow("customers", "search", "findByFirstName")
.withTemplateParameters(parameters)
.toObject(new TypeReferences.PagedResourcesType<Resource<Customer>>() {
});
assertThat(resources.getContent()).isNotNull();
assertThat(resources.getContent().size()).isEqualTo(1);
final List<Resource<Customer>> customers = new LinkedList<>(resources.getContent());
final Customer customer = customers.get(0).getContent();
assertThat(customer).isNotNull();
assertThat(customer.getFirstName()).isEqualTo("Donnie");
} catch (URISyntaxException e) {
fail(e.getMessage());
}
}
示例5: discoverStoreSearch
@Test
public void discoverStoreSearch() {
Traverson traverson = new Traverson(URI.create(String.format(SERVICE_URI, port)), MediaTypes.HAL_JSON);
// Set up path traversal
TraversalBuilder builder = traverson. //
follow("stores", "search", "by-location");
// Log discovered
log.info("");
log.info("Discovered link: {}", builder.asTemplatedLink());
log.info("");
Map<String, Object> parameters = new HashMap<>();
parameters.put("location", "40.740337,-73.995146");
parameters.put("distance", "0.5miles");
PagedResources<Resource<Store>> resources = builder.//
withTemplateParameters(parameters).//
toObject(new PagedResourcesType<Resource<Store>>() {});
PageMetadata metadata = resources.getMetadata();
log.info("Got {} of {} stores: ", resources.getContent().size(), metadata.getTotalElements());
StreamSupport.stream(resources.spliterator(), false).//
map(Resource::getContent).//
forEach(store -> log.info("{} - {}", store.name, store.address));
}
示例6: index
/**
* Get a listing of ALL {@link Employee}s by querying the remote services' root URI, and then
* "hopping" to the {@literal employees} rel.
*
* NOTE: Also create a form-backed {@link Employee} object to allow creating a new entry with
* the Thymeleaf template.
*
* @param model
* @return
* @throws URISyntaxException
*/
@GetMapping
public String index(Model model) throws URISyntaxException {
Traverson client = new Traverson(new URI(REMOTE_SERVICE_ROOT_URI), MediaTypes.HAL_JSON);
Resources<Resource<Employee>> employees = client
.follow("employees")
.toObject(new ResourcesType<Resource<Employee>>(){});
model.addAttribute("employee", new Employee());
model.addAttribute("employees", employees);
return "index";
}
示例7: create
public Traverson create(String serviceId) {
ServiceInstance instance = loadBalancerClient.choose(serviceId);
if (instance == null) {
throw new IllegalStateException("No instances for service: "+serviceId);
}
return new Traverson(instance.getUri(), MediaTypes.HAL_JSON);
}
示例8: getRubricAssociationResource
/**
* Returns the ToolItemRubricAssociation resource for the given tool and associated item ID, wrapped as an Optional.
* @param toolId the tool id, something like "sakai.assignment"
* @param associatedToolItemId the id of the associated element within the tool
* @return
*/
protected Optional<Resource<ToolItemRubricAssociation>> getRubricAssociationResource(String toolId, String associatedToolItemId) throws Exception {
TypeReferences.ResourcesType<Resource<ToolItemRubricAssociation>> resourceParameterizedTypeReference =
new TypeReferences.ResourcesType<Resource<ToolItemRubricAssociation>>() {};
URI apiBaseUrl = new URI(serverConfigurationService.getServerUrl() + RBCS_SERVICE_URL_PREFIX);
Traverson traverson = new Traverson(apiBaseUrl, MediaTypes.HAL_JSON);
Traverson.TraversalBuilder builder = traverson.follow("rubric-associations", "search",
"by-tool-item-ids");
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", String.format("Bearer %s", generateJsonWebToken(toolId)));
builder.withHeaders(headers);
Map<String, Object> parameters = new HashMap<>();
parameters.put("toolId", toolId);
parameters.put("itemId", associatedToolItemId);
Resources<Resource<ToolItemRubricAssociation>> associationResources = builder.withTemplateParameters(
parameters).toObject(resourceParameterizedTypeReference);
// Should only be one matching this search criterion
if (associationResources.getContent().size() > 1) {
throw new IllegalStateException(String.format(
"Number of rubric association resources greater than one for request: %s",
associationResources.getLink(Link.REL_SELF).toString()));
}
Optional<Resource<ToolItemRubricAssociation>> associationResource = associationResources.getContent().stream().findFirst();
return associationResource;
}
示例9: HalLinkDiscoverer
public HalLinkDiscoverer() {
super("$._links..['%s']..href", MediaTypes.HAL_JSON);
}
示例10: discoverNamedLink
public Link discoverNamedLink(URI uri, String... linkNames){
Traverson traverson = new Traverson(uri, MediaTypes.HAL_JSON);
Traverson.TraversalBuilder builder = traverson.follow(linkNames);
return builder.asLink();
}