Created
January 31, 2023 04:07
-
-
Save mageddo/bb0eb78d23f7d61c4273976d01365e15 to your computer and use it in GitHub Desktop.
WRITING JSON REST SERVICES
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.fasterxml.jackson.databind.ObjectMapper; | |
import io.quarkus.jackson.ObjectMapperCustomizer; | |
import javax.enterprise.inject.Instance; | |
import javax.enterprise.inject.Produces; | |
import javax.inject.Singleton; | |
public class CustomObjectMapper { | |
// Replaces the CDI producer for ObjectMapper built into Quarkus | |
@Singleton | |
@Produces | |
ObjectMapper objectMapper(Instance<ObjectMapperCustomizer> customizers) { | |
ObjectMapper mapper = myObjectMapper(); // Custom `ObjectMapper` | |
// Apply all ObjectMapperCustomizer beans (incl. Quarkus) | |
for (ObjectMapperCustomizer customizer : customizers) { | |
customizer.customize(mapper); | |
} | |
return mapper; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://quarkus.io/guides/rest-json | |
package org.acme.rest.json; | |
import java.util.Collections; | |
import java.util.LinkedHashMap; | |
import java.util.Set; | |
import javax.ws.rs.DELETE; | |
import javax.ws.rs.GET; | |
import javax.ws.rs.POST; | |
import javax.ws.rs.Path; | |
@Path("/fruits") | |
public class FruitResource { | |
private Set<Fruit> fruits = Collections.newSetFromMap(Collections.synchronizedMap(new LinkedHashMap<>())); | |
public FruitResource() { | |
fruits.add(new Fruit("Apple", "Winter fruit")); | |
fruits.add(new Fruit("Pineapple", "Tropical fruit")); | |
} | |
@GET | |
public Set<Fruit> list() { | |
return fruits; | |
} | |
@POST | |
public Set<Fruit> add(Fruit fruit) { | |
fruits.add(fruit); | |
return fruits; | |
} | |
@DELETE | |
public Set<Fruit> delete(Fruit fruit) { | |
fruits.removeIf(existingFruit -> existingFruit.name.contentEquals(fruit.name)); | |
return fruits; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment