Skip to content
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

Open
singh-abhijeet opened this issue Jan 19, 2023 · 14 comments
Assignees
Labels
azure-spring All azure-spring related issues azure-spring-cosmos Spring cosmos related issues. Client This issue points to a problem in the data-plane of the library. cosmos-java-ecosystem-dt-planning Considered for Dilithium semester planning customer-reported Issues that are reported by GitHub users external to the Azure organization. needs-team-attention This issue needs attention from Azure service team or SDK team question The issue doesn't require a change to the product in order to be resolved. Most issues start as that

Comments

@singh-abhijeet
Copy link

singh-abhijeet commented Jan 19, 2023

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:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
            <version>2.7.7</version>
        </dependency>
        <dependency>
            <groupId>com.azure</groupId>
            <artifactId>azure-spring-data-cosmos</artifactId>
            <version>3.31.0</version>
        </dependency>

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

@ghost ghost added needs-triage This is a new issue that needs to be triaged to the appropriate team. customer-reported Issues that are reported by GitHub users external to the Azure organization. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that labels Jan 19, 2023
@joshfree joshfree added Client This issue points to a problem in the data-plane of the library. azure-spring All azure-spring related issues azure-spring-cosmos Spring cosmos related issues. labels Jan 19, 2023
@ghost ghost removed the needs-triage This is a new issue that needs to be triaged to the appropriate team. label Jan 19, 2023
@joshfree
Copy link
Member

@backwind1233 could you please follow up?

@backwind1233
Copy link
Contributor

@joshfree sure, I will follow up this.

@backwind1233
Copy link
Contributor

backwind1233 commented Jan 20, 2023

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.

@singh-abhijeet
Copy link
Author

singh-abhijeet commented Jan 20, 2023

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:
"org.springframework.data.rest.core.mapping.ResourceMetadata.isExported()" because "resource" is null

Sharing the project so it becomes easier for you to reproduce and help:
SpringDataREST.zip

Removed application.yml from zip, but it looks like this:

spring:
  mvc:
    pathmatch:
      matching-strategy: ANT_PATH_MATCHER
logging:
  level:
    org.springframework.web: DEBUG
    root: INFO

server:
  port: 8080
  servlet:
    context-path: /config-meta

@backwind1233
Copy link
Contributor

backwind1233 commented Jan 20, 2023

Hi @singh-abhijeet
I reproduced you issue with your sample, but the error is because of springfox, your samples depends on springfox-boot-starter

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>

image

You can report your issue in https://github.com/springfox/springfox, and I think they already tracked this: springfox/springfox#3462.

@backwind1233 backwind1233 added the needs-author-feedback More information is needed from author to address the issue. label Jan 20, 2023
@singh-abhijeet
Copy link
Author

@backwind1233 Thanks for reproducing the error. As per your suggestion, I have removed SpringFox from the deps but the issue is still coming:

2023-01-20 13:05:36.883 DEBUG 7804 --- [nio-8080-exec-3] o.s.w.s.m.m.a.HttpEntityMethodProcessor  : Writing [org.springframework.data.rest.webmvc.support.ExceptionMessage@cd0ae5b]
2023-01-20 13:05:36.884 DEBUG 7804 --- [nio-8080-exec-3] .m.m.a.ExceptionHandlerExceptionResolver : Resolved [java.lang.NullPointerException: Cannot invoke "org.springframework.data.rest.core.mapping.ResourceMetadata.isExported()" because "metadata" is null]
2023-01-20 13:05:36.884 DEBUG 7804 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet        : Completed 500 INTERNAL_SERVER_ERROR

The web app starts and on hitting the API (GET) it gives 500 status code
image

Attaching new project without SpringFox - SpringDataRESTNoSpringFox.zip

@ghost ghost added needs-team-attention This issue needs attention from Azure service team or SDK team and removed needs-author-feedback More information is needed from author to address the issue. labels Jan 20, 2023
@backwind1233
Copy link
Contributor

Hi @kushagraThapar, could you help look at the issue?

@kushagraThapar
Copy link
Member

@singh-abhijeet not sure on this one, the error is not coming from the spring data cosmos SDK.

@singh-abhijeet
Copy link
Author

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.

@kushagraThapar
Copy link
Member

@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!

@TheovanKraay
Copy link
Member

TheovanKraay commented Jan 23, 2023

@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

@singh-abhijeet
Copy link
Author

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 ("metadata" is null error gone when switched to Mongo!).

Auto-generated traversable REST API is accessible at: http://localhost:8080/config-meta/
Code: SpringDataRESTMongo.zip

I am interested in utilizing Cosmos with Spring Data REST, but without manually creating Controllers.

@TheovanKraay
Copy link
Member

TheovanKraay commented Jan 24, 2023

@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.

@singh-abhijeet
Copy link
Author

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.

@kushagraThapar kushagraThapar added the cosmos-java-ecosystem-dt-planning Considered for Dilithium semester planning label Mar 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
azure-spring All azure-spring related issues azure-spring-cosmos Spring cosmos related issues. Client This issue points to a problem in the data-plane of the library. cosmos-java-ecosystem-dt-planning Considered for Dilithium semester planning customer-reported Issues that are reported by GitHub users external to the Azure organization. needs-team-attention This issue needs attention from Azure service team or SDK team question The issue doesn't require a change to the product in order to be resolved. Most issues start as that
Projects
Status: Todo
Development

No branches or pull requests

6 participants