1

I'm using Spring Boot (v1.3.1) with Spring Data Mongo (1.8.2) and trying to add an aggregation to one of our existing Mongo repositories, but I'm having some trouble getting it all working together.

I'm referencing the Spring documentation related to adding custom behavior to a single repository.

On the application config we have the following :

@EnableMongoRepositories(basePackages = { "com.test" }, repositoryImplementationPostfix = "Impl")

To illustrate this I put together some test classes. My custom interface looks like this :

public interface TestRepositoryCustom {
TestEntity getStuff();}

My repository looks like this:

public interface TestRepository extends MongoRepository<TestEntity, String>, TestRepositoryCustom {

TestEntity findByName(@Param("name") String name);}

The implementing class is here :

public class TestRepositoryImpl implements TestRepositoryCustom {

@Override
public TestEntity getStuff(){
    System.out.println("!!!!TESTOK!!!");
    return new TestEntity();
}}

The getStuff method is not available when I look at the search URL ( http://localhost:9090/testEntities/search/ ). Also when I go to the URL ( http://localhost:9090/testEntities/search/getStuff ) I'm getting a 404.

Out of desperation I've tried changing the repositoryImplementationPostfix and have noticed that if implementing class suffix and this value are not in sync I get an error like this:

...
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property getStuff found for type TestEntity!
...

So it seems like the app context is aware of the implementing class, but I just can't figure out what I'm doing wrong.

Any input would be appreciated

Tristan
  • 279
  • 5
  • 21
  • Maybe it should be TestRepositoryCustomImpl not TestRepositoryImpl? – Orest Jan 29 '16 at 20:55
  • Could you try with other method name which isn't a getter? Getters are supposed to return properties, not to perform data access. Try with `loadStuff`, for example. – Aritz Jan 30 '16 at 10:56
  • same problem here I am starting to think that this method is not exposed through REST – Skeeve Feb 05 '16 at 17:21

2 Answers2

1

It seems that you can not do this for the rest repository, according to the answer Rest Repository not exposed.

If you want the data to look like it came from the Mongo repository you can return the data as a PagedResource, and that should be returned to look the same. You can see an example Returning Paged Resources.

It so happens I ran into a gottcha here, since we had our own WebMVCConfig which is overriding the configureMessageConverter section, it wasn't returning PagedResources correctly, so to get around this I had to add the halJacksonConverter to the list, and make the RestController method produce hal+json. In our MVCConfig I added:

@Autowired
private TypeConstrainedMappingJackson2HttpMessageConverter halJacksonHttpMessageConverter;

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    List<MediaType> halMediaTypes = new ArrayList<MediaType>();
    halMediaTypes.add(RestMediaTypes.HAL_JSON);
    halJacksonHttpMessageConverter.setSupportedMediaTypes(halMediaTypes);
    converters.add(halJacksonHttpMessageConverter);
    converters.add(new DelimitedFileMessageConverter());
}

After all that, it seems to be returning correctly.

Community
  • 1
  • 1
UtherPup
  • 26
  • 4
0

Please try renaming your class "public class TestRepositoryImpl" to "public class TestRepositoryCustomImpl" to match the custom interface name.