I have created a Java class in Groovy Project in src/java folder.
I have used #Controller annotation to create a REST Webservice.
Code look like this
and then I configured component in resources.groovy
xmlns context: 'http://www.springframework.org/schema/context'
context.'component-scan'('base-package': 'com.**.**')
which matches package of the above piece of code.
When I'm trying to invoke service using URL its not able to recognize this.
Component scanning is already enabled by Grails, so delete yours. If you want annotated Spring beans to be discovered and registered, add one or more package names to the grails.spring.bean.packages list in Config.groovy, e.g.
grails.spring.bean.packages = ['com.foo.bar', 'some.other.package']
Related
I have a spring boot app in which I have added another library (in pom.xml).
This library is also a spring boot library which has created some beans and made the beans available using #Bean. The code snippet in 3rd party library is like this :
#Bean
public CustomObject customObject() {
return new CustomObject();
}
Now, I am using this library in my application, but I dont see this bean registered with my application.
I checked all the beans registered with application Context and also tried loading from application context. But no luck.
What am I missing here? The purpose of this 3rd party library is to check some config and create this bean, so if I have to manually create this bean, it takes away the whole purpose.
This is information is not enough, can you please share your pom file of library that contains CustomObject.
I need to see pom file build section code.
as suggested by #Rohit in comments, I missed to add the package of the class in the 3rd party lib jar under component scan. After adding the package under component scan, issue is resolved.
What's going on
I am using Java, Springboot
I am trying to create a simple API.
I have a package called Example.
I have two sub-packages called config and rest.
In config is the class Application, which is my spring app.
In rest is the class TheController which is the rest controller
Currently when i run the app, Application and try and go to one of the get mappings i get a white label error page.
However if i move theController to the config package i do not get this error and it's plain sailing.
What I have tried
I have tried using an import statement.
com.Example.rest.* and com.Example.rest.TheControllerwith no results.
Any help would be appreciated :)
Add a #ComponentScan on your application class.
package com.example.config;
#SpringBootApplication
#ComponentScan(basePackages = "com.example")
public class SpringBootComponentScanApp {
}
I personally think it's a good idea to put your configuration in a sub-package "com.example.config" and not in the parent package "com.example", but you need to override Spring Boot's default component scan for that case.
See also https://www.baeldung.com/spring-component-scanning
Spring Boot will only scan for components (controllers, services, repositories, ...) starting from the package of the application class (annotated with #SpringBootApplication) and below.
So best to use com.example.Application, then you can use com.example.rest.TheController and things should work.
I have a 2 Spring boot jars which work fine as 2 independent applications, however, I have been asked to merge 2 jars into a single application
The easiest thing I thought would be to add app-2 as a maven dependency into app-1 but the problem is that when the app-1 starts it only recognises the app-1 REST endpoints but ignores REST endpoint of app-2 altogether.
I was hoping that when the app-1 starts it will automatically pick the endpoints declared in app-2
#RestController
Class2{
#GetMapping(/hello-from-app2)
public String myapp2(){
return "HELLO FROM APP2"
}
This code gets ignored and at server start up I can only see the endpoints exposed by app-1 are visible.
If you are including App2.jar as a dependency into App1.jar, the best approach would be to import the Configuration of App2. If you start adding scans and stuff you would be tightly coupling you App1 to your App2. App1 would have to know implementation details of App2 that doesn't need to.
If you just import the configuration of App2, the configuration details would remain encapsulated.
I assume you have a Java Config class (or an XML Config file) for App1 and another one for App2. I also assume that the config of App2 contains all the necessary annotations for component scanning and the correct base-packages.
If that's the case, you can add an import like this and it should work right away:
#Configuration
#Import(SpringConfigurationApp2.class)
public class SpringConfigurationApp1 {
//... some beans....
}
I saw answer provided by #Diego but with that user need to make changes in client application. (ex. #Import(SpringConfigurationApp2.class) here).
I have another approach where client (App-1) does not need to make any change in application. It will just work seamlessly. This approach is by use of spring's auto configuration and same feature is used by spring-boot dependency.
Here is my answer to achieve using autoconfiguration.
For App-2,
1) create spring.factories file under resources/META-INF
2) Add org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
path-to-app-2-application/SpringConfigurationApp2
For App-1,
Just include App-2 as maven dependency and you are Done.
Here is a link to get more information about https://dzone.com/articles/what-is-spring-boot-auto-configuration.
I am new to Karaf, hence was looking for resources to create a project for RESTful web services using felix annotations and without the use of BundleActivator class(i mean by an actual class that needs to be written by me, but its ok if some compiler or maven plugin does the same for me) and blueprint xml file. So far I got success in the first part(BundleActivator part) which now after compilation auto creates my MANIFEST.MF with import and export statements, creates the relevant XML file for each component class, and packages it into a a nice jar bundle which works very well when I deploy it on Karaf container. But what is not working is the RESTful services. The bundle is deployed correctly, but the REST urls are not exposed and hence I am unable to access them.
Please help me in getting this done. I don't want to write an XML file which needs to be modified everytime there is an addition or deletion of a rest service.
Thanks
If you want to completely avoid blueprint then you should use cxf-dosgi. You simply annotate your rest service using jaxrs and publish it as an OSGi service with some special properties.
See the cxf-dosgi rest sample.
The example uses the standard DS annotation and the maven bundle plugin to create the DS component xml on the fly.
If you prefer to have blueprint at runtime then you can use the blueprint-maven-plugin. See this example.
I figured out a way to do so without using the CXF feature. That is, create a component class and in activate method get the object of ConfigurationAdmin and put the required context path against the jersy server process(using jersey publisher jar). Using this mehtod, I was able to deploy any rest/serlvet in Karaf without using blueprint.xml file. I hope this helps.
I'm developing a Spring application which shall be used by any kind of other application, no matter if that is a Spring project, a web application or even a simple single-class console application. The application who uses my project will just have to add the JAR file with my application.
So my project has a static factory class that gets and returns a bean from its Spring context which acts as an access object to access all public available functions of my project.
That part is already working.
But I need the developer of the application that uses my JAR to be able to overwrite certain configurations in my project without editing the config files in the JAR itself. At the moment those settings should be overwritable:
- the data source and hibernate bean configuration
- the jasypt (encryption) bean configuration
- the log4j settings
How do I make those settings overwriteable with configs from outside the jar?
Greetings
touchdown
Maybe a good solution would be a configuration that the user could override, for this take a look into:
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-java
Specially to #Configuration and #Bean
Maybe you could have a configuration class implemented and the user can override it. After extending the class and overwrite some methods that provides some beans the user shall inform it to your factory that will do nothing else than
new AnnotationConfigApplicationContext(userConfigurationClass);
If you want to replace the complete configuration, than the easyest way would be to have a parametrized factory that takes an alternative configuration file as its argument.
If you need it a bit more fine grain (lets say up to 10 parts), than you can split your application xml in several smaller once, and use again a configurable factory that allows to exchange the smaller xml files.
So I got a solution that is working for me.
I put an general import for override context-XMLs at the bottom of my main application context:
<import resource="classpath*:project/package/config/override/or-*.xml" />
So all the user has to do is to create the package "project/package/config/override" in his classpath (e.g. resource folder) and place matching XML files in it with new bean definitions.