3

I'm trying to use two dependencies in Spring Boot 3.0.0

    <dependency>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
        <version>2.0.2</version>
    </dependency>
    <dependency>
        <groupId>com.graphql-java-kickstart</groupId>
        <artifactId>graphql-spring-boot-starter</artifactId>
        <version>15.0.0</version>
    </dependency>

The problem is they both have a Bean with the name 'objectMapperProvider'.

One used in:

[org/springdoc/core/configuration/SpringDocConfiguration.class]

And the other used in:

[graphql/kickstart/autoconfigure/web/servlet/GraphQLWebAutoConfiguration.class]

The error I'm getting is:

The bean 'objectMapperProvider', defined in class path resource [org/springdoc/core/configuration/SpringDocConfiguration.class], could not be registered. A bean with that name has already been defined in class path resource [graphql/kickstart/autoconfigure/web/servlet/GraphQLWebAutoConfiguration.class] and overriding is disabled.

Is there a way around this problem or do I need to prioritize and choose what is more important to use. Find another solution.

PlickPlick
  • 197
  • 15
  • 1
    How come they are not compatible ? Is there any error you are facing ? can you elaborate more on it – Harsh Dec 21 '22 at 17:19
  • The thing with spring is a bean name as far as I know must be unique. It's like an ID how would spring know which bean to use if you had several with the same name. What I'm wondering is if there is a way to solve this. I can not rename the beans since it is not my code. – PlickPlick Dec 21 '22 at 19:27
  • Interesting. Bean declarations in both class have `@ConditionalOnMissingBean` annotation. They must be compatible by default. – Elyorbek Ibrokhimov Dec 22 '22 at 02:09
  • The classes are not even solving the same thing they are reading different properties and handeling them in a completely different way! @ConditionalOnMissingBean are not loooking for the same condition the packages are totaly unrelated. And the two classes that just happen to have the same name do not know that the other exist. Spring is just not built in a way that this works! – PlickPlick Dec 22 '22 at 20:51

2 Answers2

0

This is not the answer to my question but the solution to my problem.

Use the new spring-boot-starter-graphql (by spring!!) instead of

java-kickstarters graphql-spring-boot-starter!

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-graphql</artifactId>
</dependency>

Documented here:

https://docs.spring.io/spring-graphql/docs/current/reference/html/#overview

Thoroughly explained here with examples:

https://www.baeldung.com/spring-graphql

https://blog.devgenius.io/graphql-with-spring-boot-starter-graphql-7b406998c0b5

One small, but importatnt thing, missed in both examples. It is that the method name in the Controller needs to match the method name in the schema exactly!!

Before it was OK to have a method name in the schema like 'books' Then match that to a method with prefix 'get' in the resolvers. Now it has to match exactly!! Or you need to specify the name in the @QueryMapping annotation like below.

//ControllerCode

@QueryMapping(name = "books")
public List<Advisor> getBooks(@Argument String authorName) {
    return bookService.listByAuthorName(authorName);
}
//    Schema code

books(authorName: String!): [Books]

If you have a missmatch in schema and corresponding method you will not get an exception you will just get null back not even if you set logs to trace will you find anything wrong!

PlickPlick
  • 197
  • 15
0

After a lot of reading I have come to the conclusion that it is not possible to solve. One bean can not override the other since they are providing different functionallity.

What might work temporarily would be to implement all funktionality from both beans in one of your own making. Annotate it with @primary and give it the same name. But as soon as anything changes in their next update it would break. In short a nightmare. I would not try it unless really desperate.

PlickPlick
  • 197
  • 15