Content-Type: text/plain;charset=UTF-8
header, which seems wrong somehow for a GET request. Or use request.setCharacterEncoding()
. But that method's javadoc also states it is to be used with POST requests. The third option is -Dclient.encoding.override=UTF-8
. Which of those three options is a sane choice?
Hi @jwcarman - hopefully you saw my response in the MP Rest Client gitter too - putting on my Open Liberty hat... I think there are a few reasons why @Path
is required on the resource class:
1) it makes it clear that it is a root resource class
2) it is a bean-defining annotation when used with CDI
3) it avoids complexity when multiple inheritance (i.e. multiple interfaces) is in play
As mentioned in the other thread, you should only need to duplicate the @Path
annotation (or work around it by using baseUri
) - the method- and parameter-level annotations are inherited as per the spec.
@Inject
onto the ctor, but if I put it on the field, everything works just fine
@jwcarman that's another optional requirement in the spec:
The following additional requirements apply when using Managed Beans, CDI-style Beans or EJBs as resource classes, providers or Application subclasses:
...
Support for constructor injection of JAX-RS resources is OPTIONAL. Portable applications MUST instead use fields or bean properties in conjunction with a @PostConstruct annotated method. Im- plementations SHOULD warn users about use of non-portable constructor injection.
I think we have a feature request work item to support constructor injection - let me see if I can find it.
Yeah, I've seen a couple of discussions on the matter.
https://stackoverflow.com/questions/54685278/using-constructor-injection-with-cdi-in-openliberty
@Path("persons")
public class PersonResource {
private final PersonRepository repository;
public PersonResource() {
this(null);
}
@Inject
public PersonResource(PersonRepository repository) {
this.repository = repository;
}
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("{id}")
public Person getPerson(@PathParam("id") String id) {
return repository.findById(id);
}
}
@Dependent
does
@mdagit you may need to specify the content type in your response - something like:requestContext.abortWith(Response.status(Status.FORBIDDEN).entity(Entity.entity(Map.of("error", err), MediaType.APPLICATION_JSON)).build());
orrequestContext.abortWith(Response.status(Status.FORBIDDEN).header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML).entity(Map.of("error", err)).build());
I'm just randomly guessing at the content type you want - but ultimately, that's the issue - the runtime doesn't know what to serialize the map to.
type(...)
method on the ResponseBuilder
class that might be cleaner than the snippets I first suggested. ex: