java.lang.IllegalArgumentException: Prefix '' is already bound to '' - spring

sun-jaxws.xml
<?xml version="1.0" encoding="UTF-8"?>
<endpoints version="2.0" xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime">
<endpoint implementation="ways.org.commerce.inventory.service.InventoryServiceImpl" name="InventoryServiceImpl" url-pattern="/services/InventoryServiceImpl"/>
</endpoints>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-Main.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
</listener>
<servlet>
<servlet-name>InventoryServiceImpl</servlet-name>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>InventoryServiceImpl</servlet-name>
<url-pattern>/services/InventoryServiceImpl</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
InventoryServiceImpl.java
#WebService(serviceName = "InventoryServiceImpl")
public class InventoryServiceImpl implements IFInventoryService {
private IFInventoryDAO inventoryDAO;
#WebMethod(exclude = true)
public IFInventoryDAO getInventoryDAO() {
return inventoryDAO;
}
#WebMethod(exclude = true)
public void setInventoryDAO(IFInventoryDAO inventoryDAO) {
this.inventoryDAO = inventoryDAO;
}
#WebMethod(operationName = "addProduct")
public void addProduct(
#WebParam(name = "product") ProductBean product) throws CommerceBaseException {
if (inventoryDAO.productExists(product.getProductUniqueCode())) {
throw new CommerceBaseException("Product Not Found", InventoryErrorCode.PRODUCT_NOT_FOUND_ERROR);
}
inventoryDAO.addProduct(product);
}
#WebMethod(operationName = "getProduct")
public ProductBean getProduct(
#WebParam(name = "productUniqueCode") String productUniqueCode) throws CommerceBaseException {
if (inventoryDAO.productExists(productUniqueCode)) {
throw new CommerceBaseException("Product Not Found", InventoryErrorCode.PRODUCT_NOT_FOUND_ERROR);
}
return inventoryDAO.getProduct(productUniqueCode);
}
#WebMethod(operationName = "productExists")
public boolean productExists(
#WebParam(name = "productUniqueCode") String productUniqueCode) {
return inventoryDAO.productExists(productUniqueCode);
}
}
I am getting following exception
SEVERE: Exception sending context initialized event to listener instance of class com.sun.xml.ws.transport.http.servlet.WSServletContextListener
com.sun.xml.ws.transport.http.servlet.WSServletException: WSSERVLET11: failed to parse runtime descriptor: java.lang.IllegalArgumentException: Prefix '' is already bound to ''
at com.sun.xml.ws.transport.http.servlet.WSServletContextListener.contextInitialized(WSServletContextListener.java:139)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4681)
at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5184)
at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5179)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
Caused by: java.lang.IllegalArgumentException: Prefix '' is already bound to ''
at com.sun.xml.txw2.StartTag.addNamespaceDecl(StartTag.java:171)
at com.sun.xml.txw2.ContainerElement._namespace(ContainerElement.java:313)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.sun.xml.txw2.ContainerElement.invoke(ContainerElement.java:114)
at $Proxy179._namespace(Unknown Source)
at com.sun.xml.ws.wsdl.writer.WSDLGenerator.generateDocument(WSDLGenerator.java:339)
at com.sun.xml.ws.wsdl.writer.WSDLGenerator.doGeneration(WSDLGenerator.java:272)
at com.sun.xml.ws.server.EndpointFactory.generateWSDL(EndpointFactory.java:443)
at com.sun.xml.ws.server.EndpointFactory.createEndpoint(EndpointFactory.java:209)
at com.sun.xml.ws.api.server.WSEndpoint.create(WSEndpoint.java:505)
at com.sun.xml.ws.transport.http.DeploymentDescriptorParser.parseAdapters(DeploymentDescriptorParser.java:253)
at com.sun.xml.ws.transport.http.DeploymentDescriptorParser.parse(DeploymentDescriptorParser.java:147)
at com.sun.xml.ws.transport.http.servlet.WSServletContextListener.contextInitialized(WSServletContextListener.java:124)
... 8 more
20 Oct, 2012 11:17:18 PM org.apache.catalina.core.ApplicationContext log
INFO: Closing Spring root WebApplicationContext
Please help me to figure out where i am doing mistake...

There is a conflict between your web service libs and tomcat's libs.
use the folowing dependecy :
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId>
<version>2.2.8</version>
<scope>provided</scope>
</dependency>

