-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Getting "resource" is null with Spring Data REST and azure-spring-data-cosmos #33115
Comments
@backwind1233 could you please follow up? |
@joshfree sure, I will follow up this. |
Hi @singh-abhijeet, thanks for reaching out, I am tring to help, but I can't reproduce your issue, could you tell me more about your issue, steps I can follow to reproduce your issue, and a mini sample would be better, if you could attach the sample here. |
Thanks for the reply @backwind1233 , I am utilizing Spring Data REST for creating RESTful CRUD APIs of models and using Cosmos DB as the persistence. To keep things simple, I'm using User model and repo. But while starting the Spring Boot app, I'm getting NPE: Sharing the project so it becomes easier for you to reproduce and help: Removed
|
Hi @singh-abhijeet
You can report your issue in https://github.com/springfox/springfox, and I think they already tracked this: springfox/springfox#3462. |
@backwind1233 Thanks for reproducing the error. As per your suggestion, I have removed SpringFox from the deps but the issue is still coming:
The web app starts and on hitting the API (GET) it gives 500 status code Attaching new project without SpringFox - SpringDataRESTNoSpringFox.zip |
Hi @kushagraThapar, could you help look at the issue? |
@singh-abhijeet not sure on this one, the error is not coming from the spring data cosmos SDK. |
Thanks @kushagraThapar for the reply. As per the Azure-Cosmos-Java-SDK docs, it supports Spring Data REST. Can you please share a sample Spring Data REST project leveraging Cosmos as persistence, that would be helpful as a starting point. |
@singh-abhijeet unfortunately we don't have any samples on this, but we should definitely be able to build one sooner. @trande4884 / @TheovanKraay can you please follow up on this, thanks! |
@singh-abhijeet we will add samples. However, to get a simple rest service up and running, you are only one step away. In your code, you just need a HomeController.java or similarly named class at the same directory as ConfigmetaApplication.java, with the rest mapping annotations. E.g. like the following (you should be able to copy/paste as long as your config is set up right): package org.test;
import com.azure.cosmos.models.PartitionKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.test.models.User;
import org.test.repo.UserRepository;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
@PropertySource("classpath:application.yaml")
@Controller
@RequestMapping(path = "/home")
public class HomeController {
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
private final UserRepository userRepository;
public HomeController(UserRepository userRepository) {
this.userRepository = userRepository;
}
@PostMapping
public @ResponseBody String createUser(@RequestBody User user) {
UUID uuid = UUID.randomUUID();
user.setId(String.valueOf(uuid));
userRepository.save(user);
return String.format("Added %s.", user);
}
@GetMapping
public @ResponseBody String getAllUsers() {
Iterable<User> iter = userRepository.findAll();
return StreamSupport.stream(iter.spliterator(), true)
.map(User::toString)
.collect(Collectors.joining(" , "));
}
@GetMapping("/{id}")
public @ResponseBody String getUser(@PathVariable UUID id) {
User user = userRepository.findById(id.toString()).get();
String response = "no users found!";
if(user != null){
logger.info("user: "+ user.getLastName());
response = "first name: "+user.getFirstName() +", lastName: "+user.getLastName()+ ", id: "+user.getId();
}
return response;
}
@DeleteMapping("/{id}")
public @ResponseBody String deleteUser(@PathVariable UUID id) {
User user = userRepository.findById(id.toString()).get();
if(user != null){
userRepository.deleteById(id.toString(), new PartitionKey(user.getLastName()));
return "Deleted " + id;
}
else{
return "no users found!";
}
}
} REST call to create doc: curl -s -d '{"firstName":"Theo","lastName":"van Kraay"}' -H "Content-Type: application/json" -X POST http://localhost:8080/config-meta/home |
Thanks @TheovanKraay for sharing customized Controller code. But I see this deviates from the pattern of Spring Data REST where the framework automatically exposes a discoverable REST API for your domain model, and creation of Controller is optional. To demonstrate this, I have changed the persistence to Mongo, and all the features of Spring Data REST are accessible as expected ( Auto-generated traversable REST API is accessible at: I am interested in utilizing Cosmos with Spring Data REST, but without manually creating Controllers. |
@singh-abhijeet I believe this is a feature of Spring Data REST support for "managed" data sources (currently JPA, MongoDB, Neo4j, Solr, Cassandra, Gemfire). With that said, we may need to clarify this better in our documentation, and we will also investigate the path to add support. cc @kushagraThapar and @trande4884 for further comment. Meanwhile, I wonder if it is worth it for you to consider using Azure Cosmos DB API for MongoDB? If you create an account with this API, Azure Cosmos DB should behave as though it is MongoDB at the wire protocol level, and the above features should then work as you expect. |
Yes, in that case the documentation should have a caveat mentioning "Spring Data REST auto RestController generation not supported". Additionally, in future releases spring-data-cosmos should support these features to be fully compatible with Spring Data REST, else as per the current approach (manual Controller creation) it is only compatible with Spring Boot WebMvc arch. |
Hi
I am trying to use azure-spring-data-cosmos with Spring Boot Starter Data REST, but encountering the following error on Spring Boot app's startup:
Caused by: java.lang.NullPointerException: Cannot invoke "org.springframework.data.rest.core.mapping.ResourceMetadata.isExported()" because "resource" is null
Dependencies in pom.xml:
Environment
JDK 17 - java version "17.0.5" 2022-10-18 LTS
Java(TM) SE Runtime Environment (build 17.0.5+9-LTS-191)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.5+9-LTS-191, mixed mode, sharing)
Windows 10 x64
The text was updated successfully, but these errors were encountered: