Hi everyone, is there a way to expose an Rest Endp...
# questions
c
Hi everyone, is there a way to expose an Rest Endpoint from a Micronaut project imported in a Grails project. My Micronaut Controller has @Controller annotation but I can not reach from the Grails application. Is there some configuration in Grails ?
j
Grails dispatches to spring controllers under the covers. I don’t think it will be possible to import the micronaut controller since the annotation based approach for url mappings in micronaut was never ported to grails.
the other block would be the micronaut databinding is different than grails - its also annotation based
c
but is there an alternative approach for the Controller in miconaut ? to be compatible with Grails ?
j
Micronaut uses Jackson databinding and uses an annotation processor to determine url mappings / parameters, I'm not saying it's not possible, but you'd have to write some type of compatibility layer between micronaut controllers & grails mappings (or add a servlet that responds to micronaut specific urls). What's interesting is this project: https://micronaut-projects.github.io/micronaut-spring/latest/guide/#springMvc. It supports calling spring controllers from micronaut, but you're wanting the opposite. Maybe see if there's a ticket under that project?
c
I am wondering if it can work, creating the Controller in the micronaut project as a Spring Controller ( so with @RestController and @GetMapping etc...) and then imported it in grails project
j
You'd have to include spring in the micronaut project. Seems like a lot of dependencies to get it working.
s
While I haven't tried it... There is the Micronaut-spring compatibility layer. Essentially allows you to use spring annotation, like
RestController
as you suggest, and use them in micronaut. So you have to use the spring flavour of annotation. It's been on my list of things to try for a while as it would allow us to share business logic between Grails/Spring and Micronaut. https://micronaut-projects.github.io/micronaut-spring/latest/guide/#springMvc Lists the "Compatible" annotations.
c
I tried but with no success, but I don't know If it is missing something on my side. Basically, going deeper what I would like to do is: • Micronaut Project with Oauth2 module activated, and so it exposes endpoints to make the oauth flow authentication ( it exposes a login and callback endpoint ) • This micronaut project should be imported into a Grails Project. Here is where I am expecting to see these two endpoints. • Unitl now I can not see any of this endpoints, neither using the Spring annotation.
s
So you can share code between the 2. i.e. Have spring controllers imported into micronaut and into spring. But you wan't be able to import a full micronaut project into spring and have things like oauth travel. Some micronaut compiled libraries can be used in spring like the HttpClient(s) but not all are compatible I don't think, You'd need a spring app with Oauth and A micronaut Project with Oath but you could share some logic via beans/controllers. There is this tutorial that might be worth looking at: https://guides.micronaut.io/latest/micronaut-spring-boot-gradle-java.html
c
@Steve Osguthorpe @jdaugherty beyond this topic, which is the correct way to inject a bean from a micronaut project to a grails project ?
s
Not entirely sure as I've not used it. But this: https://guides.micronaut.io/latest/spring-boot-micronaut-data-gradle-java.html Seems to suggest the
@Autowired
should work. Obviously your micronaut library must be compiled already and on the path, and you'll need the inject-java and maybe other dependencies in your spring boot app... I would do some investigation based on that guide.
c
but I don't understand if this is the right guide, because I need to import some @Singletons inside a Grails project
s
I'm not certain you can do that.
I think the guides suggest that you need to use the Spring
@Bean
(Not the one from micronaut) Then you can inject that bean in your micronaut app and you can also inject it in your spring app. I don't think you can have a full micronaut application that you can expose to spring. Just a shared library.
Or maybe take a look at using the post processor if you are set on using the micronaut annotations: https://micronaut-projects.github.io/micronaut-spring/latest/guide/#beanPostProcessor
c
I would like to use micronaut annotation, because my classes in Micronaut Project are annotated with Singleton, I want to use them in the Grails project
s
I do not think you can just "import" beans from a micronaut application to Grails/Spring. I haven't seen anything to suggest you can either. The closest you will be able to get is a shared library that is pre-compiled (as a jar) with the micronaut compiler. Then you can import that library into your micronaut application and your spring boot application: https://micronaut-projects.github.io/micronaut-spring/latest/guide/#micronautInsideSpring
c
yes yes, I used a shared library compiled in micronaut and I generated the jar. Then, I imported into Grails project with:
Copy code
implementation 'com......'
s
And Autowired doesn't work?
c
But I don't understand what should I change in micronaut library and what should I change in Grails project
s
I'm not understanding your question. Have you tried using
@Autowired
in your spring application after following following these docs? https://micronaut-projects.github.io/micronaut-spring/latest/guide/#sharingLibraries
c
do you mean to add in my Grails project these dependencies?
Copy code
implementation platform("io.micronaut.platform:micronaut-platform:4.0.0")
annotationProcessor platform("io.micronaut.platform:micronaut-platform:4.0.0")

annotationProcessor("io.micronaut:micronaut-inject-java")
annotationProcessor("jakarta.annotation:jakarta.annotation-api:2.1.1")
implementation("jakarta.annotation:jakarta.annotation-api:2.1.1")
implementation("io.micronaut.spring:micronaut-spring-boot-starter")
I don't understand if all of these should go to the Grails project
Basically the 5.1 section ?
s
OK. If it's Grails and not pure spring boot then try just creating a class and use Autowired to inject your bean? Have you tried Autowired. The Guides I referenced above use Autowired in tests which suggests that that should work.
Grails probably handles most of that config.
c
yes I am on Grails
s
But you should be aware that a decision was made to remove the Micronaut context in Grails 7
c
yes, right
s
So Grails 7 you might need to follow the instructions in part 5. But for Grails 6 it might just work. You should try it out as the docs I have referenced are the only docs that I have found for integrating between micronaut and Spring/Grails. If it does not work then you should maybe ask about this over on the Micronaut discord
c
but so, are you saying to import all those dependencies in the Grails project ? And then in the Grails project use the Autowired ?
s
are you saying to import all those dependencies in the Grails project
For grails No. If it was Spring(/Boot) then yes.
And then in the Grails project use the Autowired
Yes.
c
mmm ok, I already tried in Grails with just the Autowired, and it did not work
I already tried: Autowired, Inject but nothing works
s
OK. Then I would take your question to the Micronaut Discord and tell them what you have tried.
Also maybe try using @Inject instead of Autowired.
c
I tried Inject, do you suggest from jakarta or javax ?
s
I would try both. Given Grails is Micronaut 3 I think, you'll need whicever version was in use at the time. I believe micronaut switched because of licensing.
j
javax is the only one supported by grails 6.x. jakarta is supported by grails 7.x
s
That actually brings a good point. Your library is compiled with micronaut 3/4. You may need to match the grails version.
c
I have Grails 6 and Micronaut 3, and infact in Grails I can see correctly the micronaut library imported, but what it is not working is the dependency injection basically
s
Are you typing the import? i.e.
Copy code
class SomeService {
  
  @Autowired
  TheTypeFromTheLibrary theTypeFromTheLibrary
}
and not
Copy code
class SomeService {
  
  @Autowired
  def theTypeFromTheLibrary
}
c
yes I am using the type of the variable
s
OK. Then I think the best place to ask will be the Micronaut Discord. As most of the Micronaut devs don't monitor this workspace and you'll get a better catchment for your question.
1