Skip to content

Commit 347a06c

Browse files
committed
#860 - Switch to URI.create() and toUri() to avoid checked exceptions.
`URI.create()` wraps `new URI()` and converts a `UriSyntaxException` into an `IllegalArgumentException`. Also, take advantage of `toUri()`.
1 parent c4e1bed commit 347a06c

File tree

8 files changed

+106
-190
lines changed

8 files changed

+106
-190
lines changed

src/docs/java/org/springframework/hateoas/EmployeeController.java

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717

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

20-
import java.net.URI;
21-
import java.net.URISyntaxException;
2220
import java.util.ArrayList;
2321
import java.util.List;
2422
import java.util.Map;
@@ -130,7 +128,7 @@ public ResponseEntity<?> newEmployee(@RequestBody EntityModel<Employee> employee
130128

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

133-
return ResponseEntity.created(URI.create(link.getHref())).build();
131+
return ResponseEntity.created(link.toUri()).build();
134132
}
135133
// end::new[]
136134

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

148146
return ResponseEntity.noContent() //
149-
.location(URI.create(link.getHref())) //
147+
.location(link.toUri()) //
150148
.build();
151149
}
152150

@@ -170,18 +168,9 @@ public ResponseEntity<?> partiallyUpdateEmployee( //
170168

171169
EMPLOYEES.put(id, newEmployee);
172170

173-
try {
174-
return ResponseEntity //
175-
.noContent() //
176-
.location( //
177-
new URI(findOne(id) //
178-
.getLink(IanaLinkRelations.SELF) //
179-
.map(link -> link.expand().getHref()) //
180-
.orElse("") //
181-
) //
182-
).build();
183-
} catch (URISyntaxException e) {
184-
return ResponseEntity.badRequest().body(e.getMessage());
185-
}
171+
return ResponseEntity //
172+
.noContent() //
173+
.location(findOne(id).getRequiredLink(IanaLinkRelations.SELF).toUri()) //
174+
.build();
186175
}
187176
}

src/main/asciidoc/client.adoc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ The following example shows how to use it:
1515
Map<String, Object> parameters = new HashMap<>();
1616
parameters.put("user", 27);
1717
18-
Traverson traverson = new Traverson(new URI("http://localhost:8080/api/"), MediaTypes.HAL_JSON);
19-
String name = traverson.follow("movies", "movie", "actor").
20-
withTemplateParameters(parameters).
21-
toObject("$.name");
18+
Traverson traverson = new Traverson(URI.create("http://localhost:8080/api/"), MediaTypes.HAL_JSON);
19+
String name = traverson
20+
.follow("movies", "movie", "actor").withTemplateParameters(parameters)
21+
.toObject("$.name");
2222
----
2323
====
2424

src/test/java/org/springframework/hateoas/mediatype/collectionjson/CollectionJsonWebMvcIntegrationTest.java

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
2323
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;
2424

25-
import java.net.URI;
26-
import java.net.URISyntaxException;
2725
import java.util.ArrayList;
2826
import java.util.HashMap;
2927
import java.util.List;
@@ -37,11 +35,11 @@
3735
import org.springframework.context.annotation.Bean;
3836
import org.springframework.context.annotation.Configuration;
3937
import org.springframework.core.io.ClassPathResource;
38+
import org.springframework.hateoas.CollectionModel;
39+
import org.springframework.hateoas.EntityModel;
4040
import org.springframework.hateoas.IanaLinkRelations;
4141
import org.springframework.hateoas.Link;
4242
import org.springframework.hateoas.MediaTypes;
43-
import org.springframework.hateoas.EntityModel;
44-
import org.springframework.hateoas.CollectionModel;
4543
import org.springframework.hateoas.config.EnableHypermediaSupport;
4644
import org.springframework.hateoas.config.EnableHypermediaSupport.HypermediaType;
4745
import org.springframework.hateoas.support.Employee;
@@ -267,32 +265,22 @@ public ResponseEntity<?> newEmployee(@RequestBody EntityModel<Employee> employee
267265

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

270-
try {
271-
return ResponseEntity.created(new URI(findOne(newEmployeeId) //
272-
.getLink(IanaLinkRelations.SELF.value()) //
273-
.map(link -> link.expand().getHref()) //
274-
.orElse(""))) //
275-
.build();
276-
} catch (URISyntaxException e) {
277-
return ResponseEntity.badRequest().body(e.getMessage());
278-
}
268+
return ResponseEntity.created(findOne(newEmployeeId) //
269+
.getRequiredLink(IanaLinkRelations.SELF) //
270+
.toUri()) //
271+
.build();
279272
}
280273

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

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

