Spring Properties file not auto wiring, #Value values always NULL - java

I have a Spring web application (RestFul web service)
I have the following in my applicationContext.xml to read properties file:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="ignoreResourceNotFound" value="true" />
<property name="properties" ref="appProperties" />
</bean>
<bean id="appProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list>
<value>classpath:common.properties</value>
<value>classpath:local.properties</value>
<value>classpath:${XPLAT_ENV}.properties</value>
</list>
</property>
</bean>
And my Rest API is like this :
#Path("/login")
#Component
public class CLSAmple {
#Autowired
#Qualifier("appProperties")
protected Properties appProperties;
#Value("${ui.server.endpoint}")
private String uiservername;
#GET
#Produces(MediaType.TEXT_PLAIN)
public String getLoginInformation() {
if(appProperties != null){
System.out.println("AppProperties is NOT null");
String uiServer = appProperties.getProperty("ui.server.endpoint");
return uiServer;
}
System.out.println("AppProperties is NULL");
System.out.println("#Value is " + uiservername);
return "Hello Java!!";
}
}
But my #Autowired appProperties and #Value to get a particular property is always NULL. I have looked into similar posts about injecting values from properties file and tried all possible things suggested but it is still NULL.
Also tried Annotation only approach using
#PropertySource("classpath:common.properties")
but same results.
Thanks
Edited: Here is the applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:metrics="http://www.ryantenney.com/schema/metrics"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd
http://www.ryantenney.com/schema/metrics
http://www.ryantenney.com/schema/metrics/metrics.xsd">
<context:component-scan base-package="com.cl.connect" />
<context:annotation-config/>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="ignoreResourceNotFound" value="true" />
<property name="properties" ref="appProperties" />
</bean>
<bean id="appProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list>
<value>classpath:common.properties</value>
</list>
</property>
</bean>
</beans>

I found what the issue was, like someone mentioned in the comments above,
I had the integration wrong, I added following to my web.xml and it worked:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>

Related

Spring jdbc #Transactional annotation, procedure commits the transaction despite throwing appropriate exception

Namastey,
We have a class and its methods as below-
#Service
#Component
public class ReqServiceImpl implements ReqService {
#Autowired
private SubmitDAO submitDAO;
#Override
#Transactional(readOnly = false, rollbackFor = { CustomException.class },propagation=Propagation.REQUIRES_NEW)
public Map initiateSubmission(DetailsDTO vo) CustomException{
//it has two method defined within the same class
if(condition){
Map resultMap = submitRequest1(map,vo.isTestMode());
}else{
Map result = (HashMap) submitRequest2(map,flag,vo.isTestMode());
}
}
#Transactional(readOnly = false, rollbackFor = { CustomException.class }, propagation=Propagation.REQUIRED)
public void submitRequest1(Map map,boolean testMode) CustomException{
submitDAO.simpleTableInsert1(map);
submitDAO.simpleTableInsert2(map);
submitDAO.procedureCall(map);
if(!"success".equalIgnorecase(map.get("output"))){
throw new CustomException("Service Layer | submitRequest1 | Proc Not successs");
}
}
#Transactional(readOnly = false, rollbackFor = { CustomException.class },
propagation=Propagation.REQUIRED)
public void submitRequest2(Map map,boolean testMode) throws CustomException{
submitDAO.simpleTableInsert1(map);
submitDAO.simpleTableInsert2(map);
submitDAO.procedureCall(map);
}
}
Below is our spring-servlet.xml file
In this, we have added our configuration for annotation, proxy and default advice to proxy.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:annotation-config />
<context:component-scan base-package="com.company,com.company.security.filter,org.springframework.jdbc" />
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg index="0" name="defaultCharset"
value="UTF-8" />
</bean>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
</mvc:message-converters>
</mvc:annotation-driven>
<aop:aspectj-autoproxy proxy-target-class="true"/>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean
class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator">
<property name="proxyTargetClass" value="true" />
</bean>
<bean id="LogAspect" class="com.company.common.aop.LogAspect">
</bean>
<bean id="PerfAspect" class="com.company.common.aop.PerfAspect">
</bean>
<bean id="SessionAspect" class="com.company.common.aop.SessionAspect">
</bean>
<bean id="mailProperties"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" >
<list>
<value>classpath:db.properties</value>
<value>classpath:dSource.properties</value>
</list>
</property>
<property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>
<bean id="myDataSource" class="org.springframework.jndi.JndiObjectFactoryBean" primary="true">
<property name="jndiName" value="${DEVJNDI}"></property>
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="myDataSource" />
</bean>
<bean id="DataSourceCo" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="${JNDINAMECOM}"></property>
</bean>
<bean id="transactionManager1"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="DataSourceCo" />
</bean>
<bean id="myDataSourceI" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="${JNDINAMEI}"></property>
</bean>
<bean id="transactionManager2"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="myDataSourceI" />
</bean>
<!-- Create a proxy to generate session-scope -->
<bean id="userBean"
class="com.company.common.session.UserDetailsSessionBean"
scope="session">
<aop:scoped-proxy />
</bean>
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
/>`enter code here`
</beans>
The method 'procedureCall' which is in another class has a procedure call inside.
The procedure does not have any commit inside.
After, the procedure return with any error, we throw an exception, but all in vain. The insert done by procedure still gets committed and transaction does not rollback.
The proc gives output "error" or "succcess", if there was any error.
If we get any error "error", then we throw the customized exception.
It should rollback everything done inside proc, but that does not happen.
Kindly suggest.

Spring MVC Rest App - Best Practice to Loading Properties

Have been assigned to re-assess a Spring 4 MVC Rest app where previous developers
where placing the loading of the configuration properties inside the following places:
WEB-INF/mvc-dispatcher-servlet.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<import resource="classpath:mydatabase.xml" />
<context:component-scan base-package="com.myapp.rest, com.myapp.config" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<util:properties id="props" location="classpath:prop.properties" />
</beans>
src/main/resources/mydatabase.xml:
<bean id="propertyPlaceholder"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreResourceNotFound" value="true" />
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="location" value="file:/opt/database.properties">
</property>
<bean id="mydatabase" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName"><value>${db.driver}</value></property>
<property name="url"><value>${db.url}</value></property>
<property name="username"><value>${db.username}</value></property>
<property name="password"><value>${db.password}</value></property>
<property name="maxIdle" value="10" />
<property name="maxActive" value="50" />
<property name="maxWait" value="100" />
<property name="defaultAutoCommit" value="false" />
<property name="removeAbandoned" value="true" />
<property name="removeAbandonedTimeout" value="1" />
<property name="minIdle" value="0"></property>
<property name="timeBetweenEvictionRunsMillis" value="1000"></property>
<property name="minEvictableIdleTimeMillis" value="1000"></property>
</bean>
src/main/resources/prop.properties:
banner = images/banner.png
Inside the code, I've been seeing people use the follow way to insert the banner file location:
private #Value("#{props[banner]}") String banner;
My objective is to add a new properties file:
src/main/resources/config.properties
So, I can use the #Value annotation...
Question(s):
What's the BEST way to reorganize some of these config files?
Where would I declare this new config.properties file and what is the declaration?
Put the property files in src/main/resources.
2.
#Configuration
#PropertySource("classpath:config.properties")
public class PropertiesWithJavaConfig {
#Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
If you want to load multiple files, use PropertySources
PropertyPlaceholderConfigurer is deprecated in favor of PropertySourcesPlaceholderConfigurer for years now. I hope you'll ditch the XML and move to JavaConfig for, if not anything else, type safety.

Access property values inside Spring config file

I have a simple properties file at WEB-INF/local.db.properties:
db.driverClassName=org.postgresql.Driver
db.url=jdbc:postgresql://localhost:5432/db_name
db.username=postgres
db.password=password
I am trying to access these properties inside of my Spring configuration file (towards the end):
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="controllers" />
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles-config.xml</value>
</list>
</property>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.tiles3.TilesView"/>
</bean>
<util:properties id="dbProperties" location="WEB-INF/local.db.properties" />
<bean id="dbDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${db.driverClassName}" /> <!--ERROR -->
<property name="url" value="${db.url}" />
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
</bean>
</beans>
The error is Could not load JDBC driver class [${db.driverClassName}]. The error is pretty clear to me - it is not trying to look up the property it is just using the raw string.
How do I fix this?
This is a Maven 3, Spring 4, Postgresql project.
You're not loading the properties file in Spring configuration. Add this line:
<context:property-placeholder location="classpath:/WEB-INF/local.db.properties"/>
or
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="/WEB-INF/local.db.properties"/>
</bean>
If you have several properties file, change the above configuration for:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<!-- locations, not location, check the S at the end -->
<property name="locations">
<list>
<value>
/WEB-INF/local.db.properties
</value>
<value>
<!-- Path of another properties file -->
</value>
</list>
</property>
</bean>
More info: PropertySourcesPlaceholderConfigurer
If you are using Eclipse, try right click on project -> Properties -> Java Build Path -> Source, then Add Folder or Link Source to your property files.

Spring/JPA data persisting in unit test but not in application

I'm unable to persist data in my Spring/JPA/Tomcat application by calling my userService but when I call it from my unit test the data gets written to the database. Nor is there any exception thrown when calling the service from my controller.
Controller class:
#Controller
#RequestMapping("/")
public class AccessManagementController {
#Autowired
private UserService userService;
#Autowired
private ApplicationProperties applicationProperties;
#RequestMapping(method = RequestMethod.GET, value = "/register")
:
:
:
userService.createNewUser(username, password);
model.addAttribute("loginMessage", "Registration successful; you can now login");
return "/access";
}
}
Working unit test:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = {
"classpath:/spring/applicationContext.xml",
"classpath:/spring/securityContext.xml",
"classpath:/spring/jpaContext.xml"
})
public class UserServiceTest {
#Autowired
private UserService userService;
#Test
public void userServiceSaveUserTest() {
String testUsername = (new Date()).toString();
userService.createNewUser(testUsername, "password");
User findUser = userService.findByUsername(testUsername);
Assert.assertNotNull(findUser);
Assert.assertEquals(findUser.getUsername(), testUsername);
}
}
applicationContext.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="com.bytediary"/>
<bean id="applicationProperties" class="com.bytediary.util.ApplicationProperties">
<property name="location" value="classpath:application.properties"/>
</bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<bean class="org.springframework.orm.hibernate4.HibernateExceptionTranslator"/>
<mvc:annotation-driven/>
<mvc:resources mapping="/resources/**" location="/resources/"/>
</beans>
jpaContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<context:annotation-config />
<context:component-scan base-package="com.bytediary.entity" />
<jpa:repositories base-package="com.bytediary.repository"/>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="default"/>
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.bytediary.entity" />
<property name="persistenceXmlLocation" value="classpath:/jpa/persistence.xml" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true"/>
<property name="database" value="MYSQL" />
<property name="generateDdl" value="true" />
</bean>
</property>
</bean>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
<tx:annotation-driven proxy-target-class="true" transaction-manager="transactionManager" />
</beans>
persistence.xml
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="default" transaction-type="RESOURCE_LOCAL">
<class>com.bytediary.entity.User</class>
</persistence-unit>
</persistence>
1 . You do not need
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
Explanation here:
http://static.springsource.org/spring/docs/2.5.6/api/org/springframework/orm/jpa/support/PersistenceAnnotationBeanPostProcessor.html
Note: A default PersistenceAnnotationBeanPostProcessor will be
registered by the "context:annotation-config" and
"context:component-scan" XML tags. Remove or turn off the default
annotation configuration there if you intend to specify a custom
PersistenceAnnotationBeanPostProcessor bean definition.
2 . Try adding #Transactional to UserService.createNewUser().

