Search code examples
javagoogle-cloud-datastorespring-data-rest

No converter found capable of converting from type [java.lang.String] to type [com.google.cloud.datastore.Key] error if spring-data-rest is used


I am using spring-data-rest on top of spring-data-gcp-datastore.

I am trying to explore my resource using HAL browser. Initially it works fine, so I see my elements:

{
  "_embedded": {
    "configurations": [
      {
        "data": "{\n  \"test\": \"768\",\n  \"test2\": 5\n}",
        "_links": {
          "self": {
            "href": "http://localhost:8083/configurations/Key%7BprojectId=test,%20namespace=,%20path=%5BPathElement%7Bkind=configuration,%20id=null,%20name=k%7D%5D%7D"
          },
          "configuration": {
            "href": "http://localhost:8083/configurations/Key%7BprojectId=test,%20namespace=,%20path=%5BPathElement%7Bkind=configuration,%20id=null,%20name=k%7D%5D%7D"
          }
        }
      }
    ]
  },
  "_links": {
    "self": {
      "href": "http://localhost:8083/configurations{?page,size,sort}",
      "templated": true
    },
    "profile": {
      "href": "http://localhost:8083/profile/configurations"
    }
  },
  "page": {
    "size": 20,
    "totalElements": 1,
    "totalPages": 1,
    "number": 0
  }
}

But when I try to get one specific resource by following the provided link like http://localhost:8083/configurations/Key%7BprojectId=test,%20namespace=,%20path=%5BPathElement%7Bkind=configuration,%20id=null,%20name=k%7D%5D%7D I got an error: org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.String] to type [com.google.cloud.datastore.Key]

I have tried to create the converters and register them by this way:

@Component
public class WebConfig implements WebMvcConfigurer  {

    @Override
    public void addFormatters(FormatterRegistry registry) {
        System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^");
        registry.addConverter(new KeyToStringConverter());
        registry.addConverter(new StringToKeyConverter());

    }
}

I see the printout, so the code has been executed, although printouts I have inside converters were not.

I have simple entity:

package com.test.appconfig.datastore.entities;

import lombok.Data;

import com.google.cloud.datastore.Key;

import org.springframework.cloud.gcp.data.datastore.core.mapping.Entity;
import org.springframework.data.annotation.Id;

@Entity
@Data
public class Configuration {

    @Id
    private Key id;

    private String data;

}

And a simple repo:


@RepositoryRestResource
@Repository
@Transactional(isolation = Isolation.SERIALIZABLE)
public interface ConfigurationRepository extends DatastoreRepository<Configuration, Key>{

}

What are these missed converters and how can I register them?


Solution

  • I have found a solution already provided by spring developers here