{"id":910,"date":"2021-10-26T17:24:12","date_gmt":"2021-10-26T15:24:12","guid":{"rendered":"https:\/\/ondro.inginea.eu\/?p=910"},"modified":"2023-03-18T11:27:14","modified_gmt":"2023-03-18T10:27:14","slug":"portable-cdi-injection-into-jax-rs-sub-resources","status":"publish","type":"post","link":"https:\/\/ondro.inginea.eu\/index.php\/portable-cdi-injection-into-jax-rs-sub-resources\/","title":{"rendered":"How to properly inject CDI beans into JAX-RS sub-resources"},"content":{"rendered":"\n<p>Jakarta REST (JAX-RS) defines it&#8217;s own dependency injection using the <a href=\"https:\/\/jakarta.ee\/specifications\/platform\/8\/apidocs\/javax\/ws\/rs\/core\/Context.html\" target=\"_blank\" rel=\"noreferrer noopener\"><code>@Context<\/code><\/a> annotation. REST resources also support <a href=\"https:\/\/jakarta.ee\/specifications\/cdi\/\" target=\"_blank\" rel=\"noreferrer noopener\"><code>CDI injection<\/code><\/a> if you enable CDI on the REST resource class (e.g. using a bean-defining annotation like <a href=\"https:\/\/jakarta.ee\/specifications\/platform\/8\/apidocs\/javax\/enterprise\/context\/requestscoped\" target=\"_blank\" rel=\"noreferrer noopener\"><code>@RequestScoped<\/code><\/a>). <\/p>\n\n\n\n<p>But injection doesn&#8217;t work out of the box on JAX-RS <a href=\"https:\/\/jakarta.ee\/specifications\/restful-ws\/3.0\/jakarta-restful-ws-spec-3.0.html#sub_resources\" target=\"_blank\" rel=\"noreferrer noopener\">sub-resources<\/a>. How to create sub-resources so that both injection mechanisms work also in sub-resources? I&#8217;ll show you, it&#8217;s very easy.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">How to do it (for the impatient)<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Inject the sub-resource into the JAX-RS resource via the <code>@Inject<\/code> annotation<\/li>\n\n\n\n<li>Initialize the sub-resource using the <a rel=\"noreferrer noopener\" href=\"https:\/\/jakarta.ee\/specifications\/platform\/8\/apidocs\/javax\/ws\/rs\/container\/ResourceContext.html\" target=\"_blank\">ResourceContext<\/a> object injected via the <code>@Context<\/code> annotation<\/li>\n<\/ul>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-keyword\">@Path<\/span>(\"request\")\n@RequestScoped\npublic class RestResource {\n\n    <span class=\"hljs-keyword\">@Inject<\/span> \/\/ inject subresource as CDI bean\n    SubResource subResource;\n    \n    <span class=\"hljs-keyword\">@Context<\/span> \/\/ inject from JAX-RS container\n    ResourceContext resourceContext;\n    \n    <span class=\"hljs-keyword\">@Path<\/span>(\"subresource\")\n    public SubResource getSubresource() {\n        <span class=\"hljs-selector-tag\">return<\/span> <span class=\"hljs-selector-tag\">resourceContext<\/span><span class=\"hljs-selector-class\">.initResource<\/span>(<span class=\"hljs-selector-tag\">subResource<\/span>);\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<div class=\"wp-block-group omnifish-advert has-white-color has-text-color has-background\" style=\"background-color:#0b8f9b\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<div class=\"wp-block-media-text alignwide has-media-on-the-right is-stacked-on-mobile is-vertically-aligned-center has-white-color has-text-color\" style=\"grid-template-columns:auto 15%\"><div class=\"wp-block-media-text__content\">\n<p class=\"has-medium-font-size\"><a rel=\"noreferrer noopener\" href=\"https:\/\/omnifish.ee\" data-type=\"URL\" data-id=\"https:\/\/omnifish.ee\" target=\"_blank\"><strong>Need help with GlassFish, Jakarta\u00a0EE, Java in the cloud?<\/strong><\/a><\/p>\n\n\n\n<p><a rel=\"noreferrer noopener\" href=\"https:\/\/omnifish.ee\" data-type=\"URL\" data-id=\"https:\/\/omnifish.ee\" target=\"_blank\">Follow me at <strong>OmniFish<\/strong>! <\/a><\/p>\n<\/div><figure class=\"wp-block-media-text__media\"><a href=\"https:\/\/omnifish.ee\"><img loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"178\" data-attachment-id=\"1034\" data-permalink=\"https:\/\/ondro.inginea.eu\/omnifish-logo-transparent-400px\/\" data-orig-file=\"https:\/\/i0.wp.com\/ondro.inginea.eu\/wp-content\/uploads\/2022\/07\/omnifish-logo-transparent-400px.png?fit=400%2C237&amp;ssl=1\" data-orig-size=\"400,237\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"omnifish-logo-transparent-400px\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/ondro.inginea.eu\/wp-content\/uploads\/2022\/07\/omnifish-logo-transparent-400px.png?fit=300%2C178&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/ondro.inginea.eu\/wp-content\/uploads\/2022\/07\/omnifish-logo-transparent-400px.png?fit=400%2C237&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/ondro.inginea.eu\/wp-content\/uploads\/2022\/07\/omnifish-logo-transparent-400px.png?resize=300%2C178&#038;ssl=1\" alt=\"\" class=\"wp-image-1034 size-medium\" srcset=\"https:\/\/i0.wp.com\/ondro.inginea.eu\/wp-content\/uploads\/2022\/07\/omnifish-logo-transparent-400px.png?resize=300%2C178&amp;ssl=1 300w, https:\/\/i0.wp.com\/ondro.inginea.eu\/wp-content\/uploads\/2022\/07\/omnifish-logo-transparent-400px.png?w=400&amp;ssl=1 400w\" sizes=\"(max-width: 300px) 100vw, 300px\" data-recalc-dims=\"1\" \/><\/a><\/figure><\/div>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Full story<\/h2>\n\n\n\n<p>First, let&#8217;s briefly explain what a sub-resource is. It&#8217;s a class that looks similar to a usual JAX-RS resource but it&#8217;s not used standalone. Instead, an instance of this class can be returned from a JAX-RS resource to map it to a subpath of that resource. Therefore a JAX-RS resource can delegate processing of a specific subpath to another class. Sub resources look like any other JAX-RS resources but don&#8217;t specify the <code><a rel=\"noreferrer noopener\" href=\"https:\/\/jakarta.ee\/specifications\/platform\/8\/apidocs\/javax\/ws\/rs\/Path.html\" target=\"_blank\">@Path<\/a><\/code> annotation. The path is defined on the resource method that returns the sub resource:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\">\/\/ Sub-resource - no @Path annotation on the class<\/span>\n@RequestScoped\n@Produces(MediaType.TEXT_PLAIN)\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">SubResource<\/span> <\/span>{\n    \n    @GET\n    <span class=\"hljs-keyword\">public<\/span> String getMessage() {\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">\"This is a sub-resource.\"<\/span>;\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">@Path(<span class=\"hljs-string\">\"request\"<\/span>) <span class=\"hljs-comment\">\/\/ defines the path \"request\" for this resource<\/span>\n@RequestScoped\n@Produces(MediaType.TEXT_PLAIN)\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">RestResource<\/span> <\/span>{\n\n    @GET\n    <span class=\"hljs-keyword\">public<\/span> String getMessage() {\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">\"This is a JAX-RS resource.\"<\/span>;\n    }\n\n    @Path(<span class=\"hljs-string\">\"subresource\"<\/span>) <span class=\"hljs-comment\">\/\/ defines the subpath for the sub-resource: \"request\/subresource\"<\/span>\n    <span class=\"hljs-keyword\">public<\/span> SubResource getSubresource() {\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">new<\/span> SubResource();\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If you access path <code>\/request<\/code>, the response will contain &#8220;This is a JAX-RS resource.&#8221;<\/p>\n\n\n\n<p>If you access path <code>\/request\/subresource<\/code>, the response will contain &#8220;This is a sub-resource.&#8221;<\/p>\n\n\n\n<p>However, the catch is that with the simple example above, <strong>no injection works in the subresource<\/strong>. it&#8217;s not possible to inject anything into the SubResource class, any atempt to do so will result in <code>null<\/code> values for injected fields. It&#8217;s created as a plain Java object in the <code>getSubresource()<\/code> method and thus it&#8217;s not managed by any container. The <code><a href=\"https:\/\/jakarta.ee\/specifications\/platform\/8\/apidocs\/javax\/enterprise\/context\/RequestScoped.html\" target=\"_blank\" rel=\"noreferrer noopener\">@RequestScoped<\/a><\/code> annotation is ignored in this case and anything marked with the <code>@Inject<\/code> or <code>@Context<\/code> annotation would be also ignored and remain <code>null<\/code>.<\/p>\n\n\n\n<p>Injection works on JAX-RS resources because they are created by the JAX-RS container and not using the <code>new<\/code> keyword. If CDI is also enabled on the resource class, Jakarta EE runtime will first create the JAX-RS resource as a CDI bean and then pass it to the JAX-RS container, which then does its own injection. <\/p>\n\n\n\n<p>None of this happens if a sub resource is created using <code>new<\/code>, therefore we must create it in a different way.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Solution<\/h2>\n\n\n\n<p>2 simple steps are needed to add support for both types of injection in JAX-RS sub-resources:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Inject the sub-resource into the JAX-RS resource. This enables the CDI injection<\/li>\n\n\n\n<li>Initialize the sub-resource using the <a href=\"https:\/\/jakarta.ee\/specifications\/platform\/8\/apidocs\/javax\/ws\/rs\/container\/ResourceContext.html\" target=\"_blank\" rel=\"noreferrer noopener\">ResourceContext<\/a> object provided by the JAX-RS container. This enables JAX-RS injection for values annotated with <code>@Context<\/code><\/li>\n<\/ul>\n\n\n\n<p>This is how the <code>RestResource<\/code> class should look like to create the sub-resource properly:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">@Path(<span class=\"hljs-string\">\"request\"<\/span>)\n@RequestScoped\n@Produces(MediaType.TEXT_PLAIN)\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">RestResource<\/span> <\/span>{\n\n    @Inject <span class=\"hljs-comment\">\/\/ inject subresource as CDI bean<\/span>\n    SubResource subResource;\n    \n    @Context <span class=\"hljs-comment\">\/\/ inject from JAX-RS container<\/span>\n    ResourceContext resourceContext;\n    \n    @GET\n    <span class=\"hljs-keyword\">public<\/span> String getMessage() {\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">\"This is a JAX-RS resource.\"<\/span>;\n    }\n\n    @Path(<span class=\"hljs-string\">\"subresource\"<\/span>)\n    <span class=\"hljs-keyword\">public<\/span> SubResource getSubresource() {\n        <span class=\"hljs-keyword\">return<\/span> resourceContext.initResource(subResource);\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>NOTE: You need to use the <code>initResource<\/code> method of <code>ResourceContext<\/code> and not the <code>getResource<\/code> method. The <code>getResource<\/code> method creates a new JAX-RS sub-resource from a class but it&#8217;s not guaranteed that it also enables CDI injection for it. Although some Jakarta EE runtimes will enable CDI injection if you call the <code>getResource<\/code> method, it&#8217;s known that some of them like <a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/OpenLiberty\/open-liberty\/issues\/7422\" target=\"_blank\">OpenLiberty<\/a> and <a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/payara\/Payara\/issues\/5467\" target=\"_blank\">Payara<\/a> don&#8217;t do it. In the future, this will very probably be improved when the @Context injection will be replaced by CDI injection, which is <a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/eclipse-ee4j\/jaxrs-api\/issues\/951\" target=\"_blank\">already planned<\/a>.<\/p>\n<\/blockquote>\n\n\n\n<p>Now you can use both types of injection and all will work as expected:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-keyword\">@RequestScoped<\/span>\n@Produces(MediaType.TEXT_PLAIN)\npublic class SubResource {\n\n    <span class=\"hljs-keyword\">@Inject<\/span>\n    SomeCdiBean bean;\n\n    <span class=\"hljs-keyword\">@Context<\/span>\n    UriInfo uriInfo\n    \n    @GET\n    public String getMessage() {\n        <span class=\"hljs-selector-tag\">return<\/span> <span class=\"hljs-selector-tag\">bean<\/span><span class=\"hljs-selector-class\">.getMessage<\/span>() + \", <span class=\"hljs-selector-tag\">path<\/span>: \" + <span class=\"hljs-selector-tag\">uriInfo<\/span><span class=\"hljs-selector-class\">.getPath<\/span>();\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Jakarta REST (JAX-RS) defines it&#8217;s own dependency injection using the @Context annotation. REST resources also support CDI injection if you enable CDI on the REST resource class (e.g. using a bean-defining annotation like @RequestScoped). But injection doesn&#8217;t work out of the box on JAX-RS sub-resources. How to create sub-resources so that both injection mechanisms work [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","footnotes":"","jetpack_publicize_message":"Here's my article how to properly inject CDI beans into a JAX-RS sub-resource (and avoid confusing deployment errors if you do it incorrectly)","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[196],"tags":[],"series":[],"class_list":["post-910","post","type-post","status-publish","format-standard","hentry","category-ee-rest"],"jetpack_publicize_connections":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to properly inject CDI beans into JAX-RS sub-resources - .Lost in Coding<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/ondro.inginea.eu\/index.php\/portable-cdi-injection-into-jax-rs-sub-resources\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to properly inject CDI beans into JAX-RS sub-resources - .Lost in Coding\" \/>\n<meta property=\"og:description\" content=\"Jakarta REST (JAX-RS) defines it&#8217;s own dependency injection using the @Context annotation. REST resources also support CDI injection if you enable CDI on the REST resource class (e.g. using a bean-defining annotation like @RequestScoped). But injection doesn&#8217;t work out of the box on JAX-RS sub-resources. How to create sub-resources so that both injection mechanisms work [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/ondro.inginea.eu\/index.php\/portable-cdi-injection-into-jax-rs-sub-resources\/\" \/>\n<meta property=\"og:site_name\" content=\".Lost in Coding\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/ondrej.mihalyi\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/ondrej.mihalyi\" \/>\n<meta property=\"article:published_time\" content=\"2021-10-26T15:24:12+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-03-18T10:27:14+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/i0.wp.com\/ondro.inginea.eu\/wp-content\/uploads\/2021\/09\/duke-CloudSurf-small-e1637277171314.png?fit=300%2C202&ssl=1\" \/>\n\t<meta property=\"og:image:width\" content=\"300\" \/>\n\t<meta property=\"og:image:height\" content=\"202\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Ondro Mih\u00e1lyi\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/OndroMih\" \/>\n<meta name=\"twitter:site\" content=\"@OndroMih\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ondro Mih\u00e1lyi\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/ondro.inginea.eu\/index.php\/portable-cdi-injection-into-jax-rs-sub-resources\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/ondro.inginea.eu\/index.php\/portable-cdi-injection-into-jax-rs-sub-resources\/\"},\"author\":{\"name\":\"Ondro Mih\u00e1lyi\",\"@id\":\"https:\/\/ondro.inginea.eu\/#\/schema\/person\/07ac1158ec74720744f7146572215616\"},\"headline\":\"How to properly inject CDI beans into JAX-RS sub-resources\",\"datePublished\":\"2021-10-26T15:24:12+00:00\",\"dateModified\":\"2023-03-18T10:27:14+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/ondro.inginea.eu\/index.php\/portable-cdi-injection-into-jax-rs-sub-resources\/\"},\"wordCount\":534,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/ondro.inginea.eu\/#\/schema\/person\/07ac1158ec74720744f7146572215616\"},\"articleSection\":[\"REST\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/ondro.inginea.eu\/index.php\/portable-cdi-injection-into-jax-rs-sub-resources\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/ondro.inginea.eu\/index.php\/portable-cdi-injection-into-jax-rs-sub-resources\/\",\"url\":\"https:\/\/ondro.inginea.eu\/index.php\/portable-cdi-injection-into-jax-rs-sub-resources\/\",\"name\":\"How to properly inject CDI beans into JAX-RS sub-resources - .Lost in Coding\",\"isPartOf\":{\"@id\":\"https:\/\/ondro.inginea.eu\/#website\"},\"datePublished\":\"2021-10-26T15:24:12+00:00\",\"dateModified\":\"2023-03-18T10:27:14+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/ondro.inginea.eu\/index.php\/portable-cdi-injection-into-jax-rs-sub-resources\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/ondro.inginea.eu\/index.php\/portable-cdi-injection-into-jax-rs-sub-resources\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/ondro.inginea.eu\/index.php\/portable-cdi-injection-into-jax-rs-sub-resources\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/ondro.inginea.eu\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to properly inject CDI beans into JAX-RS sub-resources\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/ondro.inginea.eu\/#website\",\"url\":\"https:\/\/ondro.inginea.eu\/\",\"name\":\".Lost in Coding\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/ondro.inginea.eu\/#\/schema\/person\/07ac1158ec74720744f7146572215616\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/ondro.inginea.eu\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/ondro.inginea.eu\/#\/schema\/person\/07ac1158ec74720744f7146572215616\",\"name\":\"Ondro Mih\u00e1lyi\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/ondro.inginea.eu\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/i2.wp.com\/ondro.inginea.eu\/wp-content\/uploads\/2017\/08\/fotoOMsquare3x300.jpg?fit=300%2C300&ssl=1\",\"contentUrl\":\"https:\/\/i2.wp.com\/ondro.inginea.eu\/wp-content\/uploads\/2017\/08\/fotoOMsquare3x300.jpg?fit=300%2C300&ssl=1\",\"width\":300,\"height\":300,\"caption\":\"Ondro Mih\u00e1lyi\"},\"logo\":{\"@id\":\"https:\/\/ondro.inginea.eu\/#\/schema\/person\/image\/\"},\"description\":\"Ondro is a software developer and consultant specializing in combining standard and proven tools to solve new and challenging problems. He's been developing in Java for over 10 years. He\u2019s worked for clients like Payara, LottoLand, Uniqa and others. He has co-founded OmniFish, where he works as a director and Jakarta EE expert. He\u2019s passionate about helping his clients and the wider Java community with their projects based on Jakarta EE and similar technologies. As an experienced Java developer and instructor, he's helped companies build and educate their development teams and improve their development processes. He's a core member of several opensource projects and Jakarta EE specification projects. He\u2019s a frequent conference speaker, leader of the Czech JUG and a Java Champion. Ondro is a regular conference speaker at international conferences. Since 2016, he's presented at the following conferences: \u2022 FOSDEM, Brussels, Belgium 2023) \u2022 JChampionsConf, Online (year 2023) \u2022 EclipseCon, Germany (year 2022) \u2022 GeeCon, Prague, Czechia (years 2016, 2019, 2022) \u2022 JavaLand, Bruehl, Germany (years 2018, 2021) \u2022 JFokus, Stockholm, Sweden (year 2019) \u2022 Devops Con, Munich, Germany (year 2019) \u2022 Oracle CodeOne, San Francisco, USA (years 2018, 2019) \u2022 Devoxx, Antwerp, Belgium (year 2018) \u2022 JPrime, Sofia, Bulgaria (years 2017, 2018) \u2022 Java2Days, Sofia, Bulgaria (years 2016, 2018) \u2022 EclipseCon, France (year 2018) \u2022 JavaOne, San Francisco, USA (years 2016, 2017) \u2022 Oracle Code, Prague, Czechia (year 2017) \u2022 Devoxx, London, UK (year 2017) \u2022 GeeCon, Krakow, Poland (year 2017) \u2022 W-JAX, Munich, Germany (years 2016, 2017) \u2022 Bed-Con, Berlin, Germany (year 2017) \u2022 Oredev, Malmo, Sweden (year 2017) \u2022 Devoxx, Casablanca, Morocco (year 2017) \u2022 Java Developer Days, Krakow, Poland (year 2016)\",\"sameAs\":[\"https:\/\/www.facebook.com\/ondrej.mihalyi\",\"https:\/\/cz.linkedin.com\/in\/mihalyiondrej\",\"https:\/\/twitter.com\/https:\/\/twitter.com\/OndroMih\"],\"url\":\"https:\/\/ondro.inginea.eu\/index.php\/author\/ondrejm\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to properly inject CDI beans into JAX-RS sub-resources - .Lost in Coding","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/ondro.inginea.eu\/index.php\/portable-cdi-injection-into-jax-rs-sub-resources\/","og_locale":"en_US","og_type":"article","og_title":"How to properly inject CDI beans into JAX-RS sub-resources - .Lost in Coding","og_description":"Jakarta REST (JAX-RS) defines it&#8217;s own dependency injection using the @Context annotation. REST resources also support CDI injection if you enable CDI on the REST resource class (e.g. using a bean-defining annotation like @RequestScoped). But injection doesn&#8217;t work out of the box on JAX-RS sub-resources. How to create sub-resources so that both injection mechanisms work [&hellip;]","og_url":"https:\/\/ondro.inginea.eu\/index.php\/portable-cdi-injection-into-jax-rs-sub-resources\/","og_site_name":".Lost in Coding","article_publisher":"https:\/\/www.facebook.com\/ondrej.mihalyi","article_author":"https:\/\/www.facebook.com\/ondrej.mihalyi","article_published_time":"2021-10-26T15:24:12+00:00","article_modified_time":"2023-03-18T10:27:14+00:00","og_image":[{"width":300,"height":202,"url":"https:\/\/i0.wp.com\/ondro.inginea.eu\/wp-content\/uploads\/2021\/09\/duke-CloudSurf-small-e1637277171314.png?fit=300%2C202&ssl=1","type":"image\/png"}],"author":"Ondro Mih\u00e1lyi","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/OndroMih","twitter_site":"@OndroMih","twitter_misc":{"Written by":"Ondro Mih\u00e1lyi","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/ondro.inginea.eu\/index.php\/portable-cdi-injection-into-jax-rs-sub-resources\/#article","isPartOf":{"@id":"https:\/\/ondro.inginea.eu\/index.php\/portable-cdi-injection-into-jax-rs-sub-resources\/"},"author":{"name":"Ondro Mih\u00e1lyi","@id":"https:\/\/ondro.inginea.eu\/#\/schema\/person\/07ac1158ec74720744f7146572215616"},"headline":"How to properly inject CDI beans into JAX-RS sub-resources","datePublished":"2021-10-26T15:24:12+00:00","dateModified":"2023-03-18T10:27:14+00:00","mainEntityOfPage":{"@id":"https:\/\/ondro.inginea.eu\/index.php\/portable-cdi-injection-into-jax-rs-sub-resources\/"},"wordCount":534,"commentCount":0,"publisher":{"@id":"https:\/\/ondro.inginea.eu\/#\/schema\/person\/07ac1158ec74720744f7146572215616"},"articleSection":["REST"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/ondro.inginea.eu\/index.php\/portable-cdi-injection-into-jax-rs-sub-resources\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/ondro.inginea.eu\/index.php\/portable-cdi-injection-into-jax-rs-sub-resources\/","url":"https:\/\/ondro.inginea.eu\/index.php\/portable-cdi-injection-into-jax-rs-sub-resources\/","name":"How to properly inject CDI beans into JAX-RS sub-resources - .Lost in Coding","isPartOf":{"@id":"https:\/\/ondro.inginea.eu\/#website"},"datePublished":"2021-10-26T15:24:12+00:00","dateModified":"2023-03-18T10:27:14+00:00","breadcrumb":{"@id":"https:\/\/ondro.inginea.eu\/index.php\/portable-cdi-injection-into-jax-rs-sub-resources\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/ondro.inginea.eu\/index.php\/portable-cdi-injection-into-jax-rs-sub-resources\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/ondro.inginea.eu\/index.php\/portable-cdi-injection-into-jax-rs-sub-resources\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/ondro.inginea.eu\/"},{"@type":"ListItem","position":2,"name":"How to properly inject CDI beans into JAX-RS sub-resources"}]},{"@type":"WebSite","@id":"https:\/\/ondro.inginea.eu\/#website","url":"https:\/\/ondro.inginea.eu\/","name":".Lost in Coding","description":"","publisher":{"@id":"https:\/\/ondro.inginea.eu\/#\/schema\/person\/07ac1158ec74720744f7146572215616"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/ondro.inginea.eu\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/ondro.inginea.eu\/#\/schema\/person\/07ac1158ec74720744f7146572215616","name":"Ondro Mih\u00e1lyi","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ondro.inginea.eu\/#\/schema\/person\/image\/","url":"https:\/\/i2.wp.com\/ondro.inginea.eu\/wp-content\/uploads\/2017\/08\/fotoOMsquare3x300.jpg?fit=300%2C300&ssl=1","contentUrl":"https:\/\/i2.wp.com\/ondro.inginea.eu\/wp-content\/uploads\/2017\/08\/fotoOMsquare3x300.jpg?fit=300%2C300&ssl=1","width":300,"height":300,"caption":"Ondro Mih\u00e1lyi"},"logo":{"@id":"https:\/\/ondro.inginea.eu\/#\/schema\/person\/image\/"},"description":"Ondro is a software developer and consultant specializing in combining standard and proven tools to solve new and challenging problems. He's been developing in Java for over 10 years. He\u2019s worked for clients like Payara, LottoLand, Uniqa and others. He has co-founded OmniFish, where he works as a director and Jakarta EE expert. He\u2019s passionate about helping his clients and the wider Java community with their projects based on Jakarta EE and similar technologies. As an experienced Java developer and instructor, he's helped companies build and educate their development teams and improve their development processes. He's a core member of several opensource projects and Jakarta EE specification projects. He\u2019s a frequent conference speaker, leader of the Czech JUG and a Java Champion. Ondro is a regular conference speaker at international conferences. Since 2016, he's presented at the following conferences: \u2022 FOSDEM, Brussels, Belgium 2023) \u2022 JChampionsConf, Online (year 2023) \u2022 EclipseCon, Germany (year 2022) \u2022 GeeCon, Prague, Czechia (years 2016, 2019, 2022) \u2022 JavaLand, Bruehl, Germany (years 2018, 2021) \u2022 JFokus, Stockholm, Sweden (year 2019) \u2022 Devops Con, Munich, Germany (year 2019) \u2022 Oracle CodeOne, San Francisco, USA (years 2018, 2019) \u2022 Devoxx, Antwerp, Belgium (year 2018) \u2022 JPrime, Sofia, Bulgaria (years 2017, 2018) \u2022 Java2Days, Sofia, Bulgaria (years 2016, 2018) \u2022 EclipseCon, France (year 2018) \u2022 JavaOne, San Francisco, USA (years 2016, 2017) \u2022 Oracle Code, Prague, Czechia (year 2017) \u2022 Devoxx, London, UK (year 2017) \u2022 GeeCon, Krakow, Poland (year 2017) \u2022 W-JAX, Munich, Germany (years 2016, 2017) \u2022 Bed-Con, Berlin, Germany (year 2017) \u2022 Oredev, Malmo, Sweden (year 2017) \u2022 Devoxx, Casablanca, Morocco (year 2017) \u2022 Java Developer Days, Krakow, Poland (year 2016)","sameAs":["https:\/\/www.facebook.com\/ondrej.mihalyi","https:\/\/cz.linkedin.com\/in\/mihalyiondrej","https:\/\/twitter.com\/https:\/\/twitter.com\/OndroMih"],"url":"https:\/\/ondro.inginea.eu\/index.php\/author\/ondrejm\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p6wlb6-eG","jetpack-related-posts":[{"id":209,"url":"https:\/\/ondro.inginea.eu\/index.php\/mvc-1-0-in-java-ee-8-getting-started-using-facelets\/","url_meta":{"origin":910,"position":0},"title":"MVC 1.0 in Java EE 8: Getting started using facelets","author":"Ondro Mih\u00e1lyi","date":"19 January, 2016","format":false,"excerpt":"MVC 1.0 is an action-based Model-View-Controller web framework, which will be a part of future Java EE 8. It will live side by side with component-based JSF framework and will provide an alternative for building HTML+javascript oriented applications with full control over URLs. This post summarizes what needs to be\u2026","rel":"","context":"In &quot;Jakarta EE&quot;","block_context":{"text":"Jakarta EE","link":"https:\/\/ondro.inginea.eu\/index.php\/category\/java\/jakarta-ee\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":4,"url":"https:\/\/ondro.inginea.eu\/index.php\/cdi-events-in-swing-application-to-decouple-ui-and-event-handling\/","url_meta":{"origin":910,"position":1},"title":"CDI events in Swing application to decouple UI and event handling","author":"Ondro Mih\u00e1lyi","date":"25 January, 2015","format":false,"excerpt":"After having the pleasure of building my code around CDI for couple of years, it feels very natural to use it to structure my code according to well-known patterns. CDI is a dependency injection mechanism designed to be used within Java EE application servers, and this could be perceived as\u2026","rel":"","context":"In &quot;Java&quot;","block_context":{"text":"Java","link":"https:\/\/ondro.inginea.eu\/index.php\/category\/java\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/2.bp.blogspot.com\/-PuFbB4sHXhs\/VMQ5UIZaxZI\/AAAAAAAABs8\/dcaN9GhcPlU\/s1600\/Swing-CDI-MVC.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":931,"url":"https:\/\/ondro.inginea.eu\/index.php\/get-user-info-in-jakarta-ee\/","url_meta":{"origin":910,"position":2},"title":"Get logged-in user info in Jakarta EE &#8211; the simplest way","author":"Ondro Mih\u00e1lyi","date":"13 November, 2021","format":false,"excerpt":"The security before Java EE 8 \/ Jakarta EE 8 used to be a bit complicated and confusing. Every specification provided its own way to retrieve information about the logged-in user. The situation greatly improved with the introduction of the Security API that provides a unified way to do that\u2026","rel":"","context":"In &quot;Security&quot;","block_context":{"text":"Security","link":"https:\/\/ondro.inginea.eu\/index.php\/category\/java\/jakarta-ee\/ee-security\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":678,"url":"https:\/\/ondro.inginea.eu\/index.php\/speed-up-services-with-reactive-api-in-java-ee-8\/","url_meta":{"origin":910,"position":3},"title":"Speed Up Services With Reactive API in Java EE 8","author":"Ondro Mih\u00e1lyi","date":"27 June, 2018","format":false,"excerpt":"Services can often be optimized with asynchronous processing even without changing their behavior towards the outside world. The reason why some services aren't efficient is that they need to wait for other services to provide a result to continue further. Let's look how to call external REST services without waiting\u2026","rel":"","context":"In &quot;REST&quot;","block_context":{"text":"REST","link":"https:\/\/ondro.inginea.eu\/index.php\/category\/java\/jakarta-ee\/ee-rest\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":806,"url":"https:\/\/ondro.inginea.eu\/index.php\/possible-ways-to-use-arquillian-in-jakarta-ee-tcks\/","url_meta":{"origin":910,"position":4},"title":"Possible ways to use Arquillian in Jakarta EE TCKs","author":"Ondro Mih\u00e1lyi","date":"12 April, 2020","format":false,"excerpt":"Recently, we had a discussion how to create a standalone Jakarta Batch test kit (TCK). For most of the committers, it's pretty natural to use Arquillian to abstracts tests away from how they are executed on an implementation. But Romain proposed an intriguing idea to use plain JUnit5 that got\u2026","rel":"","context":"In &quot;Jakarta EE&quot;","block_context":{"text":"Jakarta EE","link":"https:\/\/ondro.inginea.eu\/index.php\/category\/java\/jakarta-ee\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":769,"url":"https:\/\/ondro.inginea.eu\/index.php\/new-features-in-java-versions-since-java-8\/","url_meta":{"origin":910,"position":5},"title":"New features between Java 8 and Java 19","author":"Ondro Mih\u00e1lyi","date":"8 October, 2022","format":false,"excerpt":"Since version 9, Java has new features every 6 months and it's very hard to keep track of these new changes. Most of the information on the internet describes changes between the last 2 Java versions. However, if you're in a similar situation as me, you're not using one of\u2026","rel":"","context":"In &quot;Java&quot;","block_context":{"text":"Java","link":"https:\/\/ondro.inginea.eu\/index.php\/category\/java\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/ondro.inginea.eu\/wp-content\/uploads\/2021\/09\/duke-CloudSurf-small-e1637277171314.png?fit=300%2C202&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/ondro.inginea.eu\/index.php\/wp-json\/wp\/v2\/posts\/910"}],"collection":[{"href":"https:\/\/ondro.inginea.eu\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ondro.inginea.eu\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ondro.inginea.eu\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ondro.inginea.eu\/index.php\/wp-json\/wp\/v2\/comments?post=910"}],"version-history":[{"count":16,"href":"https:\/\/ondro.inginea.eu\/index.php\/wp-json\/wp\/v2\/posts\/910\/revisions"}],"predecessor-version":[{"id":1119,"href":"https:\/\/ondro.inginea.eu\/index.php\/wp-json\/wp\/v2\/posts\/910\/revisions\/1119"}],"wp:attachment":[{"href":"https:\/\/ondro.inginea.eu\/index.php\/wp-json\/wp\/v2\/media?parent=910"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ondro.inginea.eu\/index.php\/wp-json\/wp\/v2\/categories?post=910"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ondro.inginea.eu\/index.php\/wp-json\/wp\/v2\/tags?post=910"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/ondro.inginea.eu\/index.php\/wp-json\/wp\/v2\/series?post=910"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}