From 90bbb5ab9ef18230f90bac791562542f9ae1918b Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 5 May 2015 12:04:33 +0100 Subject: [PATCH] Update samples to make them compatible with Java 7 The main project supports Java 7 or later, but, prior to this commit, the Spring HATEOAS sample required Java 8. This commit updates updates the Spring HATEOAS sample to make it compatible with Java 7 and updates the build configuration of both samples to specify Java 7. Closes gh-51 --- .travis.yml | 7 ++-- .../.settings/org.eclipse.jdt.core.prefs | 6 +-- .../rest-notes-spring-data-rest/build.gradle | 3 ++ samples/rest-notes-spring-data-rest/pom.xml | 2 +- .../.settings/org.eclipse.jdt.core.prefs | 6 +-- .../rest-notes-spring-hateoas/build.gradle | 3 ++ samples/rest-notes-spring-hateoas/pom.xml | 2 +- .../com/example/notes/NoteRepository.java | 5 +-- .../com/example/notes/NotesController.java | 41 +++++++++++-------- .../java/com/example/notes/TagRepository.java | 6 +-- .../com/example/notes/TagsController.java | 21 ++++++---- 11 files changed, 57 insertions(+), 45 deletions(-) diff --git a/.travis.yml b/.travis.yml index 43bb5feb..0477e7d0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,5 @@ language: java +jdk: + - oraclejdk7 script: - - jdk_switcher use oraclejdk7 - - "./gradlew build" - - jdk_switcher use oraclejdk8 - - "./gradlew buildSamples" \ No newline at end of file + - "./gradlew build buildSamples" \ No newline at end of file diff --git a/samples/rest-notes-spring-data-rest/.settings/org.eclipse.jdt.core.prefs b/samples/rest-notes-spring-data-rest/.settings/org.eclipse.jdt.core.prefs index 03628c92..83412a90 100644 --- a/samples/rest-notes-spring-data-rest/.settings/org.eclipse.jdt.core.prefs +++ b/samples/rest-notes-spring-data-rest/.settings/org.eclipse.jdt.core.prefs @@ -1,14 +1,14 @@ eclipse.preferences.version=1 org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled -org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve -org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.compliance=1.7 org.eclipse.jdt.core.compiler.debug.lineNumber=generate org.eclipse.jdt.core.compiler.debug.localVariable=generate org.eclipse.jdt.core.compiler.debug.sourceFile=generate org.eclipse.jdt.core.compiler.problem.assertIdentifier=error org.eclipse.jdt.core.compiler.problem.enumIdentifier=error -org.eclipse.jdt.core.compiler.source=1.8 +org.eclipse.jdt.core.compiler.source=1.7 org.eclipse.jdt.core.formatter.align_type_members_on_columns=false org.eclipse.jdt.core.formatter.alignment_for_arguments_in_allocation_expression=16 org.eclipse.jdt.core.formatter.alignment_for_arguments_in_annotation=0 diff --git a/samples/rest-notes-spring-data-rest/build.gradle b/samples/rest-notes-spring-data-rest/build.gradle index 41b3e656..0acf3af5 100644 --- a/samples/rest-notes-spring-data-rest/build.gradle +++ b/samples/rest-notes-spring-data-rest/build.gradle @@ -23,6 +23,9 @@ repositories { group = 'com.example' +sourceCompatibility = 1.7 +targetCompatibility = 1.7 + dependencies { compile 'org.springframework.boot:spring-boot-starter-data-rest' compile 'org.springframework.boot:spring-boot-starter-data-jpa' diff --git a/samples/rest-notes-spring-data-rest/pom.xml b/samples/rest-notes-spring-data-rest/pom.xml index 5346f769..a12bcf06 100644 --- a/samples/rest-notes-spring-data-rest/pom.xml +++ b/samples/rest-notes-spring-data-rest/pom.xml @@ -17,7 +17,7 @@ UTF-8 - 1.8 + 1.7 diff --git a/samples/rest-notes-spring-hateoas/.settings/org.eclipse.jdt.core.prefs b/samples/rest-notes-spring-hateoas/.settings/org.eclipse.jdt.core.prefs index 03628c92..83412a90 100644 --- a/samples/rest-notes-spring-hateoas/.settings/org.eclipse.jdt.core.prefs +++ b/samples/rest-notes-spring-hateoas/.settings/org.eclipse.jdt.core.prefs @@ -1,14 +1,14 @@ eclipse.preferences.version=1 org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled -org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve -org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.compliance=1.7 org.eclipse.jdt.core.compiler.debug.lineNumber=generate org.eclipse.jdt.core.compiler.debug.localVariable=generate org.eclipse.jdt.core.compiler.debug.sourceFile=generate org.eclipse.jdt.core.compiler.problem.assertIdentifier=error org.eclipse.jdt.core.compiler.problem.enumIdentifier=error -org.eclipse.jdt.core.compiler.source=1.8 +org.eclipse.jdt.core.compiler.source=1.7 org.eclipse.jdt.core.formatter.align_type_members_on_columns=false org.eclipse.jdt.core.formatter.alignment_for_arguments_in_allocation_expression=16 org.eclipse.jdt.core.formatter.alignment_for_arguments_in_annotation=0 diff --git a/samples/rest-notes-spring-hateoas/build.gradle b/samples/rest-notes-spring-hateoas/build.gradle index fe5b449c..d8b78d53 100644 --- a/samples/rest-notes-spring-hateoas/build.gradle +++ b/samples/rest-notes-spring-hateoas/build.gradle @@ -23,6 +23,9 @@ repositories { group = 'com.example' +sourceCompatibility = 1.7 +targetCompatibility = 1.7 + dependencies { compile 'org.springframework.boot:spring-boot-starter-web' compile 'org.springframework.boot:spring-boot-starter-data-jpa' diff --git a/samples/rest-notes-spring-hateoas/pom.xml b/samples/rest-notes-spring-hateoas/pom.xml index 1c9bfbba..7484e485 100644 --- a/samples/rest-notes-spring-hateoas/pom.xml +++ b/samples/rest-notes-spring-hateoas/pom.xml @@ -17,7 +17,7 @@ UTF-8 - 1.8 + 1.7 diff --git a/samples/rest-notes-spring-hateoas/src/main/java/com/example/notes/NoteRepository.java b/samples/rest-notes-spring-hateoas/src/main/java/com/example/notes/NoteRepository.java index 01e41b57..87c0a72f 100644 --- a/samples/rest-notes-spring-hateoas/src/main/java/com/example/notes/NoteRepository.java +++ b/samples/rest-notes-spring-hateoas/src/main/java/com/example/notes/NoteRepository.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 the original author or authors. + * Copyright 2014-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,13 +18,12 @@ package com.example.notes; import java.util.Collection; import java.util.List; -import java.util.Optional; import org.springframework.data.repository.CrudRepository; public interface NoteRepository extends CrudRepository { - Optional findById(long id); + Note findById(long id); List findByTagsIn(Collection tags); } diff --git a/samples/rest-notes-spring-hateoas/src/main/java/com/example/notes/NotesController.java b/samples/rest-notes-spring-hateoas/src/main/java/com/example/notes/NotesController.java index 2ff6c5f1..0eccb0e2 100644 --- a/samples/rest-notes-spring-hateoas/src/main/java/com/example/notes/NotesController.java +++ b/samples/rest-notes-spring-hateoas/src/main/java/com/example/notes/NotesController.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 the original author or authors. + * Copyright 2014-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,8 +19,8 @@ package com.example.notes; import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo; import java.net.URI; +import java.util.ArrayList; import java.util.List; -import java.util.stream.Collectors; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.hateoas.Resource; @@ -92,25 +92,19 @@ public class NotesController { @RequestMapping(value = "/{id}", method = RequestMethod.GET) Resource note(@PathVariable("id") long id) { - Note note = this.noteRepository.findById(id).orElseThrow( - () -> new ResourceDoesNotExistException()); - return this.noteResourceAssembler.toResource(note); + return this.noteResourceAssembler.toResource(findNoteById(id)); } @RequestMapping(value = "/{id}/tags", method = RequestMethod.GET) ResourceSupport noteTags(@PathVariable("id") long id) { return new NestedContentResource( - this.tagResourceAssembler.toResources(this.noteRepository - .findById(id) - .orElseThrow( - () -> new ResourceDoesNotExistException()).getTags())); + this.tagResourceAssembler.toResources(findNoteById(id).getTags())); } @RequestMapping(value = "/{id}", method = RequestMethod.PATCH) @ResponseStatus(HttpStatus.NO_CONTENT) void updateNote(@PathVariable("id") long id, @RequestBody NotePatchInput noteInput) { - Note note = this.noteRepository.findById(id).orElseThrow( - () -> new ResourceDoesNotExistException()); + Note note = findNoteById(id); if (noteInput.getTagUris() != null) { note.setTags(getTags(noteInput.getTagUris())); } @@ -123,14 +117,25 @@ public class NotesController { this.noteRepository.save(note); } + private Note findNoteById(long id) { + Note note = this.noteRepository.findById(id); + if (note == null) { + throw new ResourceDoesNotExistException(); + } + return note; + } + private List getTags(List tagLocations) { - return tagLocations - .stream() - .map(location -> this.tagRepository.findById(extractTagId(location)) - . orElseThrow( - () -> new IllegalArgumentException("The tag '" + location - + "' does not exist"))) - .collect(Collectors.toList()); + List tags = new ArrayList<>(tagLocations.size()); + for (URI tagLocation: tagLocations) { + Tag tag = this.tagRepository.findById(extractTagId(tagLocation)); + if (tag == null) { + throw new IllegalArgumentException("The tag '" + tagLocation + + "' does not exist"); + } + tags.add(tag); + } + return tags; } private long extractTagId(URI tagLocation) { diff --git a/samples/rest-notes-spring-hateoas/src/main/java/com/example/notes/TagRepository.java b/samples/rest-notes-spring-hateoas/src/main/java/com/example/notes/TagRepository.java index 97fbffcb..c92d9e23 100644 --- a/samples/rest-notes-spring-hateoas/src/main/java/com/example/notes/TagRepository.java +++ b/samples/rest-notes-spring-hateoas/src/main/java/com/example/notes/TagRepository.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 the original author or authors. + * Copyright 2014-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,12 +16,10 @@ package com.example.notes; -import java.util.Optional; - import org.springframework.data.repository.CrudRepository; public interface TagRepository extends CrudRepository { - Optional findById(long id); + Tag findById(long id); } diff --git a/samples/rest-notes-spring-hateoas/src/main/java/com/example/notes/TagsController.java b/samples/rest-notes-spring-hateoas/src/main/java/com/example/notes/TagsController.java index f29867c5..bc22f066 100644 --- a/samples/rest-notes-spring-hateoas/src/main/java/com/example/notes/TagsController.java +++ b/samples/rest-notes-spring-hateoas/src/main/java/com/example/notes/TagsController.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 the original author or authors. + * Copyright 2014-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -79,24 +79,29 @@ public class TagsController { @RequestMapping(value = "/{id}", method = RequestMethod.GET) Resource tag(@PathVariable("id") long id) { - Tag tag = this.repository.findById(id).orElseThrow( - () -> new ResourceDoesNotExistException()); + Tag tag = findTagById(id); return this.tagResourceAssembler.toResource(tag); } @RequestMapping(value = "/{id}/notes", method = RequestMethod.GET) ResourceSupport tagNotes(@PathVariable("id") long id) { + Tag tag = findTagById(id); return new NestedContentResource( - this.noteResourceAssembler.toResources(this.repository.findById(id) - .orElseThrow(() -> new ResourceDoesNotExistException()) - .getNotes())); + this.noteResourceAssembler.toResources(tag.getNotes())); + } + + private Tag findTagById(long id) { + Tag tag = this.repository.findById(id); + if (tag == null) { + throw new ResourceDoesNotExistException(); + } + return tag; } @RequestMapping(value = "/{id}", method = RequestMethod.PATCH) @ResponseStatus(HttpStatus.NO_CONTENT) void updateTag(@PathVariable("id") long id, @RequestBody TagPatchInput tagInput) { - Tag tag = this.repository.findById(id).orElseThrow( - () -> new ResourceDoesNotExistException()); + Tag tag = findTagById(id); if (tagInput.getName() != null) { tag.setName(tagInput.getName()); }