Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
72ndAirhacksQ&A.md

Ask questions and see you at March, 2nd, 8.PM. CET: https://vimeo.com/event/19368

Also checkout recent episode:

71st airhacks.tv

Please keep the questions Jakarta EE-stic. Means: as short and as concise as only possible. Feel free to ask several, shorter questions.

@wassim-tech

This comment has been minimized.

Copy link

wassim-tech commented Feb 15, 2020

Hi Adam,

Thank you for your effort and time for trying to support the community (Thumbs up).

Is it possible to response with a json body and not with html when the response status-code isn't in the 2xx-range?
Is the behaviour Runtime/Implementation/App-Server specific to Jakarta EE 8 + MicroProfile 3.0?
Here is an illustration of an example: One can't create two entities in the data store with the same slug.

@Path("cafe")
@Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_JSON})
@Tag(name = "CafeResource")
public class CafeResource {

    @POST
    @Operation(description = "Creates a new cafe.", summary = "Create cafe")
    @APIResponse(
            description = "cafe successfully created",
            responseCode = "201",
            content = @Content(schema = @Schema(implementation = CafeWebModel.class))
    )
    @APIResponse(
        description = "When another cafe exists with the same slug, returns null",
        responseCode = "412",
        content = @Content(example = "null", schema = @Schema(nullable = true))
    )
    public Response create(@RequestBody CafeCreationModel cafeCreationModel) {
        Cafe entity = doSomeStuff(cafeCreationModel);
        // ...
        if (entity != null) {
            return Response.status(Response.Status.CREATED).entity(entity).build();
        } else {
            return Response.status(Response.Status.PRECONDITION_FAILED)
                .header(HEADER_X_REASON, "Cafe with same slug exists already.")
                .build();
        }
    }
}
# request nr. 1
$ curl -i -X POST "http://localhost:8080/cafe-manager-app/api/v1/cafes" \
        -H  "accept: application/json" \
        -H  "Content-Type: application/json" \
        -d "{\"visibleName\":\"Cafe AG\",\"slug\":\"cafe-ag\"}"

# response nr. 1 => ok as expected



# request nr. 2
$ curl -i -X POST "http://localhost:8080/cafe-manager-app/api/v1/cafes" \
        -H  "accept: application/json" \
        -H  "Content-Type: application/json" \
        -d "{\"visibleName\":\"Cafe AG\",\"slug\":\"cafe-ag\"}"

# response nr. 2 => not as expected (body is html)

HTTP/1.1 412 Precondition Failed
Server: Payara Server  5.194 #badassfish
X-Powered-By: Servlet/4.0 JSP/2.3 (Payara Server  5.194 #badassfish Java/Azul Systems, Inc./1.8)
X-Reason: Cafe with same slug exists already
Content-Language:
Content-Type: text/html
Content-Length: 1095
X-Frame-Options: SAMEORIGIN

<!DOCTYPE html PUB...>...</html>

---
# expected response nr. 2

HTTP/1.1 412 Precondition Failed
Server: Payara Server  5.194 #badassfish
X-Powered-By: Servlet/4.0 JSP/2.3 (Payara Server  5.194 #badassfish Java/Azul Systems, Inc./1.8)
X-Reason: Cafe with same slug exists already
Content-Language:
Content-Type: application/json
Content-Length: 1095
X-Frame-Options: SAMEORIGIN

null or {}

I would be happy to hear from you about this topic.

Kind Regards,
Wassim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.