问题
My project is using azure-documentdb-spring-boot-starter:0.2.0
to interact with cosmosdb:
@Repository
public interface PingEasyRepo extends DocumentDbRepository<PingEasy, String> {
}
@Document(collection = "test")
public class PingEasy {
@Id
private String id;
private String content;
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
The code runs and can do save and findAll. But when I go to the azure portal and cosmosdb data explorer, the data not appeared. When I open browser tools, I see failed requests with
"500 Internal Server Error: Object reference not set to an instance of an object." and exceptions:
Uncaught TypeError: xhr.getResponseHeader is not a function
at Object.DocumentClientFactory._shouldForceRetry (DocumentClientFactory.js?v=1.17.110.1:12)
at HttpRequest.xhr.onreadystatechange (documentdbclient-1.14.0.js?v=1.17.110.1:3287)
at HttpRequest._onAjaxError (Request.js?v=1.17.110.1:42)
at i (jquery.min.js:2)
at Object.fireWith [as rejectWith] (jquery.min.js:2)
at y (jquery.min.js:4)
at XMLHttpRequest.c (jquery.min.js:4)
The following is the script I used to auto create my cosmodb:
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"databaseAccountName": {
"type": "string",
"metadata": {
"description": "The MongoDB database account name. Needs to be globally unique."
}
}
},
"variables": {},
"resources": [
{
"apiVersion": "2015-04-08",
"type": "Microsoft.DocumentDB/databaseAccounts",
"kind": "MongoDB",
"name": "[parameters('databaseAccountName')]",
"location": "[resourceGroup().location]",
"properties": {
"databaseAccountOfferType": "Standard",
"name": "[parameters('databaseAccountName')]"
}
}
]
}
回答1:
The issue is that you are creating a MongoDB account and using a sample that writes data using the DocumentDB API, as stated in the article you linked.
Microsoft's Spring Boot Starter enables developers to use Spring Boot applications that easily integrate with Azure Cosmos DB by using DocumentDB APIs.
MongoDB accounts are meant to be used with MongoDB clients and applications, not DocumentDB API clients and applications.
The main difference is that MongoDB's required identifier field is "_id" while DocumentDB/SQL account's required identifier is "id". When you write documents to a MongoDB account through a MongoDB client (application or a code using one of the MongoDB SDKs), the driver/client makes sure your document has the required "_id" field or autogenerates one. And when you work with a DocumentDB API sdk/client and a DocumentDB/SQL account, the sdk/client will autogenerate the required "id" field.
The Portal uses a MongoDB client to read the documents in a MongoDB account, but it is failing to read the documents because they are not valid MongoDB documents (don't have the required identifier). You will run into the same error if you try to read the documents with a MongoDB application coded by yourself or if you try to read that using a MongoDB client like Robomongo or Mongo Chef.
In your case, if you want to use the Spring Boot sample, you need to create a DocumentDB/SQL account. In the screenshots of the article you can see how to create a SQL account.
Hope this helps.
回答2:
I tried to reproduce your issue but failed. I followed this official tutorial to use the Spring Boot Starter with Azure Cosmos DB DocumentDB API.
You could refer to my code and my steps as below which works for me:
Main Class :
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
@SpringBootApplication
public class JayGongCosmosDbSpringBootProjectApplication implements CommandLineRunner{
@Autowired
private UserRepository repository;
public static void main(String[] args) {
SpringApplication.run(JayGongCosmosDbSpringBootProjectApplication.class, args);
}
public void run(String... var1) throws Exception {
final User testUser = new User("testId", "testFirstName", "testLastName");
repository.deleteAll();
repository.save(testUser);
final User result = repository.findOne(testUser.getId());
System.out.printf("\n\n%s\n\n",result.toString());
}
}
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;
this.lastName = lastName;
}
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);
}
}
UserRepository Class :
import com.microsoft.azure.spring.data.documentdb.repository.DocumentDbRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UserRepository extends DocumentDbRepository<User, String> {}
pom.xml:
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-documentdb-spring-boot-starter</artifactId>
<version>0.1.4</version>
</dependency>
Then I execute the jar package and create documents successfully.
I notice that the version of azure-documentdb-spring-boot-starter you're using is 0.2.0 and I'm using 0.1.4. I suggest you changing the version and try again.
Hope it helps you.
来源:https://stackoverflow.com/questions/47587204/cosmosdb-saved-data-cannot-be-found-in-portal-but-from-azure-documentdb-spring-b