Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 7 additions & 19 deletions src/docs/java/org/springframework/hateoas/EmployeeController.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.*;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -130,7 +128,7 @@ public ResponseEntity<?> newEmployee(@RequestBody EntityModel<Employee> employee

Link link = linkTo(methodOn(getClass()).findOne(newEmployeeId)).withSelfRel().expand();

return ResponseEntity.created(URI.create(link.getHref())).build();
return ResponseEntity.created(link.toUri()).build();
}
// end::new[]

Expand All @@ -146,7 +144,7 @@ public ResponseEntity<?> updateEmployee( //
Link link = linkTo(methodOn(getClass()).findOne(id)).withSelfRel().expand();

return ResponseEntity.noContent() //
.location(URI.create(link.getHref())) //
.location(link.toUri()) //
.build();
}

Expand All @@ -157,8 +155,7 @@ public ResponseEntity<?> partiallyUpdateEmployee( //
// end::patch[]
{

Employee oldEmployee = EMPLOYEES.get(id);
Employee newEmployee = oldEmployee;
Employee newEmployee = EMPLOYEES.get(id);

if (employee.getContent().getName() != null) {
newEmployee = newEmployee.withName(employee.getContent().getName());
Expand All @@ -170,18 +167,9 @@ public ResponseEntity<?> partiallyUpdateEmployee( //

EMPLOYEES.put(id, newEmployee);

try {
return ResponseEntity //
.noContent() //
.location( //
new URI(findOne(id) //
.getLink(IanaLinkRelations.SELF) //
.map(link -> link.expand().getHref()) //
.orElse("") //
) //
).build();
} catch (URISyntaxException e) {
return ResponseEntity.badRequest().body(e.getMessage());
}
return ResponseEntity //
.noContent() //
.location(findOne(id).getRequiredLink(IanaLinkRelations.SELF).toUri()) //
.build();
}
}
8 changes: 4 additions & 4 deletions src/main/asciidoc/client.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ The following example shows how to use it:
Map<String, Object> parameters = new HashMap<>();
parameters.put("user", 27);

Traverson traverson = new Traverson(new URI("http://localhost:8080/api/"), MediaTypes.HAL_JSON);
String name = traverson.follow("movies", "movie", "actor").
withTemplateParameters(parameters).
toObject("$.name");
Traverson traverson = new Traverson(URI.create("http://localhost:8080/api/"), MediaTypes.HAL_JSON);
String name = traverson
.follow("movies", "movie", "actor").withTemplateParameters(parameters)
.toObject("$.name");
----
====

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand All @@ -37,11 +35,11 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.hateoas.CollectionModel;
import org.springframework.hateoas.EntityModel;
import org.springframework.hateoas.IanaLinkRelations;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.MediaTypes;
import org.springframework.hateoas.EntityModel;
import org.springframework.hateoas.CollectionModel;
import org.springframework.hateoas.config.EnableHypermediaSupport;
import org.springframework.hateoas.config.EnableHypermediaSupport.HypermediaType;
import org.springframework.hateoas.support.Employee;
Expand Down Expand Up @@ -267,40 +265,29 @@ public ResponseEntity<?> newEmployee(@RequestBody EntityModel<Employee> employee

EMPLOYEES.put(newEmployeeId, employee.getContent());

try {
return ResponseEntity.created(new URI(findOne(newEmployeeId) //
.getLink(IanaLinkRelations.SELF.value()) //
.map(link -> link.expand().getHref()) //
.orElse(""))) //
.build();
} catch (URISyntaxException e) {
return ResponseEntity.badRequest().body(e.getMessage());
}
return ResponseEntity.created(findOne(newEmployeeId) //
.getRequiredLink(IanaLinkRelations.SELF) //
.toUri()) //
.build();
}

@PutMapping("/employees/{id}")
public ResponseEntity<?> updateEmployee(@RequestBody EntityModel<Employee> employee, @PathVariable Integer id) {

EMPLOYEES.put(id, employee.getContent());

try {
return ResponseEntity.noContent() //
.location(new URI(findOne(id) //
.getLink(IanaLinkRelations.SELF.value()) //
.map(link -> link.expand().getHref()) //
.orElse(""))) //
.build();
} catch (URISyntaxException e) {
return ResponseEntity.badRequest().body(e.getMessage());
}
return ResponseEntity.noContent() //
.location(findOne(id) //
.getRequiredLink(IanaLinkRelations.SELF) //
.toUri()) //
.build();
}

@PatchMapping("/employees/{id}")
public ResponseEntity<?> partiallyUpdateEmployee(@RequestBody EntityModel<Employee> employee,
@PathVariable Integer id) {

Employee oldEmployee = EMPLOYEES.get(id);
Employee newEmployee = oldEmployee;
Employee newEmployee = EMPLOYEES.get(id);

if (employee.getContent().getName() != null) {
newEmployee = newEmployee.withName(employee.getContent().getName());
Expand All @@ -312,16 +299,11 @@ public ResponseEntity<?> partiallyUpdateEmployee(@RequestBody EntityModel<Employ

EMPLOYEES.put(id, newEmployee);

try {
return ResponseEntity.noContent() //
.location(new URI(findOne(id) //
.getLink(IanaLinkRelations.SELF.value()) //
.map(link -> link.expand().getHref()) //
.orElse(""))) //
.build();
} catch (URISyntaxException e) {
return ResponseEntity.badRequest().body(e.getMessage());
}
return ResponseEntity.noContent() //
.location(findOne(id) //
.getRequiredLink(IanaLinkRelations.SELF) //
.toUri()) //
.build();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,12 @@
*/
package org.springframework.hateoas.mediatype.hal.forms;

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.*;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand All @@ -35,11 +32,11 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.hateoas.CollectionModel;
import org.springframework.hateoas.EntityModel;
import org.springframework.hateoas.IanaLinkRelations;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.MediaTypes;
import org.springframework.hateoas.EntityModel;
import org.springframework.hateoas.CollectionModel;
import org.springframework.hateoas.config.EnableHypermediaSupport;
import org.springframework.hateoas.config.EnableHypermediaSupport.HypermediaType;
import org.springframework.hateoas.support.Employee;
Expand Down Expand Up @@ -86,8 +83,9 @@ public void singleEmployee() throws Exception {
.andReturn() //
.getResolvedException();

assertThat(exception.getMessage(), containsString("Affordance's URI http://localhost/employees"));
assertThat(exception.getMessage(), containsString("doesn't match self link http://localhost/employees/0"));
assertThat(exception).isNotNull();
assertThat(exception.getMessage()).contains("Affordance's URI http://localhost/employees");
assertThat(exception.getMessage()).contains("doesn't match self link http://localhost/employees/0");
}

@Test
Expand All @@ -97,8 +95,9 @@ public void collectionOfEmployees() throws Exception {
.andExpect(status().is5xxServerError()) //
.andReturn().getResolvedException();

assertThat(exception.getMessage(), containsString("Affordance's URI http://localhost/employees/0"));
assertThat(exception.getMessage(), containsString("doesn't match self link http://localhost/employees"));
assertThat(exception).isNotNull();
assertThat(exception.getMessage()).contains("Affordance's URI http://localhost/employees/0");
assertThat(exception.getMessage()).contains("doesn't match self link http://localhost/employees");
}

/**
Expand Down Expand Up @@ -155,30 +154,24 @@ public ResponseEntity<?> newEmployee(@RequestBody Employee employee) {

EMPLOYEES.put(newEmployeeId, employee);

try {
return ResponseEntity.noContent().location(new URI(findOne(newEmployeeId).getLink(IanaLinkRelations.SELF.value())
.map(link -> link.expand().getHref()).orElse(""))).build();
} catch (URISyntaxException e) {
return ResponseEntity.badRequest().body(e.getMessage());
}
return ResponseEntity.noContent() //
.location(findOne(newEmployeeId) //
.getRequiredLink(IanaLinkRelations.SELF) //
.toUri()) //
.build();
}

@PutMapping("/employees/{id}")
public ResponseEntity<?> updateEmployee(@RequestBody Employee employee, @PathVariable Integer id) {

EMPLOYEES.put(id, employee);

try {
return ResponseEntity //
.noContent() //
.location( //
new URI(findOne(id).getLink(IanaLinkRelations.SELF.value()) //
.map(link -> link.expand().getHref()) //
.orElse("")) //
).build();
} catch (URISyntaxException e) {
return ResponseEntity.badRequest().body(e.getMessage());
}
return ResponseEntity //
.noContent() //
.location(findOne(id) //
.getRequiredLink(IanaLinkRelations.SELF) //
.toUri()) //
.build();
}

@PatchMapping("/employees/{id}")
Expand All @@ -196,18 +189,12 @@ public ResponseEntity<?> partiallyUpdateEmployee(@RequestBody Employee employee,

EMPLOYEES.put(id, newEmployee);

try {
return ResponseEntity //
.noContent() //
.location( //
new URI(findOne(id) //
.getLink(IanaLinkRelations.SELF.value()) //
.map(link -> link.expand().getHref()) //
.orElse(""))) //
.build();
} catch (URISyntaxException e) {
return ResponseEntity.badRequest().body(e.getMessage());
}
return ResponseEntity //
.noContent() //
.location(findOne(id) //
.getRequiredLink(IanaLinkRelations.SELF) //
.toUri()) //
.build();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand All @@ -37,11 +36,11 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.hateoas.CollectionModel;
import org.springframework.hateoas.EntityModel;
import org.springframework.hateoas.IanaLinkRelations;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.MediaTypes;
import org.springframework.hateoas.EntityModel;
import org.springframework.hateoas.CollectionModel;
import org.springframework.hateoas.config.EnableHypermediaSupport;
import org.springframework.hateoas.config.EnableHypermediaSupport.HypermediaType;
import org.springframework.hateoas.mediatype.collectionjson.CollectionJsonLinkDiscoverer;
Expand Down Expand Up @@ -507,23 +506,15 @@ public ResponseEntity<?> newEmployee(@RequestBody EntityModel<Employee> employee

EMPLOYEES.put(newEmployeeId, employee.getContent());

try {
return ResponseEntity.created(toUri(newEmployeeId)).build();
} catch (URISyntaxException e) {
return ResponseEntity.badRequest().body(e.getMessage());
}
return ResponseEntity.created(toUri(newEmployeeId)).build();
}

@PutMapping("/employees/{id}")
public ResponseEntity<?> updateEmployee(@RequestBody EntityModel<Employee> employee, @PathVariable Integer id) {

EMPLOYEES.put(id, employee.getContent());

try {
return ResponseEntity.noContent().location(toUri(id)).build();
} catch (URISyntaxException e) {
return ResponseEntity.badRequest().body(e.getMessage());
}
return ResponseEntity.noContent().location(toUri(id)).build();
}

@PatchMapping("/employees/{id}")
Expand All @@ -542,21 +533,14 @@ public ResponseEntity<?> partiallyUpdateEmployee(@RequestBody EntityModel<Employ

EMPLOYEES.put(id, newEmployee);

try {
return ResponseEntity.noContent().location(toUri(id)).build();
} catch (URISyntaxException e) {
return ResponseEntity.badRequest().body(e.getMessage());
}
return ResponseEntity.noContent().location(toUri(id)).build();
}

private URI toUri(Integer id) throws URISyntaxException {

String uri = findOne(id) //
.getLink(IanaLinkRelations.SELF.value()) //
.map(link -> link.expand().getHref()) //
.orElse("");
private URI toUri(Integer id) {

return new URI(uri);
return findOne(id) //
.getRequiredLink(IanaLinkRelations.SELF) //
.toUri();
}
}

Expand Down
Loading