{"id":9342,"date":"2014-04-09T14:43:01","date_gmt":"2014-04-09T19:43:01","guid":{"rendered":"http:\/\/jianmingli.com\/wp\/?p=9342"},"modified":"2015-12-16T16:26:26","modified_gmt":"2015-12-16T21:26:26","slug":"jdeveloper-restful-jpa-ejb3","status":"publish","type":"post","link":"https:\/\/jianmingli.com\/wp\/?p=9342","title":{"rendered":"JDeveloper RESTful JPA EJB3"},"content":{"rendered":"<div class='toc wptoc'>\n<h2>Contents<\/h2>\n<ol class='toc-odd level-1'>\n\t<li>\n\t\t<a href=\"#Create_Project\">Create Project<\/a>\n\t\t<ol class='toc-even level-2'>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Create_Jersey_User_Library\">Create Jersey User Library<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Create_Project_1\">Create Project<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Create_a_DB_Connection\">Create a DB Connection<\/a>\n\t\t\t<\/li>\n\t\t<\/ol>\n\t<li>\n\t\t<a href=\"#Create_Entity_Bean\">Create Entity Bean<\/a>\n\t<\/li>\n\t<li>\n\t\t<a href=\"#Create_EJB_Diagram\">Create EJB Diagram<\/a>\n\t<\/li>\n\t<li>\n\t\t<a href=\"#Create_Stateless_Session_Bean\">Create Stateless Session Bean<\/a>\n\t<\/li>\n\t<li>\n\t\t<a href=\"#Create_JAX-RS\">Create JAX-RS<\/a>\n\t<\/li>\n\t<li>\n\t\t<a href=\"#Deploy\">Deploy<\/a>\n\t<\/li>\n\t<li>\n\t\t<a href=\"#Issues\">Issues<\/a>\n\t\t<ol class='toc-even level-2'>\n\t\t\t<li>\n\t\t\t\t<a href=\"#A_message_body_writer_for_Java_class_java.util.Vector_and_Java_type_java.util.List...\">A message body writer for Java class java.util.Vector, and Java type java.util.List...<\/a>\n\t\t\t<\/li>\n\t\t<\/ol>\n\t<li>\n\t\t<a href=\"#References\">References<\/a>\n\t<\/li>\n<\/ol>\n<\/ol>\n<\/div>\n<div class='wptoc-end'>&nbsp;<\/div>\n<p>* JDeveloper 11.1.1.6<\/p>\n<span id=\"Create_Project\"><h2>Create Project<\/h2><\/span>\n<span id=\"Create_Jersey_User_Library\"><h3>Create Jersey User Library<\/h3><\/span>\n<p>Create a user library and add all Jersey lib files to it<br \/>\n* Download both Jersey zip bundle and jar bundle from <a href=\"https:\/\/jersey.java.net\/download.html\">Jersey download site<\/a>:<br \/>\n&#8211; Jersey 1.17.1 ZIP bundle<br \/>\n&#8211; Jersey 1.17.1 JAR bundle<br \/>\n* Unzip zip bundle<br \/>\n* From JDeveloper, go to Tools > Manage Libraries > User > New&#8230;<br \/>\n&#8211; Library Name: jersey1.7-lib<br \/>\n* Add Jersey jars files to the library<br \/>\n* Also check <em>Deploy by default<\/em> checkbox<\/p>\n<span id=\"Create_Project_1\"><h3>Create Project<\/h3><\/span>\n<p>* In JDev, got to New > Projects > Generic Project<br \/>\n&#8211; Project Name: jpaejb3Demo1<br \/>\n&#8211; Default package: rest.jpaejb3<br \/>\n* From <em>Project Properties > Libraries and Classpath > Add Library&#8230;<\/em>, Add <em>jersey1.7-lib<\/em> to the classpath<br \/>\n* Also need to add j2ee1.5 api libary to class path<\/p>\n<span id=\"Create_a_DB_Connection\"><h3>Create a DB Connection<\/h3><\/span>\n<p>* Create an db connection named hr<\/p>\n<span id=\"Create_Entity_Bean\"><h2>Create Entity Bean<\/h2><\/span>\n<p>* Right click newly created project<br \/>\n* Select New > Business Tier > EJB > Entities from Tables<br \/>\n* Select DEPARTMENTS table<br \/>\n* Package name: rest.jpaejb3<\/p>\n<span id=\"\"><h6><\/h6><\/span>\n<span id=\"_1\"><h6><\/h6><\/span>\n<span id=\"_2\"><h6><\/h6><\/span>\n<span id=\"_3\"><h6><\/h6><\/span>\n<span id=\"_4\"><h6><\/h6><\/span>\n<span id=\"_5\"><h6><\/h6><\/span>\n<span id=\"_6\"><h6><\/h6><\/span>\n<span id=\"_7\"><h6><\/h6><\/span>\n<span id=\"_8\"><h6><\/h6><\/span>\n<span id=\"_9\"><h6><\/h6><\/span>\n<span id=\"Create_EJB_Diagram\"><h2>Create EJB Diagram<\/h2><\/span>\n<p>* Right click newly created project<br \/>\n* Select New > Business Tier > EJB > EJB Diagram (JPA\/EJB 3.0)<br \/>\n* Enter:<br \/>\n&#8211; Name: EJB3<br \/>\n&#8211; Package: rest.jpaejb3<\/p>\n<span id=\"_10\"><h6><\/h6><\/span>\n<span id=\"_11\"><h6><\/h6><\/span>\n<span id=\"_12\"><h6><\/h6><\/span>\n<p>* Drag and drop  Departments.java to the diagram<\/p>\n<span id=\"_13\"><h6><\/h6><\/span>\n<span id=\"Create_Stateless_Session_Bean\"><h2>Create Stateless Session Bean<\/h2><\/span>\n<p>* Drag a Component Palette > EJB Components > EJB Nodes > Session Bean to the diagram<br \/>\n* Enter:<br \/>\n&#8211; EJB Name: DeptSessionEJB<br \/>\n&#8211; Session Type: Stateless<br \/>\n&#8211; Transaction Type: Container<br \/>\n&#8211; Mapped Name: RESTfulApp-jpaejb3Demo1-DeptSessionEJB<br \/>\n&#8211; Checked: Generate Session Facade Methods<br \/>\n&#8211; Entity implementation: JPA Entities<br \/>\n&#8211; Persistence Unit: jpaejb3Demo1<br \/>\n&#8211; Bean class: rest.jpaejb3.DeptSessionEJBBean<\/p>\n<span id=\"_14\"><h6><\/h6><\/span>\n<span id=\"_15\"><h6><\/h6><\/span>\n<span id=\"_16\"><h6><\/h6><\/span>\n<span id=\"Create_JAX-RS\"><h2>Create JAX-RS<\/h2><\/span>\n<p>* Create a new Java class and annotate with Jersey annotations<\/p>\n<pre lang=\"java\">\r\npackage rest.jpaejb3;\r\n\r\nimport java.util.Hashtable;\r\n\r\nimport java.util.List;\r\n\r\nimport javax.naming.Context;\r\nimport javax.naming.InitialContext;\r\nimport javax.naming.NamingException;\r\n\r\nimport javax.ws.rs.GET;\r\nimport javax.ws.rs.Path;\r\nimport javax.ws.rs.Produces;\r\nimport javax.ws.rs.core.MediaType;\r\n\r\n@Path(\"\/deptService\")\r\npublic class DeptService {\r\n    private DeptSessionEJB deptBean;\r\n    private String mappedName = \r\n        \"RESTfulApp-jpaejb3Demo1-DeptSessionEJB#rest.jpaejb3.DeptSessionEJB\";\r\n    \r\n    public DeptService() {\r\n        Context ctx;\r\n        try {\r\n            ctx = getInitialContext();\r\n            deptBean = (DeptSessionEJB)ctx\r\n                .lookup(mappedName);;\r\n        } catch (NamingException e) {\r\n            throw new RuntimeException(e.getMessage());\r\n        }\r\n    }\r\n\r\n    @GET\r\n    @Path(\"{deptId}\")\r\n    @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})\r\n    public Departments findDeptment(@PathParam(\"deptId\") int deptId){\r\n        return deptBean.findDeptById(deptId);\r\n    }\r\n    \r\n    @GET\r\n    @Path(\"\/departments\")\r\n    @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})\r\n    public List<Departments> listDepartments(){\r\n        return deptBean.getDepartmentsFindAll();\r\n    }\r\n\r\n    private static Context getInitialContext() throws NamingException {\r\n        return new InitialContext();\r\n    }\r\n}\r\n\r\n<\/pre>\n<p>* Click on @Path(&#8220;\/deptService&#8221;), then click on the question mark yellow balloon, and select configure web.xml for Jersey JAX-WS web services<\/p>\n<span id=\"_17\"><h6><\/h6><\/span>\n<p>* Also annotate Departments class with @XMLRootElement<\/p>\n<pre lang=\"java\">\r\n@Entity\r\n@NamedQueries({\r\n  @NamedQuery(name = \"Departments.findAll\", query = \"select o from Departments o\")\r\n})\r\n@XmlRootElement\r\npublic class Departments implements Serializable {\r\n\r\n<\/pre>\n<span id=\"Deploy\"><h2>Deploy<\/h2><\/span>\n<p>* Right click DeptService and select Run<br \/>\n[04:50:38 PM] &#8212;-  Deployment finished.  &#8212;-<br \/>\nRun startup time: 3713 ms.<br \/>\n[Application RESTfulApp deployed to Server Instance IntegratedWebLogicServer]<\/p>\n<p>Target Application WADL &#8212; http:\/\/localhost:7101\/RESTfulApp-jpaejb3Demo1-context-root\/jersey\/application.wadl<\/p>\n<p>Target URL &#8212; http:\/\/127.0.0.1:7101\/RESTfulApp-jpaejb3Demo1-context-root\/jersey\/deptService\/departments<\/p>\n<p>* Test by pointing browser to <a href=\"http:\/\/127.0.0.1:7101\/RESTfulApp-jpaejb3Demo1-context-root\/jersey\/deptService\/departments\">http:\/\/127.0.0.1:7101\/RESTfulApp-jpaejb3Demo1-context-root\/jersey\/deptService\/departments<\/a> and you should see sample output:<\/p>\n<pre lang=\"xml\">\r\n<departmentss><departments><departmentId>10<\/departmentId><departmentName>Administration<\/departmentName><locationId>1700<\/locationId><managerId>200<\/managerId><\/departments><departments><departmentId>20<\/departmentId><departmentName>Marketing<\/departmentName><locationId>1800<\/locationId><managerId>201<\/managerId><\/departments>\r\n<\/pre>\n<span id=\"Issues\"><h2>Issues<\/h2><\/span>\n<span id=\"A_message_body_writer_for_Java_class_java.util.Vector_and_Java_type_java.util.List8230\"><h3>A message body writer for Java class java.util.Vector, and Java type java.util.List&#8230;<\/h3><\/span>\n<p>* <a href=\"http:\/\/stackoverflow.com\/questions\/11040995\/a-message-body-writer-for-java-class-java-util-arraylist\">A message body writer for Java class java.util.ArrayList<\/a><br \/>\n* <a href=\"http:\/\/stackoverflow.com\/questions\/18646747\/how-to-produce-json-output-with-jersey-1-17-1-using-jaxb\/18662286#18662286\">How to produce JSON output with Jersey 1.17.1 using JAXB<\/a><\/p>\n<span id=\"References\"><h2>References<\/h2><\/span>\n<p>* <a href=\"http:\/\/coenraets.org\/blog\/2011\/12\/restful-services-with-jquery-and-java-using-jax-rs-and-jersey\/\">RESTful services with jQuery and Java using JAX-RS and Jersey<\/a><br \/>\n* <a href=\"http:\/\/theblasfrompas.blogspot.com\/2011\/03\/developing-restful-web-services-from.html\">Developing RESTful Web Services from JDeveloper 11g (11.1.1.4)<\/a><br \/>\n* <a href=\"http:\/\/theblasfrompas.blogspot.com\/2011\/03\/restful-web-service-using-jpaejb-30.html\">RESTful Web Service using JPA\/EJB 3.0 Entities<\/a><br \/>\n* <a href=\"http:\/\/www.oracle.com\/webfolder\/technetwork\/tutorials\/obe\/jdev\/obe11jdev\/ps1\/ejb\/ejb.html\">http:\/\/www.oracle.com\/webfolder\/technetwork\/tutorials\/obe\/jdev\/obe11jdev\/ps1\/ejb\/ejb.html<\/a><br \/>\n* <a href=\"http:\/\/biemond.blogspot.com\/2012\/10\/easy-way-to-access-jpa-with-rest-json.html\">Easy way to access JPA with REST (JSON \/ XML)<\/a> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>* JDeveloper 11.1.1.6 Create Project Create Jersey User Library Create a user library and add all Jersey lib files to it * Download both Jersey zip bundle and jar bundle from Jersey download site: &#8211; Jersey 1.17.1 ZIP bundle &#8211; &hellip; <a href=\"https:\/\/jianmingli.com\/wp\/?p=9342\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[14,483],"tags":[484,194,590],"class_list":["post-9342","post","type-post","status-publish","format-standard","hentry","category-java","category-restful-soa","tag-ejb3","tag-jersey","tag-weblogic"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8cRUO-2qG","_links":{"self":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/9342","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=9342"}],"version-history":[{"count":14,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/9342\/revisions"}],"predecessor-version":[{"id":11115,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/9342\/revisions\/11115"}],"wp:attachment":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9342"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9342"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9342"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}