Skip to content

Conversation

tfesenko
Copy link
Member

@tfesenko tfesenko commented Sep 25, 2018

Here we create a new class that will contain our implementations of the various API methods. This class will live alongside the existing PetsApiDelegate class in the demo project.

Use Java 8

We use Java 8 features in our implementation, but the project is configured to Use Java 7.

Update pom.xml of the generated project: <java.version>1.8</java.version>.
As pom.xml is now manually modified, to prevent overriding it with codegen, we need to add it to the.openapi-generator-ignore file.

Note: if you do not see .openapi-geneator-ignore in your project, you need to disable the .* resources filter here:
image

After making these changes, right-click on the generated project and select Maven -> Update project... to activate the change to Java 8.

New PetsApiDelegateImpl Class

For simplicity, I am using a simple HashMap instead of a database.

Create this class, named PetsApiDelegateImpl, alongside the PetsApiDelegate generated class:

package com.reprezen.demo.springboot.api;

@Service
public class PetsApiDelegateImpl implements PetsApiDelegate {
 	private final Map<Long, Pet> pets = Maps.newHashMap();
	private long nextId = 0l;
 	@Override
	public ResponseEntity<Pet> addPet(NewPet newPet) {
		Pet petToAdd = new Pet();
		petToAdd.id(nextId++).name(newPet.getName()).tag(newPet.getTag());
		pets.put(petToAdd.getId(), petToAdd);
		return new ResponseEntity<>(petToAdd, HttpStatus.CREATED);
	}
 	@Override
	public ResponseEntity<Void> deletePet(Long id) {
		if (!pets.containsKey(id)) {
			return new ResponseEntity<>(HttpStatus.NOT_FOUND);
		}
		pets.remove(id);
		return new ResponseEntity<>(HttpStatus.NO_CONTENT);
	}
 	@Override
	public ResponseEntity<Pet> findPetById(Long id) {
		if (!pets.containsKey(id)) {
			return new ResponseEntity<>(HttpStatus.NOT_FOUND);
		}
		return new ResponseEntity<>(pets.get(id), HttpStatus.ACCEPTED);
	}
 	@Override
	public ResponseEntity<List<Pet>> findPets(List<String> tags, Integer limitObject) {
		int limit = limitObject == null ? Integer.MAX_VALUE : limitObject;
		List<Pet> filteredPets = pets.values().stream()//
				.filter(pet -> (tags == null || tags.isEmpty()) ? true : tags.contains(pet.getTag()))//
				.limit(limit)//
				.collect(Collectors.toList());
		return new ResponseEntity<>(filteredPets, HttpStatus.ACCEPTED);
	}
 }

You can use the usual "Organize Imports" feature after creating this file to add the necessary imports, after which you should see a clean build. (Choose java.util.List and com.google.common.collect.Maps for those classes.)

The result of step 3 is on branch step_2b.

Now, you can run the API locally using mvn spring-boot:run. You can create a new pet and view it, you can also view all pets. To exercise the API, you can use a utility like curl or open the service in Swagger-UI at http://localhost:8080 and use its Try It Out feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant