Merge pull request #541 from rmcmurray/20180705
Updating Spring Cosmos DB article
|
@ -9,7 +9,7 @@ editor: ''
|
|||
|
||||
ms.assetid:
|
||||
ms.author: robmcm;yungez;kevinzha
|
||||
ms.date: 02/01/2018
|
||||
ms.date: 07/05/2018
|
||||
ms.devlang: java
|
||||
ms.service: cosmos-db
|
||||
ms.tgt_pltfrm: multiple
|
||||
|
@ -35,7 +35,7 @@ The following prerequisites are required in order to follow the steps in this ar
|
|||
|
||||
## Create an Azure Cosmos DB by using the Azure portal
|
||||
|
||||
1. Browse to the Azure portal at <https://portal.azure.com/> and click **+New**.
|
||||
1. Browse to the Azure portal at <https://portal.azure.com/> and click **+Create a resource**.
|
||||
|
||||
![Azure portal][AZ01]
|
||||
|
||||
|
@ -46,7 +46,7 @@ The following prerequisites are required in order to follow the steps in this ar
|
|||
1. On the **Azure Cosmos DB** page, enter the following information:
|
||||
|
||||
* Enter a unique **ID**, which you will use as the URI for your database. For example: *wingtiptoysdata.documents.azure.com*.
|
||||
* Choose **SQL (Document DB)** for the API.
|
||||
* Choose **SQL** for the API.
|
||||
* Choose the **Subscription** you want to use for your database.
|
||||
* Specify whether to create a new **Resource group** for your database, or choose an existing resource group.
|
||||
* Specify the **Location** for your database.
|
||||
|
@ -67,13 +67,18 @@ The following prerequisites are required in order to follow the steps in this ar
|
|||
|
||||
1. Browse to <https://start.spring.io/>.
|
||||
|
||||
1. Specify that you want to generate a **Maven** project with **Java**, enter the **Group** and **Artifact** names for your application, and then click the button to **Generate Project**.
|
||||
1. Specify that you want to generate a **Maven** project with **Java**, enter the **Group** and **Artifact** names for your application, specify your **Spring Boot** version, and then click the button to **Generate Project**.
|
||||
|
||||
> [!IMPORTANT]
|
||||
>
|
||||
> There were several breaking changes to the APIs in Spring Boot version 2.0.n, as a result, you will need to one of the Spring Boot 1.5.n versions to complete the steps in this tutorial.
|
||||
>
|
||||
|
||||
![Basic Spring Initializr options][SI01]
|
||||
|
||||
> [!NOTE]
|
||||
>
|
||||
> The Spring Initializr uses the **Group** and **Artifact** names to create the package name; for example: *com.example.wintiptoys*.
|
||||
> The Spring Initializr uses the **Group** and **Artifact** names to create the package name; for example: *com.example.wintiptoysdata*.
|
||||
>
|
||||
|
||||
1. When prompted, download the project to a path on your local computer.
|
||||
|
@ -88,11 +93,11 @@ The following prerequisites are required in order to follow the steps in this ar
|
|||
|
||||
1. Locate the *pom.xml* file in the directory of your app; for example:
|
||||
|
||||
`C:\SpringBoot\wingtiptoys\pom.xml`
|
||||
`C:\SpringBoot\wingtiptoysdata\pom.xml`
|
||||
|
||||
-or-
|
||||
|
||||
`/users/example/home/wingtiptoys/pom.xml`
|
||||
`/users/example/home/wingtiptoysdata/pom.xml`
|
||||
|
||||
![Locate the pom.xml file][PM01]
|
||||
|
||||
|
@ -108,17 +113,28 @@ The following prerequisites are required in order to follow the steps in this ar
|
|||
|
||||
![Editing the pom.xml file][PM02]
|
||||
|
||||
1. Verify that the Spring Boot version is one of the 1.5.n versions; for example:
|
||||
|
||||
```xml
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.5.14.RELEASE</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
```
|
||||
|
||||
1. Save and close the *pom.xml* file.
|
||||
|
||||
## Configure your Spring Boot app to use your Azure Cosmos DB
|
||||
|
||||
1. Locate the *application.properties* file in the *resources* directory of your app; for example:
|
||||
|
||||
`C:\SpringBoot\wingtiptoys\src\main\resources\application.properties`
|
||||
`C:\SpringBoot\wingtiptoysdata\src\main\resources\application.properties`
|
||||
|
||||
-or-
|
||||
|
||||
`/users/example/home/wingtiptoys/src/main/resources/application.properties`
|
||||
`/users/example/home/wingtiptoysdata/src/main/resources/application.properties`
|
||||
|
||||
![Locate the application.properties file][RE01]
|
||||
|
||||
|
@ -150,13 +166,14 @@ In this section you create two Java classes for storing user data, and then you
|
|||
1. Open the *User.java* file in a text editor, and add the following lines to the file to define a generic user class that stores and retrieve values in your database:
|
||||
|
||||
```java
|
||||
package com.example.wingtiptoys;
|
||||
package com.example.wingtiptoysdata;
|
||||
|
||||
// Define a generic User class.
|
||||
public class User {
|
||||
private String id;
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
|
||||
|
||||
public User(String id, String firstName, String lastName) {
|
||||
this.id = id;
|
||||
this.firstName = firstName;
|
||||
|
@ -166,30 +183,30 @@ In this section you create two Java classes for storing user data, and then you
|
|||
public String getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("User: %s %s", firstName, lastName);
|
||||
return String.format("User: %s %s %s", id, firstName, lastName);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
@ -203,13 +220,13 @@ In this section you create two Java classes for storing user data, and then you
|
|||
1. Open the *UserRepository.java* file in a text editor, and add the following lines to the file to define a user repository interface that extends the default DocumentDB repository interface:
|
||||
|
||||
```java
|
||||
package com.example.wingtiptoys;
|
||||
|
||||
package com.example.wingtiptoysdata;
|
||||
|
||||
import com.microsoft.azure.spring.data.documentdb.repository.DocumentDbRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
|
||||
@Repository
|
||||
public interface UserRepository extends DocumentDbRepository<User, String> {}
|
||||
public interface UserRepository extends DocumentDbRepository<User, String> { }
|
||||
```
|
||||
|
||||
1. Save and close the *UserRepository.java* file.
|
||||
|
@ -218,42 +235,58 @@ In this section you create two Java classes for storing user data, and then you
|
|||
|
||||
1. Locate the main application Java file in the package directory of your app; for example:
|
||||
|
||||
`C:\SpringBoot\wingtiptoys\src\main\java\com\example\wingtiptoys\WingtiptoysApplication.java`
|
||||
`C:\SpringBoot\wingtiptoysdata\src\main\java\com\example\wingtiptoysdata\WingtiptoysdataApplication.java`
|
||||
|
||||
-or-
|
||||
|
||||
`/users/example/home/wingtiptoys/src/main/java/com/example/wingtiptoys/WingtiptoysApplication.java`
|
||||
`/users/example/home/wingtiptoysdata/src/main/java/com/example/wingtiptoysdata/WingtiptoysdataApplication.java`
|
||||
|
||||
![Locate the application Java file][JV01]
|
||||
|
||||
1. Open the main application Java file in a text editor, and add the following lines to the file:
|
||||
|
||||
```java
|
||||
package com.example.wingtiptoys;
|
||||
|
||||
package com.example.wingtiptoysdata;
|
||||
|
||||
// These imports are required for the application.
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
|
||||
|
||||
// These imports are only used to create an ID for this example.
|
||||
import java.util.Date;
|
||||
import java.text.SimpleDateFormat;
|
||||
|
||||
@SpringBootApplication
|
||||
public class WingtiptoysApplication implements CommandLineRunner {
|
||||
|
||||
public class wingtiptoysdataApplication implements CommandLineRunner {
|
||||
|
||||
@Autowired
|
||||
private UserRepository repository;
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(WingtiptoysApplication.class, args);
|
||||
// Execute the command line runner.
|
||||
SpringApplication.run(wingtiptoysdataApplication.class, args);
|
||||
}
|
||||
|
||||
public void run(String... var1) throws Exception {
|
||||
final User testUser = new User("testId", "testFirstName", "testLastName");
|
||||
|
||||
|
||||
public void run(String... args) throws Exception {
|
||||
// Create a simple date/time ID.
|
||||
SimpleDateFormat userId = new SimpleDateFormat("yyyyMMddHHmmssSSS");
|
||||
Date currentDate = new Date();
|
||||
|
||||
// Create a new User class.
|
||||
final User testUser = new User(userId.format(currentDate), "Gena", "Soto");
|
||||
|
||||
// For this example, remove all of the existing records.
|
||||
repository.deleteAll();
|
||||
|
||||
// Save the User class to the Azure database.
|
||||
repository.save(testUser);
|
||||
|
||||
|
||||
// Retrieve the database record for the User class you just saved by ID.
|
||||
final User result = repository.findOne(testUser.getId());
|
||||
|
||||
|
||||
// Display the results of the database record retrieval.
|
||||
System.out.printf("\n\n%s\n\n",result.toString());
|
||||
}
|
||||
}
|
||||
|
@ -265,24 +298,24 @@ In this section you create two Java classes for storing user data, and then you
|
|||
|
||||
1. Open a command prompt and change directory to the folder where your *pom.xml* file is located; for example:
|
||||
|
||||
`cd C:\SpringBoot\wingtiptoys`
|
||||
`cd C:\SpringBoot\wingtiptoysdata`
|
||||
|
||||
-or-
|
||||
|
||||
`cd /users/example/home/wingtiptoys`
|
||||
`cd /users/example/home/wingtiptoysdata`
|
||||
|
||||
1. Build your Spring Boot application with Maven and run it; for example:
|
||||
|
||||
```shell
|
||||
mvn package
|
||||
java -jar target/wingtiptoys-0.0.1-SNAPSHOT.jar
|
||||
mvn clean package
|
||||
mvn spring-boot:run
|
||||
```
|
||||
|
||||
1. Your application will display several runtime messages, and you should see the message `User: testFirstName testLastName` displayed to indicate that values have been successfully stored and retrieved from your database.
|
||||
|
||||
![Successful output from the application][JV02]
|
||||
|
||||
1. OPTIONAL: You can use the Azure portal to view the contents of your Azure Cosmos DB from the properties page for your database by clicking **Document Explorer**, and then selecting and item from the displayed list to view the contents.
|
||||
1. OPTIONAL: You can use the Azure portal to view the contents of your Azure Cosmos DB from the properties page for your database by clicking **Data Explorer**, and then selecting and item from the displayed list to view the contents.
|
||||
|
||||
![Using the Document Explorer to view your data][JV03]
|
||||
|
||||
|
@ -298,7 +331,7 @@ For more information about using Azure Cosmos DB and Java, see the following art
|
|||
|
||||
For more information about using Spring Boot applications on Azure, see the following articles:
|
||||
|
||||
* [Spring Boot DocumenDB Starter for Azure]
|
||||
* [Spring Boot Document DB Starter for Azure]
|
||||
|
||||
* [Deploy a Spring Boot Application to the Azure App Service](deploy-spring-boot-java-web-app-on-azure.md)
|
||||
|
||||
|
@ -314,7 +347,7 @@ The **[Spring Framework]** is an open-source solution that helps Java developers
|
|||
[Azure for Java Developers]: https://docs.microsoft.com/java/azure/
|
||||
[Build a SQL API app with Java]: https://docs.microsoft.com/azure/cosmos-db/create-sql-api-java
|
||||
[Spring Data for Azure Cosmos DB SQL API]: https://azure.microsoft.com/blog/spring-data-azure-cosmos-db-nosql-data-access-on-azure/
|
||||
[Spring Boot DocumenDB Starter for Azure]:https://github.com/Microsoft/azure-spring-boot-starters/tree/master/azure-documentdb-spring-boot-starter-sample
|
||||
[Spring Boot Document DB Starter for Azure]:https://github.com/Microsoft/azure-spring-boot-starters/tree/master/azure-documentdb-spring-boot-starter-sample
|
||||
[free Azure account]: https://azure.microsoft.com/pricing/free-trial/
|
||||
[Java Tools for Visual Studio Team Services]: https://java.visualstudio.com/
|
||||
[MSDN subscriber benefits]: https://azure.microsoft.com/pricing/member-offers/msdn-benefits-details/
|
||||
|
|
До Ширина: | Высота: | Размер: 43 KiB После Ширина: | Высота: | Размер: 51 KiB |
До Ширина: | Высота: | Размер: 48 KiB После Ширина: | Высота: | Размер: 48 KiB |
До Ширина: | Высота: | Размер: 18 KiB После Ширина: | Высота: | Размер: 22 KiB |
До Ширина: | Высота: | Размер: 12 KiB После Ширина: | Высота: | Размер: 18 KiB |
До Ширина: | Высота: | Размер: 39 KiB После Ширина: | Высота: | Размер: 54 KiB |
До Ширина: | Высота: | Размер: 22 KiB После Ширина: | Высота: | Размер: 26 KiB |
До Ширина: | Высота: | Размер: 18 KiB После Ширина: | Высота: | Размер: 22 KiB |
До Ширина: | Высота: | Размер: 13 KiB После Ширина: | Высота: | Размер: 13 KiB |
До Ширина: | Высота: | Размер: 33 KiB После Ширина: | Высота: | Размер: 39 KiB |
До Ширина: | Высота: | Размер: 6.0 KiB После Ширина: | Высота: | Размер: 7.3 KiB |
До Ширина: | Высота: | Размер: 39 KiB После Ширина: | Высота: | Размер: 39 KiB |