286-
try {
287-
return ResponseEntity.noContent() //
288-
.location(new URI(findOne(id) //
289-
.getLink(IanaLinkRelations.SELF.value()) //
290-
.map(link -> link.expand().getHref()) //
291-
.orElse(""))) //
292-
.build();
293-
} catch (URISyntaxException e) {
294-
return ResponseEntity.badRequest().body(e.getMessage());
295-
}
279+
return ResponseEntity.noContent() //
280+
.location(findOne(id) //
281+
.getRequiredLink(IanaLinkRelations.SELF) //
282+
.toUri()) //
283+
.build();
296284
}
297285

298286
@PatchMapping("/employees/{id}")
@@ -312,16 +300,11 @@ public ResponseEntity<?> partiallyUpdateEmployee(@RequestBody EntityModel<Employ
312300

313301
EMPLOYEES.put(id, newEmployee);
314302

315-
try {
316-
return ResponseEntity.noContent() //
317-
.location(new URI(findOne(id) //
318-
.getLink(IanaLinkRelations.SELF.value()) //
319-
.map(link -> link.expand().getHref()) //
320-
.orElse(""))) //
321-
.build();
322-
} catch (URISyntaxException e) {
323-
return ResponseEntity.badRequest().body(e.getMessage());
324-
}
303+
return ResponseEntity.noContent() //
304+
.location(findOne(id) //
305+
.getRequiredLink(IanaLinkRelations.SELF) //
306+
.toUri()) //
307+
.build();
325308
}
326309
}
327310

src/test/java/org/springframework/hateoas/mediatype/hal/forms/HalFormsValidationIntegrationTest.java

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
2323
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;
2424

25-
import java.net.URI;
26-
import java.net.URISyntaxException;
2725
import java.util.ArrayList;
2826
import java.util.List;
2927
import java.util.Map;
@@ -35,11 +33,11 @@
3533
import org.springframework.beans.factory.annotation.Autowired;
3634
import org.springframework.context.annotation.Bean;
3735
import org.springframework.context.annotation.Configuration;
36+
import org.springframework.hateoas.CollectionModel;
37+
import org.springframework.hateoas.EntityModel;
3838
import org.springframework.hateoas.IanaLinkRelations;
3939
import org.springframework.hateoas.Link;
4040
import org.springframework.hateoas.MediaTypes;
41-
import org.springframework.hateoas.EntityModel;
42-
import org.springframework.hateoas.CollectionModel;
4341
import org.springframework.hateoas.config.EnableHypermediaSupport;
4442
import org.springframework.hateoas.config.EnableHypermediaSupport.HypermediaType;
4543
import org.springframework.hateoas.support.Employee;
@@ -155,30 +153,24 @@ public ResponseEntity<?> newEmployee(@RequestBody Employee employee) {
155153

156154
EMPLOYEES.put(newEmployeeId, employee);
157155

158-
try {
159-
return ResponseEntity.noContent().location(new URI(findOne(newEmployeeId).getLink(IanaLinkRelations.SELF.value())
160-
.map(link -> link.expand().getHref()).orElse(""))).build();
161-
} catch (URISyntaxException e) {
162-
return ResponseEntity.badRequest().body(e.getMessage());
163-
}
156+
return ResponseEntity.noContent() //
157+
.location(findOne(newEmployeeId) //
158+
.getRequiredLink(IanaLinkRelations.SELF) //
159+
.toUri()) //
160+
.build();
164161
}
165162

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

169166
EMPLOYEES.put(id, employee);
170167

