Skip to content

Commit

Permalink
Change default media type to JSON
Browse files Browse the repository at this point in the history
This adds a new RESTEasy provider that will produce
JSON by default. This removes the need to add
@produces(MediaType.APPLICATION_JSON) and
@consumes(MediaType.APPLICATION_JSON) everywhere.

The old behaviour can be retained by setting:

quarkus.resteasy-json.default-json=false

in application.properties.
  • Loading branch information
stuartwdouglas committed Oct 7, 2020
1 parent 93b61a1 commit 18456a9
Show file tree
Hide file tree
Showing 113 changed files with 324 additions and 340 deletions.
4 changes: 0 additions & 4 deletions docs/src/main/asciidoc/amazon-dynamodb.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -281,17 +281,13 @@ package org.acme.dynamodb;
import java.util.List;
import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/fruits")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class FruitResource {
@Inject
Expand Down
2 changes: 0 additions & 2 deletions docs/src/main/asciidoc/amazon-s3.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,6 @@ public class S3SyncClientResource extends CommonResource {
}
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<FileObject> listFiles() {
ListObjectsRequest listRequest = ListObjectsRequest.builder().bucket(bucketName).build();
Expand Down Expand Up @@ -511,7 +510,6 @@ public class S3AsyncClientResource extends CommonResource {
}
@GET
@Produces(MediaType.APPLICATION_JSON)
public Uni<List<FileObject>> listFiles() {
ListObjectsRequest listRequest = ListObjectsRequest.builder()
.bucket(bucketName)
Expand Down
4 changes: 0 additions & 4 deletions docs/src/main/asciidoc/amazon-sqs.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,6 @@ public class QuarksShieldSyncResource {
static ObjectReader QUARK_READER = new ObjectMapper().readerFor(Quark.class);
@GET
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public List<Quark> receive() {
List<Message> messages = sqs.receiveMessage(m -> m.maxNumberOfMessages(10).queueUrl(queueUrl)).messages();
Expand Down Expand Up @@ -423,8 +421,6 @@ public class QuarksShieldAsyncResource {
static ObjectReader QUARK_READER = new ObjectMapper().readerFor(Quark.class);
@GET
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Uni<List<Quark>> receive() {
return Uni.createFrom()
.completionStage(sqs.receiveMessage(m -> m.maxNumberOfMessages(10).queueUrl(queueUrl)))
Expand Down
4 changes: 0 additions & 4 deletions docs/src/main/asciidoc/blaze-persistence.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,6 @@ public class GiftResource {
SantaClausService santaClausService;
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Transactional
public Response createGift(GiftUpdateView view) {
entityViewManager.save(entityManager, view);
Expand All @@ -193,8 +191,6 @@ public class GiftResource {
@PUT
@Path("{id}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Transactional
public GiftView updateGift(@EntityViewId("id") GiftUpdateView view) {
evm.save(em, view);
Expand Down
2 changes: 0 additions & 2 deletions docs/src/main/asciidoc/cache.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ import java.util.List;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.jboss.resteasy.annotations.jaxrs.QueryParam;
Expand All @@ -170,7 +169,6 @@ public class WeatherForecastResource {
WeatherForecastService service;
@GET
@Produces(MediaType.APPLICATION_JSON)
public WeatherForecast getForecast(@QueryParam String city, @QueryParam long daysInFuture) { <1>
long executionStart = System.currentTimeMillis();
List<String> dailyForecasts = Arrays.asList(
Expand Down
2 changes: 0 additions & 2 deletions docs/src/main/asciidoc/cassandra.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,6 @@ The last missing piece is the REST API that will expose GET and POST methods:
[source,java]
----
@Path("/fruits")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class FruitResource {
private static final String STORE_NAME = "acme";
Expand Down
1 change: 0 additions & 1 deletion docs/src/main/asciidoc/context-propagation.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ them to the client as JSON using link:rest-json[JSON-B or Jackson]:
@Transactional
@GET
@Path("/people")
@Produces(MediaType.APPLICATION_JSON)
public CompletionStage<List<Person>> people() throws SystemException {
// Create a REST client to the Star Wars API
WebClient client = WebClient.create(vertx,
Expand Down
2 changes: 0 additions & 2 deletions docs/src/main/asciidoc/elasticsearch.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,6 @@ Now, edit the `org.acme.elasticsearch.FruitResource` class as follows:
[source,java]
----
@Path("/fruits")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class FruitResource {
@Inject
FruitService fruitService;
Expand Down
2 changes: 0 additions & 2 deletions docs/src/main/asciidoc/getting-started-reactive.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -540,8 +540,6 @@ import javax.ws.rs.core.Response.Status;
import java.net.URI;
@Path("fruits")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class FruitResource {
@Inject
Expand Down
4 changes: 0 additions & 4 deletions docs/src/main/asciidoc/hibernate-orm.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -765,14 +765,10 @@ Let's start by implementing the `/{tenant}` endpoint. As you can see from the so
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
@ApplicationScoped
@Produces("application/json")
@Consumes("application/json")
@Path("/{tenant}")
public class FruitResource {
Expand Down
2 changes: 0 additions & 2 deletions docs/src/main/asciidoc/hibernate-search-elasticsearch.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,6 @@ import org.jboss.resteasy.annotations.jaxrs.FormParam;
import org.jboss.resteasy.annotations.jaxrs.PathParam;
@Path("/library")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class LibraryResource {
@Inject
Expand Down
4 changes: 0 additions & 4 deletions docs/src/main/asciidoc/kafka-streams.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -725,11 +725,9 @@ import java.util.List;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
Expand All @@ -746,8 +744,6 @@ public class WeatherStationEndpoint {
@GET
@Path("/data/{id}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response getWeatherStationData(@PathParam("id") int id) {
GetWeatherStationDataResult result = interactiveQueries.getWeatherStationData(id);
Expand Down
2 changes: 0 additions & 2 deletions docs/src/main/asciidoc/kotlin.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,12 @@ We also update the `GreetingResource.kt` like so:
----
import javax.ws.rs.GET
import javax.ws.rs.Path
import javax.ws.rs.Produces
import javax.ws.rs.core.MediaType
@Path("/greeting")
class GreetingResource {
@GET
@Produces(MediaType.APPLICATION_JSON)
fun hello() = Greeting("hello")
}
----
Expand Down
1 change: 0 additions & 1 deletion docs/src/main/asciidoc/kubernetes-client.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ public class Pods {
}
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/{namespace}")
public List<Pod> pods(@PathParam("namespace") String namespace) {
return kubernetesClient.pods().inNamespace(namespace).list().getItems();
Expand Down
3 changes: 2 additions & 1 deletion docs/src/main/asciidoc/micrometer.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.concurrent.atomic.LongAccumulator;
import java.util.function.Supplier;
Expand All @@ -124,7 +125,7 @@ public class PrimeNumberResource {
@GET
@Path("/{number}")
@Produces("text/plain")
@Produces(MediaType.TEXT_PLAIN)
public String checkIfPrime(@PathParam("number") int number) {
if (number < 1) {
return "Only natural numbers can be prime numbers.";
Expand Down
3 changes: 0 additions & 3 deletions docs/src/main/asciidoc/microprofile-fault-tolerance.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,10 @@ import java.util.concurrent.atomic.AtomicLong;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.jboss.logging.Logger;
@Path("/coffee")
@Produces(MediaType.APPLICATION_JSON)
public class CoffeeResource {
private static final Logger LOGGER = Logger.getLogger(CoffeeResource.class);
Expand Down
3 changes: 2 additions & 1 deletion docs/src/main/asciidoc/microprofile-metrics.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ import org.jboss.resteasy.annotations.jaxrs.PathParam;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/")
public class PrimeNumberChecker {
Expand All @@ -115,7 +116,7 @@ public class PrimeNumberChecker {
@GET
@Path("/{number}")
@Produces("text/plain")
@Produces(MediaType.TEXT_PLAIN)
@Counted(name = "performedChecks", description = "How many primality checks have been performed.")
@Timed(name = "checksTimer", description = "A measure of how long it takes to perform the primality test.", unit = MetricUnits.MILLISECONDS)
public String checkIfPrime(@PathParam long number) {
Expand Down
2 changes: 0 additions & 2 deletions docs/src/main/asciidoc/mongodb.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,6 @@ Now, edit the `org.acme.mongodb.FruitResource` class as follows:
[source,java]
----
@Path("/fruits")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class FruitResource {
@Inject FruitService fruitService;
Expand Down
5 changes: 0 additions & 5 deletions docs/src/main/asciidoc/neo4j.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -200,16 +200,11 @@ Add a `FruitResource` skeleton like this and `@Inject` a `org.neo4j.driver.Drive
package org.acme.neo4j;
import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.neo4j.driver.Driver;
@Path("fruits")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class FruitResource {
@Inject
Expand Down
5 changes: 0 additions & 5 deletions docs/src/main/asciidoc/openapi-swaggerui.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,11 @@ import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.DELETE;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.Consumes;
import javax.ws.rs.core.MediaType;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Set;
@Path("/fruits")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class FruitResource {
private Set<Fruit> fruits = Collections.newSetFromMap(Collections.synchronizedMap(new LinkedHashMap<>()));
Expand Down
5 changes: 0 additions & 5 deletions docs/src/main/asciidoc/optaplanner.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -612,19 +612,14 @@ package org.acme.optaplanner.rest;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.acme.optaplanner.domain.TimeTable;
import org.optaplanner.core.api.solver.SolverJob;
import org.optaplanner.core.api.solver.SolverManager;
@Path("/timeTable")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class TimeTableResource {
@Inject
Expand Down
2 changes: 1 addition & 1 deletion docs/src/main/asciidoc/performance-measure.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public class GreetingEndpoint {

@GET
@Path("/greeting")
@Produces("application/json")
@Produces(MediaType.APPLICATION_JSON)
public Greeting greeting(@QueryParam("name") String name) {
System.out.println(new SimpleDateFormat("HH:mm:ss.SSS").format(new java.util.Date(System.currentTimeMillis())));
String suffix = name != null ? name : "World";
Expand Down
1 change: 0 additions & 1 deletion docs/src/main/asciidoc/quartz.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,6 @@ import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/tasks")
@Produces(MediaType.APPLICATION_JSON)
public class TaskResource {
@GET
Expand Down
2 changes: 0 additions & 2 deletions docs/src/main/asciidoc/reactive-sql-clients.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,6 @@ With that you may create your `FruitResource` skeleton and `@Inject` a `io.vertx
.src/main/java/org/acme/vertx/FruitResource.java
----
@Path("fruits")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class FruitResource {
@Inject
Expand Down
5 changes: 0 additions & 5 deletions docs/src/main/asciidoc/redis.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -200,19 +200,14 @@ import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.PathParam;
import javax.ws.rs.PUT;
import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
import javax.ws.rs.Path;
import javax.ws.rs.POST;
import javax.ws.rs.DELETE;
import javax.ws.rs.core.MediaType;
import java.util.List;
import io.smallrye.mutiny.Uni;
@Path("/increments")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class IncrementResource {
@Inject
Expand Down
17 changes: 7 additions & 10 deletions docs/src/main/asciidoc/rest-json.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -126,17 +126,12 @@ import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Set;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/fruits")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class FruitResource {
private Set<Fruit> fruits = Collections.newSetFromMap(Collections.synchronizedMap(new LinkedHashMap<>()));
Expand Down Expand Up @@ -172,9 +167,13 @@ depending on the extension you chose when initializing the project.

[NOTE]
====
While RESTEasy supports auto-negotiation, when using Quarkus, it is very important to define the `@Produces` and `@Consumes` annotations.
They are analyzed at build time and Quarkus restricts the number of JAX-RS providers included in the native executable to the minimum required by the application.
It allows to reduce the size of the native executable.
When JSON extension is installed such as `quarkus-resteasy-jsonb` Quarkus will use this by default for most return values, unless the media type is explicitly set via
and `@Produces` or `@Consumes` annotation (there are some exceptions for well known types, such as `String` and `File`, which default to `text/plain` and `application/octet-stream`
respectively).
If you don't want JSON my default you can set `quarkus.resteasy-json.default-json=false` and the default will change back to `application/octet-stream`. If you set this
you will need to add `@Produces(MediaType.APPLICATION_JSON)` and `@Consumes(MediaType.APPLICATION_JSON)` to your endpoints in order to use JSON.
====

=== Configuring JSON support
Expand Down Expand Up @@ -266,7 +265,6 @@ When you have the following REST method, Quarkus determines that `Fruit` will be
[source,JAVA]
----
@GET
@Produces("application/json")
public List<Fruit> list() {
// ...
}
Expand All @@ -286,7 +284,6 @@ Your REST method then looks like this:
[source,JAVA]
----
@GET
@Produces("application/json")
public Response list() {
// ...
}
Expand Down
Loading

0 comments on commit 18456a9

Please sign in to comment.