Spring Boot Apache CXF JAX-RS service context path / base URI - spring-boot

I configure my JAXRS Server in Spring Boot like so:
JAXRSServerFactoryBean factoryBean = new JAXRSServerFactoryBean();
factoryBean.setBus(this.bus);
factoryBean.setFeatures(singletonList(swagger2Feature()));
factoryBean.setServiceBeans(Arrays.asList(blah(), blah2(), blah3()));
factoryBean.setAddress("/api/v1/"); // HERE
List<Object> providers = new ArrayList<>();
providers.add(new JacksonJaxbJsonProvider());
factoryBean.setProviders(providers);
BindingFactoryManager manager = factoryBean.getBus().getExtension(BindingFactoryManager.class);
JAXRSBindingFactory restFactory = new JAXRSBindingFactory();
restFactory.setBus(factoryBean.getBus());
manager.registerBindingFactory(JAXRSBindingFactory.JAXRS_BINDING_ID, restFactory);
return factoryBean.create();
However, the URLs always require /services in front, which is a nuisance (but not the end of the world). Is there any way I can remove /services and just get it deployed to the root context?

If you have not created your own CxfServlet bean you can set the path by setting cxf.path property in your application.properties file
cxf.path=/
Another way is to override ServletRegistrationBean.
#Bean
public ServletRegistrationBean cxfServletRegistration() {
String urlMapping = "/*";
ServletRegistrationBean registration = new ServletRegistrationBean(
new CXFServlet(), urlMapping);
registration.setLoadOnStartup(-1);
return registration;
}

Related

Include newly added data sources into route Data Source object without restarting the application server

Implemented Spring's AbstractRoutingDatasource by dynamically determining the actual DataSource based on the current context.
Refered this article : https://www.baeldung.com/spring-abstract-routing-data-source.
Here on spring boot application start up . Created a map of contexts to datasource objects to configure our AbstractRoutingDataSource. All these client context details are fetched from a database table.
#Bean
#DependsOn("dataSource")
#Primary
public DataSource routeDataSource() {
RoutingDataSource routeDataSource = new RoutingDataSource();
DataSource defaultDataSource = (DataSource) applicationContext.getBean("dataSource");
List<EstCredentials> credentials = LocalDataSourcesDetailsLoader.getAllCredentails(defaultDataSource); // fetching from database table
localDataSourceRegistrationBean.registerDataSourceBeans(estCredentials);
routeDataSource.setDefaultTargetDataSource(defaultDataSource);
Map<Object, Object> targetDataSources = new HashMap<>();
for (Credentials credential : credentials) {
targetDataSources.put(credential.getEstCode().toString(),
(DataSource) applicationContext.getBean(credential.getEstCode().toString()));
}
routeDataSource.setTargetDataSources(targetDataSources);
return routeDataSource;
}
The problem is if i add a new client details, I cannot get that in routeDataSource. Obvious reason is that these values are set on start up.
How can I achieve to add new client context and I had to re intialize the routeDataSource object.
Planning to write a service to get all the client context newly added and reset the routeDataSource object, no need to restart the server each time any changes in the client details.
A simple solution to this situation is adding #RefreshScope to the bean definition:
#Bean
#Primary
#RefreshScope
public DataSource routeDataSource() {
RoutingDataSource routeDataSource = new RoutingDataSource();
DataSource defaultDataSource = (DataSource) applicationContext.getBean("dataSource");
List<EstCredentials> credentials = LocalDataSourcesDetailsLoader.getAllCredentails(defaultDataSource); // fetching from database table
localDataSourceRegistrationBean.registerDataSourceBeans(estCredentials);
routeDataSource.setDefaultTargetDataSource(defaultDataSource);
Map<Object, Object> targetDataSources = new HashMap<>();
for (Credentials credential : credentials) {
targetDataSources.put(credential.getEstCode().toString(),
(DataSource) applicationContext.getBean(credential.getEstCode().toString()));
}
routeDataSource.setTargetDataSources(targetDataSources);
return routeDataSource;
}
Add Spring Boot Actuator as a dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Then trigger the refresh endpoint POST to /actuator/refresh to update the DataSource (actually every refresh scoped bean).
So this will depend on how much you know about the datasources to be added, but you could set this up as a multi-tenant project. Another example of creating new datasources:
#Autowired private Map <String, Datasource> mars2DataSources;
public void addDataSourceAtRuntime() {
DataSourceBuilder dataSourcebuilder = DataSourcebuilder.create(
MultiTenantJPAConfiguration.class.getclassloader())
.driverclassName("org.postgresql.Driver")
.username("postgres")
.password("postgres")
.url("Jdbc: postgresql://localhost:5412/somedb");
mars2DataSources("tenantX", datasourcebuilder.build())
}
Given that you are using Oracle, you could also use its database change notification features.
Think of it as a listener in the JDBC driver that gets notified whenever something changes in your database table. So upon receiving a change, you could reinitialize/add datasources.
You can find a tutorial of how to do this here: https://docs.oracle.com/cd/E11882_01/java.112/e16548/dbchgnf.htm#JJDBC28820
Though, depending on your organization database notifications need some extra firewall settings for the communication to work.
Advantage: You do not need to manually call the REST Endpoint if something changes, (though Marcos Barberios answer is perfectly valid!)

spring boot with redis

I worked with spring boot and redis to caching.I can cache my data that fetch from database(oracle) use #Cacheable(key = "{#input,#page,#size}",value = "on_test").
when i try to fetch data from key("on_test::0,0,10") with redisTemplate the result is 0
why??
Redis Config:
#Configuration
public class RedisConfig {
#Bean
JedisConnectionFactory jedisConnectionFactory() {
RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration("localhost", 6379);
redisStandaloneConfiguration.setPassword(RedisPassword.of("admin#123"));
return new JedisConnectionFactory(redisStandaloneConfiguration);
}
#Bean
public RedisTemplate<String,Objects> redisTemplate() {
RedisTemplate<String,Objects> template = new RedisTemplate<>();
template.setStringSerializer(new StringRedisSerializer());
template.setValueSerializer(new StringRedisSerializer());
template.setConnectionFactory(jedisConnectionFactory());
return template;
}
//service
#Override
#Cacheable(key = "{#input,#page,#size}",value = "on_test")
public Page<?> getAllByZikaConfirmedClinicIs(Integer input,int page,int size) {
try {
Pageable newPage = PageRequest.of(page, size);
String fromCache = controlledCacheService.getFromCache();
if (fromCache == null && input!=null) {
log.info("cache is empty lets initials it!!!");
Page<DataSet> all = dataSetRepository.getAllByZikaConfirmedClinicIs(input,newPage);
List<DataSet> d = redisTemplate.opsForHash().values("on_test::0,0,10");
System.out.print(d);
return all;
}
return null;
The whole point of using #Cacheable is that you don't need to be using RedisTemplate directly. You just need to call getAllByZikaConfirmedClinicIs() (from outside of the class it is defined in) and Spring will automatically check first if a cached result is available and return that instead of calling the function.
If that's not working, have you annotated one of your Spring Boot configuration classes with #EnableCaching to enable caching?
You might also need to set spring.cache.type=REDIS in application.properties, or spring.cache.type: REDIS in application.yml to ensure Spring is using Redis and not some other cache provider.

Creating a CacheManager from an XMLConfiguration using a StatisticsService

I know how to create a CacheManager using an XMLConfiguration in org.ehcache:ehcache:3.8.1:
import org.ehcache.config.Configuration;
import org.ehcache.xml.XmlConfiguration;
import org.ehcache.config.builders.CacheManagerBuilder;
.
.
.
URL myUrl = CacheUtil.class.getResource("/my-config.xml");
Configuration xmlConfig = new XmlConfiguration(myUrl);
cacheManager = CacheManagerBuilder.newCacheManager(xmlConfig);
cacheManager.init();
I also know how to create a CacheManager with a StatisticsService:
StatisticsService statisticsService = new DefaultStatisticsService();
CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
.using(statisticsService)
.build();
cacheManager.init();
But how do I create a CacheManager from an XMLConfiguration using a StatisticsService?
Here is a sample code that demonstrates the usage of basic JCache configuration APIs:
CachingProvider provider = Caching.getCachingProvider();
CacheManager cacheManager = provider.getCacheManager();
MutableConfiguration<Long, String> configuration =
new MutableConfiguration<Long, String>()
.setTypes(Long.class, String.class)
.setStoreByValue(false)
.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(Duration.ONE_MINUTE));
Cache<Long, String> cache = cacheManager.createCache("jCache", configuration);
cache.put(1L, "one");
String value = cache.get(1L);
Retrieves the default CachingProvider implementation from the application’s classpath. This method will work if and only if there is only one JCache implementation jar in the classpath. If there are multiple providers in your classpath then use the fully qualified name org.ehcache.jsr107.EhcacheCachingProvider to retrieve the Ehcache caching provider. You can do this by using the Caching.getCachingProvider(String) static method instead.
Retrieve the default CacheManager instance using the provider.
Create a cache configuration using MutableConfiguration
with key type and value type as Long and String respectively…​
configured to store the cache entries by reference(not by value)…​
and with an expiry time of one minute defined for entries from the moment, they are created.
Using the cache manager, create a cache named jCache with the configuration created in step <3>
Put some data into the cache.
Retrieve the data from the same cache.
There is a constructor inside the class EhcacheManager that takes 2 arguments:
public EhcacheManager(Configuration config, Collection<Service> services)
You could use it as follows:
URL myUrl = CacheUtil.class.getResource("/my-config.xml");
Configuration xmlConfig = new XmlConfiguration(myUrl);
StatisticsService statisticsService = new DefaultStatisticsService();
Set<Service> services = new HashSet<>();
services.add(statisticsService);
cacheManager = new EhcacheManager(xmlConfig, services);

How to specifiy Hibernate mappings with Spring Boot 2.0?

With Spring Boot 1.x, we could specify hibernate mapping files by extending HibernateJpaAutoConfiguration, overriding LocalContainerEntityManagerFactoryBean bean and set mapping resources, like in this answer.
Since Spring Boot 2.0 (2.0.0.M5 precisly), we can’t do this anymore because HibernateJpaAutoConfiguration has changed (with this commit) and we can’t extend the HibernateJpaConfiguration because it is package protected.
Do you know another way to specify hibernate mapping files using Spring Boot 2.0?
Thanks!
Since Spring Boot 2.0.0.M6, rather than overriding Spring Boot's internal, you should use the new spring.jpa.mapping-resources property for defining custom mappings.
Example in YML:
spring:
jpa:
mapping-resources:
- db/mappings/dummy.xml
For a complete example, check the application.yml configuration file of this repository.
private String[] loadResourceNames() {
Resource[] resources = null;
List<String> names = new ArrayList<String>();
try {
resources = ResourcePatternUtils.getResourcePatternResolver(resourceLoader).getResources("classpath: *.hbm.xml");
for ( Resource resource : resources ) {
names.addAll( Files.list( resource.getFile().toPath() )
.map ( path -> path.getFileName().toString() )
.filter ( p -> p.endsWith( "hbm.xml") )
.map ( p -> "your directory on class path".concat(p) )
.collect ( Collectors.toList() ) );
}
}catch(IOException e){
e.printStackTrace();
}
System.out.println(resources);
return names.toArray(new String[names.size()]);
}
#Primary
#Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource,EntityManagerFactoryBuilder builder) {
Properties properties = new Properties();
properties.put("hibernate.dialect","org.hibernate.dialect.H2Dialect");
properties.put("hibernate.format_sql","true");
properties.put("hibernate.show_sql","true");
//properties.put("hibernate.current_session_context_class","thread");
properties.put("hibernate.hbm2ddl.auto","create");
properties.put("hibernate.ddl-auto","create");
return builder
.dataSource(dataSource)
.packages("edu.balu.batch.migration.dataloadccp.model.target")
.properties(new HashMap(properties))
.mappingResources(loadResourceNames())
.build();
}

How to use jersey 2.0 guice on grizzly

I want to use Guice + Jersey 2.0 on Grizzly. According to this How to use guice-servlet with Jersey 2.0? discussion there is no direct Guice integration for Jersey2 at present but it can be achieved using HK2 as a bridge. I also checked the sample project in Github https://github.com/piersy/jersey2-guice-example-with-test . This project is implemented using Jetty.
But my problem is to implement it in Grizzly. On Jetty it is used like this
#Inject
public MyApplication(ServiceLocator serviceLocator) {
// Set package to look for resources in
packages("example.jersey");
System.out.println("Registering injectables...");
GuiceBridge.getGuiceBridge().initializeGuiceBridge(serviceLocator);
GuiceIntoHK2Bridge guiceBridge = serviceLocator.getService(GuiceIntoHK2Bridge.class);
guiceBridge.bridgeGuiceInjector(Main.injector);
}
My problem on grizzly is , how to get this serviceLocator object?
Thank you.
I have created the sample here
https://github.com/oleksiys/samples/tree/master/jersey2-guice-example-with-test
The Grizzly initialization code looks like this:
final URI uri = UriBuilder.fromUri("http://127.0.0.1/")
.port(8080).build();
// Create HttpServer
final HttpServer serverLocal = GrizzlyHttpServerFactory.createHttpServer(uri, false);
// Create Web application context
final WebappContext context = new WebappContext("Guice Webapp sample", "");
context.addListener(example.jersey.Main.class);
// Initialize and register Jersey ServletContainer
final ServletRegistration servletRegistration =
context.addServlet("ServletContainer", ServletContainer.class);
servletRegistration.addMapping("/*");
servletRegistration.setInitParameter("javax.ws.rs.Application",
"example.jersey.MyApplication");
// Initialize and register GuiceFilter
final FilterRegistration registration =
context.addFilter("GuiceFilter", GuiceFilter.class);
registration.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), "/*");
context.deploy(serverLocal);
serverLocal.start();
add dependecy
compile group: "org.glassfish.hk2", name: "guice-bridge", version: "2.4.0"
create feature
public class GuiceFeature implements Feature {
#Override
public boolean configure(FeatureContext context) {
ServiceLocator serviceLocator = ServiceLocatorProvider.getServiceLocator(context);
GuiceBridge.getGuiceBridge().initializeGuiceBridge(serviceLocator);
GuiceIntoHK2Bridge guiceBridge = serviceLocator.getService(GuiceIntoHK2Bridge.class);
Injector injector = Guice.createInjector(new AbstractModule() {
#Override
protected void configure() {
bind(YYY.class).to(ZZZ.class);
}
});
guiceBridge.bridgeGuiceInjector(injector);
return true;
}
}
register feature
ResourceConfig resourceConfig = new ResourceConfig();
resourceConfig.register(GuiceFeature.class);

Resources