Skip to content

Commit 3cc6a13

Browse files
committed
Updating web app sample with v4 SDK
1 parent 8e3e81b commit 3cc6a13

File tree

2 files changed

+68
-150
lines changed

2 files changed

+68
-150
lines changed

.openpublishing.publish.config.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,11 @@
419419
"url": "https://github.com/Azure/azure-cosmos-dotnet-v2",
420420
"branch": "master"
421421
},
422+
{
423+
"path_to_root": "samples-cosmosdb-java-v4-web-app",
424+
"url": "https://github.com/Azure-Samples/azure-cosmos-java-sql-api-todo-app",
425+
"branch": "master"
426+
},
422427
{
423428
"path_to_root": "samples-cosmosdb-dotnet-change-feed-processor",
424429
"url": "https://github.com/Azure-Samples/cosmos-dotnet-change-feed-processor",

articles/cosmos-db/sql-api-java-application.md

Lines changed: 63 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -89,48 +89,51 @@ To create the JSP application:
8989

9090
## <a id="InstallSDK"></a>Install the SQL Java SDK
9191

92-
The easiest way to pull in the SQL Java SDK and its dependencies is through [Apache Maven](https://maven.apache.org/).
93-
94-
To do this, you will need to convert your project to a maven project by completing the following steps:
92+
The easiest way to pull in the SQL Java SDK and its dependencies is through [Apache Maven](https://maven.apache.org/). To do this, you need to convert your project to a maven project by using the following steps:
9593

9694
1. Right-click your project in the Project Explorer, click **Configure**, click **Convert to Maven Project**.
97-
2. In the **Create new POM** window, accept the defaults, and click **Finish**.
98-
3. In **Project Explorer**, open the pom.xml file.
99-
4. On the **Dependencies** tab, in the **Dependencies** pane, click **Add**.
100-
5. In the **Select Dependency** window, do the following:
101-
102-
* In the **Group Id** box, enter com.microsoft.azure.
103-
* In the **Artifact Id** box, enter azure-documentdb.
104-
* In the **Version** box, enter 1.5.1.
105-
106-
![Install SQL Java Application SDK](./media/sql-api-java-application/image13.png)
107-
108-
* Or add the dependency XML for Group ID and Artifact ID directly to the pom.xml via a text editor:
109-
```xml
110-
<dependency>
111-
<groupId>com.microsoft.azure</groupId>
112-
<artifactId>azure-documentdb</artifactId>
113-
<version>1.9.1</version>
114-
</dependency>
115-
```
116-
6. Click **OK** and Maven will install the SQL Java SDK.
117-
7. Save the pom.xml file.
118-
119-
## <a id="UseService"></a>Using the Azure Cosmos DB service in a Java application
120-
121-
1. First, let's define the TodoItem object in TodoItem.java:
122-
123-
@Data
124-
@Builder
125-
public class TodoItem {
126-
private String category;
127-
private boolean complete;
128-
private String id;
129-
private String name;
130-
}
95+
96+
1. In the **Create new POM** window, accept the defaults, and click **Finish**.
97+
98+
1. In **Project Explorer**, open the pom.xml file.
99+
100+
1. On the **Dependencies** tab, in the **Dependencies** pane, click **Add**.
101+
102+
1. In the **Select Dependency** window, do the following:
131103

132-
In this project, you are using [Project Lombok](https://projectlombok.org/) to generate the constructor, getters, setters, and a builder. Alternatively, you can write this code manually or have the IDE generate it.
133-
2. To invoke the Azure Cosmos DB service, you must instantiate a new **DocumentClient**. In general, it is best to reuse the **DocumentClient** - rather than construct a new client for each subsequent request. We can reuse the client by wrapping the client in a **DocumentClientFactory**. In DocumentClientFactory.java, you need to paste the URI and PRIMARY KEY value you saved to your clipboard in [step 1](#CreateDB). Replace [YOUR\_ENDPOINT\_HERE] with your URI and replace [YOUR\_KEY\_HERE] with your PRIMARY KEY.
104+
* In the **Group Id** box, enter `com.azure`.
105+
* In the **Artifact Id** box, enter `azure-cosmos`.
106+
* In the **Version** box, enter `4.0.1-beta.1`.
107+
108+
Or you can add the dependency XML for Group ID and Artifact ID directly to the *pom.xml* file:
109+
110+
```xml
111+
<dependency>
112+
<groupId>com.azure</groupId>
113+
<artifactId>azure-cosmos</artifactId>
114+
<version>4.0.1-beta.1</version>
115+
</dependency>
116+
```
117+
118+
1. Click **OK** and Maven will install the SQL Java SDK or save the pom.xml file.
119+
120+
## <a id="UseService"></a>Use the Azure Cosmos DB service in your Java application
121+
122+
Now let's add the models, the views, and the controllers to your web application.
123+
124+
### Add a model
125+
126+
First, let's define a model within a new file *TodoItem.java*. The `TodoItem` class defines the schema of an item along with the getter and setter methods:
127+
128+
[!INCLUDE[Release notes](~/samples-cosmosdb-java-v4-web-app/src/com/microsoft/azure/documentdb/sample/model/TodoItem.java)]
129+
130+
### Add the Data Access Object(DAO) classes
131+
132+
Create a Data Access Object (DAO) to abstract persisting the ToDo items to Azure Cosmos DB. In order to save ToDo items to a collection, the client needs to know which database and collection to persist to (as referenced by self-links). In general, it is best to cache the database and collection when possible to avoid additional round-trips to the database.
133+
134+
The following code illustrates how to retrieve our database and collection, if it exists, or create a new one if it doesn't exist:
135+
136+
To invoke the Azure Cosmos DB service, you must instantiate a new **DocumentClient**. In general, it is best to reuse the **DocumentClient** - rather than construct a new client for each subsequent request. We can reuse the client by wrapping the client in a **DocumentClientFactory**. In DocumentClientFactory.java, you need to paste the URI and PRIMARY KEY value you saved to your clipboard in [step 1](#CreateDB). Replace [YOUR\_ENDPOINT\_HERE] with your URI and replace [YOUR\_KEY\_HERE] with your PRIMARY KEY.
134137

135138
private static final String HOST = "[YOUR_ENDPOINT_HERE]";
136139
private static final String MASTER_KEY = "[YOUR_KEY_HERE]";
@@ -141,11 +144,27 @@ To do this, you will need to convert your project to a maven project by completi
141144
public static DocumentClient getDocumentClient() {
142145
return documentClient;
143146
}
144-
3. Now let's create a Data Access Object (DAO) to abstract persisting our ToDo items to Azure Cosmos DB.
145-
146-
In order to save ToDo items to a collection, the client needs to know which database and collection to persist to (as referenced by self-links). In general, it is best to cache the database and collection when possible to avoid additional round-trips to the database.
147-
148-
The following code illustrates how to retrieve our database and collection, if it exists, or create a new one if it doesn't exist:
147+
148+
### Add a controller
149+
150+
Add the *TodoItemController* controller to your application. In this project, you are using [Project Lombok](https://projectlombok.org/) to generate the constructor, getters, setters, and a builder. Alternatively, you can write this code manually or have the IDE generate it.:
151+
152+
[!INCLUDE[Release notes](~/samples-cosmosdb-java-v4-web-app/src/com/microsoft/azure/documentdb/sample/sample/controller/TodoItemController.java)]
153+
154+
### Create a servlet
155+
156+
Next, create a servlet to route HTTP requests to the controller:
157+
158+
[!INCLUDE[Release notes](~/samples-cosmosdb-java-v4-web-app/src/com/microsoft/azure/documentdb/sample/sample/ApiServlet.java)]
159+
160+
161+
162+
163+
164+
Now that we've finished the fun bits - all that's left is to build a quick user interface and wire it up to our DAO.
165+
166+
167+
3. Now
149168

150169
public class DocDbDao implements TodoDao {
151170
// The name of our database.
@@ -352,113 +371,7 @@ To do this, you will need to convert your project to a maven project by completi
352371
}
353372

354373
## <a id="Wire"></a>Wiring the rest of the of Java application development project together
355-
Now that we've finished the fun bits - all that's left is to build a quick user interface and wire it up to our DAO.
356374

357-
1. First, let's start with building a controller to call our DAO:
358-
359-
public class TodoItemController {
360-
public static TodoItemController getInstance() {
361-
if (todoItemController == null) {
362-
todoItemController = new TodoItemController(TodoDaoFactory.getDao());
363-
}
364-
return todoItemController;
365-
}
366-
367-
private static TodoItemController todoItemController;
368-
369-
private final TodoDao todoDao;
370-
371-
TodoItemController(TodoDao todoDao) {
372-
this.todoDao = todoDao;
373-
}
374-
375-
public TodoItem createTodoItem(@NonNull String name,
376-
@NonNull String category, boolean isComplete) {
377-
TodoItem todoItem = TodoItem.builder().name(name).category(category)
378-
.complete(isComplete).build();
379-
return todoDao.createTodoItem(todoItem);
380-
}
381-
382-
public boolean deleteTodoItem(@NonNull String id) {
383-
return todoDao.deleteTodoItem(id);
384-
}
385-
386-
public TodoItem getTodoItemById(@NonNull String id) {
387-
return todoDao.readTodoItem(id);
388-
}
389-
390-
public List<TodoItem> getTodoItems() {
391-
return todoDao.readTodoItems();
392-
}
393-
394-
public TodoItem updateTodoItem(@NonNull String id, boolean isComplete) {
395-
return todoDao.updateTodoItem(id, isComplete);
396-
}
397-
}
398-
399-
In a more complex application, the controller may house complicated business logic on top of the DAO.
400-
2. Next, we'll create a servlet to route HTTP requests to the controller:
401-
402-
public class TodoServlet extends HttpServlet {
403-
// API Keys
404-
public static final String API_METHOD = "method";
405-
406-
// API Methods
407-
public static final String CREATE_TODO_ITEM = "createTodoItem";
408-
public static final String GET_TODO_ITEMS = "getTodoItems";
409-
public static final String UPDATE_TODO_ITEM = "updateTodoItem";
410-
411-
// API Parameters
412-
public static final String TODO_ITEM_ID = "todoItemId";
413-
public static final String TODO_ITEM_NAME = "todoItemName";
414-
public static final String TODO_ITEM_CATEGORY = "todoItemCategory";
415-
public static final String TODO_ITEM_COMPLETE = "todoItemComplete";
416-
417-
public static final String MESSAGE_ERROR_INVALID_METHOD = "{'error': 'Invalid method'}";
418-
419-
private static final long serialVersionUID = 1L;
420-
private static final Gson gson = new Gson();
421-
422-
@Override
423-
protected void doGet(HttpServletRequest request,
424-
HttpServletResponse response) throws ServletException, IOException {
425-
426-
String apiResponse = MESSAGE_ERROR_INVALID_METHOD;
427-
428-
TodoItemController todoItemController = TodoItemController
429-
.getInstance();
430-
431-
String id = request.getParameter(TODO_ITEM_ID);
432-
String name = request.getParameter(TODO_ITEM_NAME);
433-
String category = request.getParameter(TODO_ITEM_CATEGORY);
434-
boolean isComplete = StringUtils.equalsIgnoreCase("true",
435-
request.getParameter(TODO_ITEM_COMPLETE)) ? true : false;
436-
437-
switch (request.getParameter(API_METHOD)) {
438-
case CREATE_TODO_ITEM:
439-
apiResponse = gson.toJson(todoItemController.createTodoItem(name,
440-
category, isComplete));
441-
break;
442-
case GET_TODO_ITEMS:
443-
apiResponse = gson.toJson(todoItemController.getTodoItems());
444-
break;
445-
case UPDATE_TODO_ITEM:
446-
apiResponse = gson.toJson(todoItemController.updateTodoItem(id,
447-
isComplete));
448-
break;
449-
default:
450-
break;
451-
}
452-
453-
response.getWriter().println(apiResponse);
454-
}
455-
456-
@Override
457-
protected void doPost(HttpServletRequest request,
458-
HttpServletResponse response) throws ServletException, IOException {
459-
doGet(request, response);
460-
}
461-
}
462375
3. We'll need a web user interface to display to the user. Let's re-write the index.jsp we created earlier:
463376
```html
464377
<html>

0 commit comments

Comments
 (0)