Java Restful Web Services Example Java 13

The goal of this exercise is to create a project and generate entity classes and RESTful web services from a database.

This section uses the JavaDB (Derby) database and the jdbc/sample data source. JavaDB is included with the SDK. The jdbc/sample data source is generated by NetBeans IDE automatically when you install the IDE together with GlassFish.

Creating the Project

To create RESTful web services, you need a Java Web application project.

*To create the project: *

  1. Choose File > New Project (Ctrl-Shift-N on Linux and Windows, ⌘-Shift-N on MacOS). Under Categories, select Java Web. Under Projects, select Web Application. Click Next. The New Web Application wizard opens.

Alternatively, you can create a Maven Web Application. Choose File > New Project (Ctrl-Shift-N on Linux and Windows, ⌘-Shift-N on MacOS). Under Categories, select Maven. Under Projects, select Maven Web Application and click Next.

  1. Under Project Name, enter CustomerDB . Click Next.

  1. Select either Java EE 6 Web or Java EE 7 Web. Under Server, select the server you want to use, but note that Java EE projects require GlassFish server 3.x or 4.x. Click through the remaining options and click Finish.

Important for Maven Projects: In NetBeans IDE 7.2 you cannot set the server when you create a Maven web application. However, you need to set the server before you can create a persistence unit. Therefore, after you create the Maven web application, open the project's Properties and set the server in the Run properties. To open the project's Properties, right-click on the Project node and select Properties from the context menu.

Generating Entity Classes and RESTful Services

When you have a Java web application, add entity classes and RESTful web services to the project.

*To generate entity classes and RESTful web services: *

  1. Right-click the CustomerDB node and choose New > Other > Web Services > RESTful Web Services from Database. The New RESTful Web Service wizard opens, on the Database Tables panel.

open wizard

  1. In the Database Tables panel, if you are using the GlassFish server, select the jdbc/sample data source from the Data Source drop-down field.

If you are using Tomcat, select jdbc:derby://localhost:1527/sample. If the Derby database server does not start automatically, you need to start it from the Databases tab in the Services window.

Note for MySQL users: You have to create a new data source. Select New Data Source, give it an arbitrary descriptive name, and select the jdbc:mysql://localhost:3306/sample database connection. You created this connection when you created the sample database on MySQL.

new mysql datasource

  1. Under Available Tables, select CUSTOMER and then click Add. The DISCOUNT_CODE table, which has a relationship with the CUSTOMER table, is also automatically added to the Selected Tables list. If you are using a MySQL database or some versions of Derby, the MICRO_MARKET table is also added. You now see the following (Derby version):

select tables

Figure 2. Database Tables panel of New Entity Classes from Database wizard, showing CUSTOMER and DISCOUNT_CODE tables selected

  1. Click Next. The Entity Classes page opens. Type entities for the Package name. You now see the following (Derby version).

Note: The RESTful Web Services from Database wizard automatically generates JAXB annotations. If you generate entity classes for a Java EE application with the Entity Classes from Database wizard, and you might later want to create RESTful web services from those entity classes, make sure the Generate JAXB Annotations box is checked. You can also add JAXB annotations by hand to entity classes before running the RESTful Web Services from Entity Classes wizard. For more information, see NetBeans to Generate Simpler RESTful Web Services.

entity classes

  1. Click Next. A panel opens in which you can set the name and location of generated service classes and packages. For Java EE projects, you have the option of changing the name and location of the REST application configuration class.

For this tutorial, accept the defaults and click Finish. When you click Finish the IDE generates entity and service classes. In an Java EE project, the IDE also generates an application configuration class, which is a subclass of Application.

class name location

The IDE now generates the RESTful web services. When the IDE is finished, look in the Projects window. The generated entity classes are in the entities package and services are in the service package. Java EE RESTful web services from a database instantiate EntityManager in each service class. This removes the need for JPA controller classes and generates simpler code.

johnsoncloneffew.blogspot.com

Source: https://netbeans.apache.org/kb/docs/websvc/rest.html

0 Response to "Java Restful Web Services Example Java 13"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel