Spring 4.x based CXF web service configuration - WildFly 10.1 - spring

We would like to migrate our Spring application from JBoss 7.1.1 to WildFly 10.1. We used CXF based and Spring managed web services but on WildFly 10.1 we couldn't configure these services.
We tried two methods.
When we use WildFly webservices subsystem the web services are published correctly but in WS implementation we can't access spring managed beans.
When we exclude webservices subsystem in the jboss-deployment-structure.xml, configure CXFServlet in web.xml and configure jaxws:endpoint in spring xml configuration file the log shows that service creation is ok, but not based on the real implementation. Wrong service name, namespace and address.
Used spring configuration:
<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:cxf="http://cxf.apache.org/core"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-jaxws.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<jaxws:endpoint id="testWs" implementor="ns.test.ws.impl.TestWsImpl"
address="/test">
<jaxws:properties>
<entry key="schema-validation-enabled" value="true" />
</jaxws:properties>
</jaxws:endpoint>
</beans>
Log content:
18:31:11,783 INFO
[org.springframework.beans.factory.xml.XmlBeanDefinitionReader]
(ServerService Thread Pool -- 58) Loading XML bean definitions from
class path resource [META-INF/cxf-beans.xml] 18:31:12,177 INFO
[org.springframework.beans.factory.xml.XmlBeanDefinitionReader]
(ServerService Thread Pool -- 58) Loading XML bean definitions from
class path resource [META-INF/cxf/cxf.xml] 18:31:12,290 INFO
[org.springframework.beans.factory.xml.XmlBeanDefinitionReader]
(ServerService Thread Pool -- 58) Loading XML bean definitions from
class path resource [META-INF/cxf/cxf-extension-jaxws.xml]
18:31:12,455 INFO
[org.springframework.beans.factory.xml.XmlBeanDefinitionReader]
(ServerService Thread Pool -- 58) Loading XML bean definitions from
class path resource [META-INF/cxf/cxf-servlet.xml] 18:31:12,960
INFO
[org.apache.cxf.wsdl.service.factory.ReflectionServiceFactoryBean]
(ServerService Thread Pool -- 58) Creating Service
{http://impl.ws.test.ns/}TestWsImplService from class
ns.test.ws.impl.TestWsImpl
WS implementation:
package ns.test.ws.impl;
import java.util.ArrayList;
import javax.jws.WebService;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import ns.test.ws.api.TestWs;
import ns.test.ws.domain.ApplicationInfo;
import ns.test.ws.domain.ApplicationInfoRequest;
import ns.test.ws.domain.ApplicationInfoResultContainer;
import ns.test.ws.domain.CallContext;
import ns.test.ws.domain.ResultContext;
import ns.test.ws.domain.ResultMessage;
#WebService(name = "ApplicationInfoService", serviceName = "ApplicationInfoService", portName = "ApplicationInfoServicePort", endpointInterface = "ns.test.ws.api.TestWs", targetNamespace = "http://test.ns/")
#Transactional
public class TestWsImpl implements TestWs {
#SuppressWarnings("unused")
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(TestWsImpl.class);
#Autowired
private VersionInfo versionInfo;
public ApplicationInfoResultContainer test(CallContext callContext, ApplicationInfoRequest appInfoRequest) {
ApplicationInfo result = new ApplicationInfo();
result.setAppName(versionInfo.getAppName());
result.setAppVersion(versionInfo.getVersion());
ResultContext resultContext = new ResultContext();
resultContext.setCorrelationId("corrId");
resultContext.setHighestMessageSeverity("W");
resultContext.setMessageList(new ArrayList<ResultMessage>());
resultContext.getMessageList().add(new ResultMessage("code", "sev", "system", "message"));
return new ApplicationInfoResultContainer(result, resultContext);
}
}
What do we wrong? How should we configure the spring application?
Used dependencies:
WildFly 10.1
CXF 3.1.6 (WildFly 10.1 module)
Spring 4.3.7

I found the solution. See my sample code on the following repo.
https://github.com/SeniorRoland/spring-wildfly

Related

java.lang.NoClassDefFoundError: org/springframework/messaging/converter/MessageConverter

I am new to Spring Integration . I am not using maven and my integration-spring.xml is located inside src/resources/integration-spring.xml and all my jars are having v4.0.5+. I am getting below exception when running my JUnit Test case.
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:99)
at org.springframework.test.context.DefaultTestContext.getApplicationContext(DefaultTestContext.java:101)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:331)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:213)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:290)
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from class path resource [resources/integration-spring.xml]; nested exception is java.lang.NoClassDefFoundError: org/springframework/messaging/converter/MessageConverter
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:414)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:336)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:181)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:217)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188)
integration-spring.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:int="http://www.springframework.org/schema/integration"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd">
<int:gateway service-interface="com.fil.MyService" id="myGateway"
default-request-channel="in" />
<int:service-activator input-channel="in" method="someMethod"
ref="exampleHandler" />
<bean id="exampleHandler" class="com.fil.MyServiceImpl" />
Test.java
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import com.fil.MyService;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = { "/resources/integration-spring.xml" })
public class MyServiceTest {
#Autowired
private MyService service;
#Test
public void test() {
try{
service.single("hELLO ");
}
catch(Exception e){
e.printStackTrace();
}
}
}
This problem may occur, when you use not compatible spring boot and spring libraries too.
I my case I had to to update spring boot to current version.
You really should use maven or gradle to manage your project's dependencies; doing it manually is just too painful - especially with regard to getting all the versions right.
NoClassDefFoundError: org/springframework/messaging/converter/MessageConverter
The org.springframework.messaging package is provided by the spring-messaging jar file from the core Spring Framework. It contains general messaging abstractions used by Spring Integration (and other Spring projects).
Since you are "new" to the framework(s), you really should be using the latest versions:
org.springframework.integration:spring-integration-core:4.3.9.RELEASE
which depends on
org.springframework:spring-messaging:4.3.8.RELEASE
by default.
v4.0.5+
You must make sure that all jars from the same project (Spring Integration or Spring Framework) have their same respective versions (spring-beans, spring-messaging, etc must match and spring-integration-* must all be the same version).

Moving Camel Spring XML to Spring Boot

I would like to move several of our camel application to Spring Boot. Most of the examples I find are using the java dsl. The application we currently support are all based on Spring XML.
What steps should I take to migrate these application to Spring Boot. If I use FatJarRouter it is my understanding that a camel context is auto-cofigured already, so it seems to me I should be able to just define routes in spring xml.
#SpringBootApplication
#ImportResource("classpath:camel-context.xml")
public class CamelApplication extends FatJarRouter {
public static void main(String[] args) {
SpringApplication.run(CamelApplication.class, args);
}
#Bean
String myBean() {
return "I'm Spring bean!";
}
}
camel-context.xml
<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.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
<routes xmlns="http://camel.apache.org/schema/spring">
<route id="test">
<from uri="timer://trigger"/>
<transform>
<simple>ref:myBean</simple>
</transform>
<to uri="log:out"/>
</route>
</routes>
</beans>
Exception
2015-11-13 15:59:13.734 WARN 2156 --- [on(5)-127.0.0.1] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Cannot locate BeanDefinitionParser for element [routes]
Offending resource: class path resource [camel-context.xml]
at org.springframework.beans.factory.parsing.FailFastProblemReporter.fatal(FailFastProblemReporter.java:60) ~[spring-beans-4.2.2.RELEASE.jar:na]
at org.springframework.beans.factory.parsing.ReaderContext.fatal(ReaderContext.java:68) ~[spring-beans-4.2.2.RELEASE.jar:na]
See the bottom of this page how to include XML routes:
https://cwiki.apache.org/confluence/display/CAMEL/Spring+Boot

ActiveMQ JMS Broker and Spring configuration

I'm pretty new to using Camel and ActiveMQ. I'm trying to run a simple code from Eclipse, without using Maven. But, it fails during Bean initialization. From the error, it seems its due to the JAR version differences, but i m failing to locate it. Kindly help me resolve this .
import org.apache.camel.CamelContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestingBroker {
private TestingBroker() {
// Helper class
}
public static void main(final String[] args) throws Exception {
AbstractApplicationContext aContext = new ClassPathXmlApplicationContext(
"classpath:META-INF/spring/camel-config-client.xml");
CamelContext ctx = (CamelContext) aContext.getBean("mycamelTemplate", CamelContext.class);
ctx.addRoutes(new BuildTwitterComponent());
ctx.start();
}
}
Here's the camel-config-client.xml file contents-
<?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:camel="http://camel.apache.org/schema/spring"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring-2.11.1.xsd">
<camel:camelContext id="my-camel-client">
<camel:template id="mycamelTemplate"/>
</camel:camelContext>
<bean id="jms" class="org.apache.activemq.camel.component.ActiveMQComponent">
<property name="brokerURL" value="tcp://135.207.178.237:61616" />
</bean>
</beans>
------------------------------
I have spring.jar.2.5.6 and camel-spring-2.11.1 JAR in the classpath. Pls suggest what am i missing here.
Thanks!
GR
Camel 2.11.1 uses Spring 3.2.3.RELEASE
Also, you'll need to have ActiveMQ 5.8.0 jars (activemq-camel, etc) in your path as well to find the org.apache.activemq.camel.component.ActiveMQComponent class

Problems with #Autowire annotation (null)

I have problems with two services that i autowired in a validator class. The services works ok because in my controller are autowired. I've an applicationContext.xml file and MyApp-servlet.xml file. My base package is es.unican.meteo and i have problems with the package es.unican.meteo.validator. The package es.unican.meteo.controller and es.unican.meteo.service can autowire the services properly.
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:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
....
some beans
...
</beans>
Myapp-servlet.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:context="http://www.springframework.org/schema/context"
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.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
<!-- Enabling Spring beans auto-discovery -->
<context:component-scan base-package="es.unican.meteo" />
<!-- Enabling Spring MVC configuration through annotations -->
<mvc:annotation-driven />
Class ResetPasswordValidator:
package es.unican.meteo.validator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import es.unican.meteo.model.User;
import es.unican.meteo.service.MessageService;
import es.unican.meteo.service.UserService;
public class ResetPasswordValidation implements Validator{
#Autowired
private UserService userService;
#Autowired
private MessageService messageService;
public boolean supports(Class<?> clazz) {
return User.class.equals(clazz);
}
public void validate(Object target, Errors errors) {
User user = (User)target;
if(userService.getUserByEmail(user.getEmail())==null){
errors.rejectValue("email", messageService.getMessage("app.error.nonexistentemail"));
}
}
}
I can see the controllers, services and autowired elements in the Spring elements. It seems like spring is not detecting the autowired properties in the package validator. Any ideas?
Edit: Log of the ResetPasswordValidation (Autowire fields)
12:48:50,697 DEBUG main support.DefaultListableBeanFactory:217 - Creating shared instance of singleton bean 'resetPasswordValidation'
12:48:50,697 DEBUG main support.DefaultListableBeanFactory:430 - Creating instance of bean 'resetPasswordValidation'
12:48:50,701 DEBUG main annotation.InjectionMetadata:60 - Found injected element on class [es.unican.meteo.validator.ResetPasswordValidation]: AutowiredFieldElement for private es.unican.meteo.service.UserService es.unican.meteo.validator.ResetPasswordValidation.userService
12:48:50,702 DEBUG main annotation.InjectionMetadata:60 - Found injected element on class [es.unican.meteo.validator.ResetPasswordValidation]: AutowiredFieldElement for private es.unican.meteo.service.MessageService es.unican.meteo.validator.ResetPasswordValidation.messageService
12:48:50,702 DEBUG main support.DefaultListableBeanFactory:504 - Eagerly caching bean 'resetPasswordValidation' to allow for resolving potential circular references
12:48:50,707 DEBUG main annotation.InjectionMetadata:85 - Processing injected method of bean 'resetPasswordValidation': AutowiredFieldElement for private es.unican.meteo.service.UserService es.unican.meteo.validator.ResetPasswordValidation.userService
Make sure you annotate the class so Spring picks it up as a bean. Autowiring can only occur on beans/classes managed by the DI container.
Adding #Component will cause the class to be picked up by Spring's component scanning, causing ResetPasswordValidation to become a bean. At this point, it should be eligible to have fields autowired.
#Component
public class ResetPasswordValidation implements Validator

Spring AnnotationHandlerMapping not working

I'm new to spring controllers using annotated controllers.
Here is my configuration
Bean definition
<bean
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
Controller
package learn.web.controller.annotation;
import javax.servlet.http.HttpServletRequest;
import learn.web.controller.BaseController;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
#Controller
public class FirstController extends BaseController {
#RequestMapping("/annotation/first.ftl")
public ModelAndView first(HttpServletRequest request) {
if(messageSource instanceof ReloadableResourceBundleMessageSource){
ReloadableResourceBundleMessageSource m = (ReloadableResourceBundleMessageSource) messageSource;
m.clearCache();
}
messageSource.getMessage("learn.message.first", new Object[] {},
localResolver.resolveLocale(request));
return new ModelAndView("/annotation/first");
}
}
When tried to access the given URL Spring is throwing a warning org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/Learn/annotation/first.ftl] in DispatcherServlet with name 'springapp'
I think what you are missing is the component scan
<context:component-scan base-package="learn.web.controller" />
Add this to your configuration and try.
This will load all annotated components from the specified package
Your configuration may look like this
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:component-scan base-package="learn.web.controller" />
<bean
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
</beans>

Resources