171-
try {
172-
return ResponseEntity //
173-
.noContent() //
174-
.location( //
175-
new URI(findOne(id).getLink(IanaLinkRelations.SELF.value()) //
176-
.map(link -> link.expand().getHref()) //
177-
.orElse("")) //
178-
).build();
179-
} catch (URISyntaxException e) {
180-
return ResponseEntity.badRequest().body(e.getMessage());
181-
}
168+
return ResponseEntity //
169+
.noContent() //
170+
.location(findOne(id) //
171+
.getRequiredLink(IanaLinkRelations.SELF) //
172+
.toUri()) //
173+
.build();
182174
}
183175

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

197189
EMPLOYEES.put(id, newEmployee);
198190

199-
try {
200-
return ResponseEntity //
201-
.noContent() //
202-
.location( //
203-
new URI(findOne(id) //
204-
.getLink(IanaLinkRelations.SELF.value()) //
205-
.map(link -> link.expand().getHref()) //
206-
.orElse(""))) //
207-
.build();
208-
} catch (URISyntaxException e) {
209-
return ResponseEntity.badRequest().body(e.getMessage());
210-
}
191+
return ResponseEntity //
192+
.noContent() //
193+
.location(findOne(id) //
194+
.getRequiredLink(IanaLinkRelations.SELF) //
195+
.toUri()) //
196+
.build();
211197
}
212198
}
213199

src/test/java/org/springframework/hateoas/server/mvc/MultiMediaTypeWebMvcIntegrationTest.java

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;
2424

2525
import java.net.URI;
26-
import java.net.URISyntaxException;
2726
import java.util.ArrayList;
2827
import java.util.List;
2928
import java.util.Map;
@@ -37,11 +36,11 @@
3736
import org.springframework.context.annotation.Bean;
3837
import org.springframework.context.annotation.Configuration;
3938
import org.springframework.core.io.ClassPathResource;
39+
import org.springframework.hateoas.CollectionModel;
40+
import org.springframework.hateoas.EntityModel;
4041
import org.springframework.hateoas.IanaLinkRelations;
4142
import org.springframework.hateoas.Link;
4243
import org.springframework.hateoas.MediaTypes;
43-
import org.springframework.hateoas.EntityModel;
44-
import org.springframework.hateoas.CollectionModel;
4544
import org.springframework.hateoas.config.EnableHypermediaSupport;
4645
import org.springframework.hateoas.config.EnableHypermediaSupport.HypermediaType;
4746
import org.springframework.hateoas.mediatype.collectionjson.CollectionJsonLinkDiscoverer;
@@ -507,23 +506,15 @@ public ResponseEntity<?> newEmployee(@RequestBody EntityModel<Employee> employee
507506

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

510-
try {
511-
return ResponseEntity.created(toUri(newEmployeeId)).build();
512-
} catch (URISyntaxException e) {
513-
return ResponseEntity.badRequest().body(e.getMessage());
514-
}
509+
return ResponseEntity.created(toUri(newEmployeeId)).build();
515510
}
516511

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

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

522-
try {
523-
return ResponseEntity.noContent().location(toUri(id)).build();
524-
} catch (URISyntaxException e) {
525-
return ResponseEntity.badRequest().body(e.getMessage());
526-
}
517+
return ResponseEntity.noContent().location(toUri(id)).build();
527518
}
528519

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

543534
EMPLOYEES.put(id, newEmployee);
544535

545-
try {
546-
return ResponseEntity.noContent().location(toUri(id)).build();
547-
} catch (URISyntaxException e) {
548-
return ResponseEntity.badRequest().body(e.getMessage());
549-
}
536+
return ResponseEntity.noContent().location(toUri(id)).build();
550537
}
551538

552-
private URI toUri(Integer id) throws URISyntaxException {
553-
554-
String uri = findOne(id) //
555-
.getLink(IanaLinkRelations.SELF.value()) //
556-
.map(link -> link.expand().getHref()) //
557-
.orElse("");
539+
private URI toUri(Integer id) {
558540

559-
return new URI(uri);
541+
return findOne(id) //
542+
.getRequiredLink(IanaLinkRelations.SELF) //
543+
.toUri();
560544
}
561545
}
562546

0 commit comments

Comments
 (0)