jaxrs with embedded jetty configuration problem

I'm trying to get a web service up using jaxrs and jetty:
This is my jaxrms.xml file:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cxf="http://cxf.apache.org/core"
xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xsi:schemaLocation="
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<!-- import resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml" /-->
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<bean id="restService" class="com.as.rover.service.rest.RestService" >
</bean>
<jaxrs:server id="jaxrsRestService" address="/rest/">
<jaxrs:serviceBeans>
<ref bean="restService" />
</jaxrs:serviceBeans>
</jaxrs:server>
</beans>
This is my jetty.xml file:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean class="com.as.rover.service.JettyManager" factory-method="getInstance" id="jettyManager">
<property name="server" >
<bean id="jetty-server" class="org.eclipse.jetty.server.Server" init-method="start" destroy-method="stop">
<property name="connectors">
<list>
<bean id="Connector" class="org.eclipse.jetty.server.nio.SelectChannelConnector">
<property name="port" value="8080"/>
</bean>
</list>
</property>
<property name="handler">
<bean id="handlers" class="org.eclipse.jetty.server.handler.HandlerList">
<property name="handlers">
<list>
<ref bean="servletContextHandler"></ref>
<!--bean class="org.eclipse.jetty.server.handler.ResourceHandler">
<property name="directoriesListed" value="true"/>
<property name="welcomeFiles">
<list>
<value>index.html</value>
</list>
</property>
<property name="resourceBase" value="."/>
</bean>
<bean class="org.eclipse.jetty.server.handler.DefaultHandler"/-->
<!-- add more handlers here -->
</list>
</property>
</bean>
</property>
</bean>
</property>
</bean>
<bean id="web-context" class="org.eclipse.jetty.webapp.WebAppContext">
<property name="resourceBase" value="./src/main/web"></property>
<property name="contextPath" value="/services/*"></property>
<bean id="servletContextHandler" class="org.eclipse.jetty.servlet.ServletContextHandler">
<property name="contextPath" value="/" />
</bean>
<bean id="javaVersion" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject" ref="servletContextHandler"/>
<property name="targetMethod" value="addServlet"/>
<property name="arguments">
<list>
<bean class="org.eclipse.jetty.servlet.ServletHolder">
<property name="name" value="services" />
<property name="servlet">
<bean class="org.apache.cxf.transport.servlet.CXFServlet"/>
</property>
</bean>
<value>/</value>
</list>
</property>
</bean>
</beans>
My rest service class looks like this:
#Path("/test") // bind to versionnr in path
public class RestService{
#GET
public long get() {
return 1L;
}
}
Whenever I make a request to localhost:8080/services/test I get the following error message:
service not found.
I want to configure my embedded jetty server with jaxrs but it doesn't seem to work. Have I misconfigured jetty?
If you are using Spring, and all your dependencies are well placed, the only thing you need to expose a restful service is your first file.
cxf-rt-transports-http-jetty will take care of the link between your service code and the jetty server.
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd">
<context:annotation-config />
<bean id="serviceImpl" class="com.as.rover.service.rest.serviceImpl" ></bean>
<!-- CXF -->
<import resource="classpath:META-INF/cxf/cxf.xml" />
<jaxrs:server id="JaxrsService" address="http://localhost:6066/services">
<jaxrs:serviceBeans>
<ref bean="serviceImpl" />
</jaxrs:serviceBeans>
</jaxrs:server>
Hope it helps.

Categories

Resources