I had the same problem.
I was using the webservice-rt-1.4 with Maven's dependency and repaired who the webservice-api jar also was coming along with COMPILE scope. Then change to PROVIDE scope in the Maven and put webservice-rt-1.4 in the tomcat.
Also check was conflicts between the XML's lib of app with the tomcat.
Hopefully this is also be the case.
Hugs!

In my case, it was the jaxws-rt.jar included in the war file generated for the web service which was causing the problem but once I cleaned and built the war again keeping dependency as provided, it worked fine

Related

org.h2.Driver version for embedded mode

My requirement is H2 database should get started while deploying my war file in embedded mode. For that I created class(DbSetup) in my java project. I mentioned the same class in web.xml . Then I implemented contextInitialized(ServletContextEvent sce) method. In that method I am making connection to h2 db to which I made connection while deploying war. While getting war file, I am getting exception like " No suitable class found for jdbc:h2:~/test1. Could you please how to approach this problem? I am binding h2-1.4.192.jar with my war. I also copied the same jar to class path.
web.xml :
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>CEMDBWS</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<listener>
<listener-class>org.h2.server.web.DbStarter</listener-class>
</listener>
<context-param>
<param-name>db.url</param-name>
<param-value>"jdbc:h2:~/test1"</param-value>
</context-param>
<context-param>
<param-name>db.user</param-name>
<param-value>sa</param-value>
</context-param>
<context-param>
<param-name>db.password</param-name>
<param-value>sa</param-value>
</context-param>
<listener>
<listener-class>DbSetup</listener-class>
</listener>
<servlet>
<servlet-name>JSON TO SQL Rest API</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>JSON TO SQL Rest API</servlet-name>
<url-pattern>/Request/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>-1</session-timeout>
<tracking-mode>COOKIE</tracking-mode>
</session-config>
<mime-mapping>
<extension>json</extension>
<mime-type>application/json</mime-type>
</mime-mapping>
my class:
try
{
Connection connection = null;
final String DB_CONNECTION = "jdbc:h2:~/test1";
final String DB_USER = "sa";
Class.forName(org.h2.Driver);
final String DB_PASSWORD = "sa";
connection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD);
return connection;
} catch (SQLException e) {
LOGGER.error("Error while getting DB connection", e);
return connection;
} catch (ClassNotFoundException e) {
LOGGER.error("Error while loading Driver", e.getMessage());
}
return connection

ServletContextParameterFactoryBean without web.xml

