SpringBoot HAPI FHIR JPA Server

2,440 views
Skip to first unread message

kevin.m...@xml-solutions.com

unread,
Apr 15, 2017, 6:27:40 AM4/15/17
to HAPI FHIR
Hi,

Has anyone got this working? What I want to do is configure JPA server to run in an embedded tomcat. 

but I couldn't get it to work. 

I have springBoot working with hawtio, Swagger, Apache Camel and HAPI FHIR Libs here https://github.com/KevinMayfield/FHIRTest/tree/master/eRSSwagger but I want to replace the external server calls with an embedded one.

Kev

Message has been deleted

mcc...@gmail.com

unread,
Apr 17, 2017, 3:02:24 PM4/17/17
to HAPI FHIR
This doesn't use the Hapi JPA server, but does illustrate the Spring Config to register a Hapi server to run in embedded Tomcat with SpringBoot. 

import java.util.Map;

import javax.servlet.ServletException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.embedded.FilterRegistrationBean;
import org.springframework.boot.context.embedded.ServletRegistrationBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.rest.server.IResourceProvider;
import ca.uhn.fhir.rest.server.RestfulServer;

@Configuration
@EnableAutoConfiguration
//comp scan to find package of resource providers; if you want to auto detect them
@ComponentScan(basePackageClasses = { path.to.package.resourceproviers.myPatientProvider.class })
public class FhirServerV1Config {

@Autowired
private ApplicationContext appContext;

@Bean
public FhirServer fhirServlet(FhirContext fhirCtx) {
return new FhirServer(fhirCtx) {

private static final long serialVersionUID = 1L;

@Override
protected void initialize() throws ServletException {
        //register resource providers manually or by discovering them through component scanning
        Map<String, IResourceProvider> resourceProviders = appContext.getBeansOfType(IResourceProvider.class);
setResourceProviders(resourceProviders.values());

Map<String, IServerBaseAware> serverAwares = appContext.getBeansOfType(IServerBaseAware.class);
for (IServerBaseAware serverAware : serverAwares.values())
serverAware.setServerBase(this);
}
};
}

@Bean
public ServletRegistrationBean fhirServletRegistrationBean(RestfulServer fhirServlet) {
ServletRegistrationBean s = new ServletRegistrationBean(fhirServlet, "/fhir/mybase/*");
s.setName("my server");
return s;
}
/**
* This enables CORS support
*/
@Bean
public FilterRegistrationBean corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
//config.addAllowedOrigin("http://example.com");
config.addAllowedOrigin("*");
config.addAllowedHeader("*");
config.addAllowedMethod("*");
source.registerCorsConfiguration("/**", config);
FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
bean.setOrder(0);
return bean;
}
}

 

contac...@gmail.com

unread,
Apr 20, 2017, 11:16:02 AM4/20/17
to HAPI FHIR
Hi,

I tried both the method documented in the above link and also tried what  mcc suggested, I keep getting the error:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'myResourceProvidersDstu3' available

at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:687) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]

at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1207) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]


Can someone post the config we need to do to make it work in spring boot.


Thanks

Ravi Kuchi

kevin.m...@xml-solutions.com

unread,
Apr 20, 2017, 11:27:25 AM4/20/17
to HAPI FHIR, contac...@gmail.com
I managed to get past that part. I'll look at getting my code on github tomorrow, it's not working as I had problems with web application context.  Hopefully mccraig's post has the missing link

thanks

mcc...@gmail.com

unread,
Apr 24, 2017, 10:14:52 AM4/24/17
to HAPI FHIR, contac...@gmail.com
NOTE: My code above is missing a Bean definition for the FhirContext. You will need to create a Bean to define it. 
And the fhirServlet bean initialization will need to be customized to fit your needs. In my case, I am defining components for all the ResourceProviders elsewhere in the codebase, so the code above dynamically discovers all the ResourceProviders and registers them to the FhirServer. Other use cases will likely be different, and I assume the JPAServer code has it's own ResouceProviders which are likely not annotated such that Spring can discover them in the same way. 

kevin.m...@xml-solutions.com

unread,
Apr 25, 2017, 10:51:17 AM4/25/17
to HAPI FHIR, contac...@gmail.com, mcc...@gmail.com
Many many thanks, that worked.

Seems the problem I had was how coded the servlet registration bean.

So this section fixed it

@Bean
public ServletRegistrationBean fhirServletRegistrationBean(RestfulServer fhirServlet) {
ServletRegistrationBean s = new ServletRegistrationBean(fhirServlet, "/fhir/mybase/*");
s.setName("my server");
return s;
}

kevin.m...@xml-solutions.com

unread,
Apr 29, 2017, 5:28:52 AM4/29/17
to HAPI FHIR
Had the same problem with providers which I resolved by dropping my HAPI FHIR version to 2.2 (need to investigate but it seems to be related to Hibernate Search 5.7 and spring boot 1.5.2

See 
 


Which uses Apache Derby, Hawtio, embedded tomcat and a Dstu2 HAPI JPA Server. It doesn't include the HAPI FHIR overlay. 

 

Kev

Reply all
Reply to author
Forward
0 new messages