I´m using spring boot on a project. On that project I need to import an applicationContext.xml from another project, like the following code:
#SpringBootApplication
#EnableSwagger
#EnableEntityLinks
#ImportResource("classpath:applicationContext.xml")
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
#Bean
public CurieProvider curieProvider() {
return new DefaultCurieProvider("pm", new UriTemplate("http://www.xpto.com/docs/pm/rels/{rel}"));
}
}
One of the beans on applicationContext.xml has the following aspect:
<bean id="configLocation" class="org.springframework.web.context.support.ServletContextParameterFactoryBean">
<property name="initParamName">
<value>propertiesLocation</value>
</property>
</bean>
Now, I want to define the propertiesLocation without using a web.xml with the following:
<context-param>
<description>
</description>
<param-name>propertiesLocation</param-name>
<param-value>file:/a/b/c/application.properties</param-value>
</context-param>
I tried all the solutions that I found but without sucess (for instance How to set context-param in spring-boot). When I build the project, it always complain about the missing propertiesLocation. Is there any solution that does not involve a web.xml or modifications to the applicationContext.xml?
When I try to do a "mvn spring-boot:run", it fails with a IllegalArgumentException:
java.lang.IllegalArgumentException: Resource must not be null
at org.springframework.util.Assert.notNull(Assert.java:112)
at org.springframework.core.io.support.EncodedResource.<init>(EncodedResource.java:82)
at org.springframework.core.io.support.EncodedResource.<init>(EncodedResource.java:67)
at org.springframework.core.io.support.PropertiesLoaderSupport.loadProperties(PropertiesLoaderSupport.java:175)
at org.springframework.core.io.support.PropertiesLoaderSupport.mergeProperties(PropertiesLoaderSupport.java:156)
at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:80)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:265)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:162)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:606)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:462)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
at com.nsn.oss.pm.api.MyApplication.main(MyApplication.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.boot.maven.RunMojo$LaunchRunner.run(RunMojo.java:418)
at java.lang.Thread.run(Thread.java:724)
Another project that I'm using as guidance uses the following web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>portal</display-name>
<context-param>
<description></description>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:/datasourceContext.xml
classpath:/applicationContext.xml
classpath:/aopContext.xml
classpath:/mailContext.xml
</param-value>
</context-param>
<context-param>
<description>
</description>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:/log4j.properties</param-value>
</context-param>
<context-param>
<description>
</description>
<param-name>propertiesLocation</param-name>
<param-value>file:/a/b/c/application.properties</param-value>
</context-param>
...
So, I'm trying to configure my project like the above without the web.xml
Don't use the ServletContextParameterFactoryBean. Newer version of spring use a PropertySourcePlaceholderConfigurer. So instead use a #Value("${propertiesLocation}") which will depending on the location will use the servlet context or not to lookup the property. So if you can remove it, added advantage is that you could use system properties to override properties from the servlet context (or define them in JNDI for instance).
If you really want to configure it adding it to the application.properties should be enough. But I would strongly urge you to remove the bean all together.
A final solution is to simply override the bean with a #Bean method. Which should give you the advantage of using PropertySources.
#Autowired
private Environment env;
#Bean
public String configLocation() {
return env.getRequiredProperty("propertiesLocation");
}

Spring Injection with Servlets: NoSuchBean

New to Spring, and working with Spring 3.2.5 trying to get injection to work with a servlet in a vanilla web app (i.e., it's not a Spring MVC web app - it's a pre-existing app I'm extending using the Spring framework). The container is Tomcat 7.0.47.
My problem is that I'm getting NoSuchBeanDefinitionException errors (No bean named 'MyServlet' is defined) when I hit the servlet. There are no errors at startup, so at least one of my beans (the ServiceImplementation bean) is getting successfully instantiated. The problem appears to be with finding the HttpRequestHandler-derived bean (MyServlet) when a new HTTP request comes in.
The full stack trace for the exception is:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'MyServlet' is defined
org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:570)
org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1114)
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:279)
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:198)
org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1121)
org.springframework.web.context.support.HttpRequestHandlerServlet.init(HttpRequestHandlerServlet.java:58)
com.random.webapp.MySpringServlet.init(Unknown Source)
javax.servlet.GenericServlet.init(GenericServlet.java:160)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
java.lang.Thread.run(Thread.java:662)
I followed this pattern for my setup:
http://andykayley.blogspot.com/2008/06/how-to-inject-spring-beans-into.html
...with one minor (I think) twist. I have a class derived from HttpRequestHandlerServlet so that I can override the init method with some application-specific stuff. The extension class looks like this:
public class MySpringServlet extends HttpRequestHandlerServlet
{
public void init() throws ServletException
{
super.init();
appSpecificInit();
}
}
The servlet I want injected looks like this:
public class MyServlet implements HttpRequestHandler
{
private IService _service = null;
public void setService( IService theService ) {
_service = theService;
}
#Override
public void handleRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
_service.DoSomething();
}
}
The implementation I want it injected with looks like this:
public class ServiceImplementation implements IService
{
#Override
public void DoSomething()
{
// some code goes here
}
}
These are the relevant entries in web.xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml /WEB-INF/implementation.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>com.random.webapp.MySpringServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/myservlet/*</url-pattern>
</servlet-mapping>
</web-app>
This is the applicationContext.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<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 id="MyServlet" class="com.random.webapp.MyServlet">
<property name="Service" ref="ServiceImplementation" />
</bean>
</beans>
...and this is what implementation.xml looks like:
<?xml version="1.0" encoding="UTF-8"?>
<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 id="ServiceImplementation" class="com.random.webapp.ServiceImplementation">
</bean>
</beans>
I've been back and forth between the web.xml, applicationContext.xml, and implementation.xml files to double and triple check my configuration, and I don't see anything wrong with any of them, but I'm obviously missing something.
Anyone have any ideas?
The exception you are getting
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'MyServlet' is defined
occurs in the init() method of the HttpRequestHandlerServlet which tries to load a delegate HttpRequestHandler object from your context based on the name you give the HttpRequestHandlerServlet in your web.xml
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>com.random.webapp.MySpringServlet</servlet-class>
</servlet>
In the configuration above, that would be MyServlet. Although it appears you have it correct in
<bean id="MyServlet" class="com.random.webapp.MyServlet">
<property name="Service" ref="ServiceImplementation" />
</bean>
make sure you are loading the correct context file as declared here
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml /WEB-INF/implementation.xml</param-value>
</context-param>

extending SpringBeanAutowiringSupport with GlassFish 3.1

I'm trying to expose my web service by extending SpringBeanAutowiringSupport. I'm using GlassFish 3.1 and Spring 3.0.5.RELEASE.
I'm following the tutorial here.
However, when I go to the URL of my web service I get a ClassCastException.
Any ideas on what I'm missing or doing wrong?
WARNING: StandardWrapperValve[GreetingService]: PWC1382: Allocate exception for servlet GreetingService
java.lang.ClassCastException: com.service.GreetingServiceEndpoint cannot be cast to javax.servlet.Servlet
at com.sun.enterprise.web.WebContainer.createServletInstance(WebContainer.java:702)
at com.sun.enterprise.web.WebModule.createServletInstance(WebModule.java:1958)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1263)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:1070)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:189)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98)
at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:326)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:227)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:170)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:822)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:719)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1013)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:680)
Endpoint
#WebService (serviceName="GreetingService")
public class GreetingServiceEndpoint extends SpringBeanAutowiringSupport {
#Autowired
private GreetingService greetingService;
#WebMethod
public String sayHello () {
return greetingService.sayHello();
}
}
Service
#Service ("greetingService")
public class GreetingService {
public String sayHello () {
return "Hello from Greeting Service";
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0" metadata-complete="true">
<display-name>Archetype Created Web Application</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>GreetingService</servlet-name>
<servlet-class>com.service.GreetingServiceEndpoint</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>GreetingService</servlet-name>
<url-pattern>/GreetingService</url-pattern>
</servlet-mapping>
</web-app>
Your class: com.service.GreetingServiceEndpoint is not an instance of javax.servlet.Servlet. You can't put it into your web.xml as a servlet. To run JAX-WS web services, JAX-WS servlet has to be declared in web.xml: com.sun.xml.ws.transport.http.servlet.WSServlet this servlet will serve the requests to your web service.
The full servlet declaration should look something like this:
<servlet>
<servlet-name>Greeting</servlet-name>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Greeting</servlet-name>
<url-pattern>/services/Greeting</url-pattern>
</servlet-mapping>
Your com.service.GreetingServiceEndpoint class should be defined in sun-jaxws.xml file, something like this:
<endpoints xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime" version="2.0">
<endpoint name="Greeting" implementation="com.service.GreetingServiceEndpoint" url-pattern="/services/Greeting"/>
</endpoints>
This file has to be added into WEB-INF of your web app. Take a look at my post here. It should be helpful.
There's no need of registering com.service.GreetingServiceEndpoint as a Servlet in web.xml; as it doesn't extends HttpServlet.

Setting up Vaadin and Spring 3 - Autowiring fails (annotations)

I'm having some trouble using the #Autowire annotation of spring 3 in a vaadin application.
I have made a tiny test project with just a button which executes a method in an autowired service class.
I'm getting a NullPointerException when I click the button, because the service is never injected.
I have annotated the service with #Service("calculationService")
This is my application class:
package vaadinBaas;
// imports
public class MyVaadinApplication extends Application {
private Window window;
private CalculationService calculationService;
#Autowired
public void setCalculationService(CalculationService calculationService) {
this.calculationService = calculationService;
}
#Override
public void init() {
window = new Window("Show me the magic");
setMainWindow(window);
Button button = new Button("Click Me");
button.addListener(new Button.ClickListener() {
public void buttonClick(ClickEvent event) {
window.addComponent(new Label(String.valueOf(calculationService.multiply(10, 10))));
}
});
window.addComponent(button);
}
}
This is my spring application context:
<?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:ct="http://www.springframework.org/schema/context"
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">
<ct:component-scan base-package="vaadinBaas" />
<ct:annotation-config />
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>
</beans>
And finally my web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Vaadin Web Application</display-name>
<context-param>
<description>Vaadin production mode</description>
<param-name>productionMode</param-name>
<param-value>false</param-value>
</context-param>
<servlet>
<servlet-name>Vaadin Application Servlet</servlet-name>
<servlet-class>com.vaadin.terminal.gwt.server.ApplicationServlet</servlet-class>
<init-param>
<param-name>application</param-name>
<param-value>vaadinBaas.MyVaadinApplication</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Vaadin Application Servlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
</web-app>
Am I missing something obvious here?
Try annotating MyVaadinApplication with Component annotation. Since MyVaadinApplication is not considered as spring managed class the Autowire didnt work for you. Also you need to load the bean definition xml using ClassPathXmlApplicationContext and try calling getBean(MyVaadinApplication.class) so that all the autowiring happens automatically

Resources