extending SpringBeanAutowiringSupport with GlassFish 3.1 - spring

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.

Related

WARNING: No mapping found for HTTP request with URI in DispatcherServlet with name 'SpringRest'

I am trying to hit the url but I am 404 error & in console I am getting warning as WARNING: No mapping found for HTTP request with URI [/spring-mvc/hello] in DispatcherServlet with name 'SpringRest'
I am not sure about the web.xml settings. My code is like below.
Any help would be appreaciated. Thanks in Advance..
web.xml
<!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>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>SpringRest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringRest</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
TestController.java
package controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.wocs.services.common.ServiceException;
import com.wocs.services.order.iface.OrderServiceIface;
import com.wocs.services.order.model.hiera.OrderHiera;
#RestController
public class TestController {
#RequestMapping("/hello")
public String hello() throws ServiceException
{
return "Hello............";
}
}
SpringRest-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.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" >
<mvc:annotation-driven/>
<context:annotation-config></context:annotation-config>
<context:component-scan base-package = "com.wocs" />
<import resource="classpath:/beanfactory/service-bean-config.xml" />
</beans>
It might be the #RequestMapping("/hello") as you're NOT defining what type of method is used to receive the request.
Try this
#GetMapping(value="/hello", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> hello() throws ServiceException
{
return new ResponseEntity<>("Hello............", HttpStatus.OK);
}
You can modify your Controller class like this and tell me if it works
#Controller
public class TestController {
#GetMapping("/hello")
public String hello() throws ServiceException
{
return "Hello............";
}
}
if this doesn't work you can try
#Controller
public class TestController {
#RequestMapping(value = "/hello", method = RequestMethod.GET, produces = "application/json")
public String hello() throws ServiceException
{
return "Hello............";
}
}
web.xml
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>SpringRest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SpringRest</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
check that your web.xml file and SpringRest-servlet.xml file is in webapp/WEB-INF folder, also check that your controller java class is in src/main/java and not in src/main/test , most of the time improper directory structure causes the problem. Let me know if it still does not work.
Add this to your web.xml file:-
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/SpringRest-servlet.xml</param-value>
</context-param>
<listener>
<listener class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

Disable default search of rest services in JBoss EAP 6.3

I need to disable default rest easy search in JBoss EAP 6.3.
I need to deploy the same EAR in JBoss as well as weblogic and websphere, hence cannot make changes in web.xml that is specific to JBoss.
Java Class:
package com.first;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
#Path("/hello")
public class Hello {
#GET
#Produces(MediaType.TEXT_PLAIN)
public String sayPlainTextHello() {
return "Hello sayPlainTextHello";
}
#GET
#Produces(MediaType.TEXT_XML)
public String sayXMLHello() {
return "<?xml version=\"1.0\"?>" + "<hello> Hello sayXMLHello" + "</hello>";
}
#GET
#Produces(MediaType.TEXT_HTML)
public String sayHtmlHello() {
return "<html> " + "<title>" + "Hello sayHtmlHello" + "</title>"
+ "<body><h1>" + "Hello String" + "</body></h1>" + "</html> ";
}
}
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>com.first</display-name>
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.first</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<context-param>
<param-name>resteasy.scan</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<param-name>resteasy.scan.providers</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<param-name>resteasy.scan.resources</param-name>
<param-value>false</param-value>
</context-param>
</web-app>
jboss-deployment-structure.xml
<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.2">
<deployment>
<exclude-subsystems>
<!-- Disable the default JAX-RS subsystem -->
<subsystem name="jaxrs" />
</exclude-subsystems>
</deployment>
</jboss-deployment-structure>
This throws an error: The ResourceConfig instance does not contain any root resource classes.
If I remove below lines from web.xml, it works fine:
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.first</param-value>
</init-param>
Can anyone please suggest me what should be the ideal approach without making changes in web.xml(ideally it should work in all servers)
Thanks
I haven't tested this myself, but have you tried just excluding the resteasy modules, and not the entire jaxrs subsystem? The jaxrs module is likely required to be on the classpath to support your jersey implementation.
Specifically:
resteasy-atom-provider
resteasy-cdi
resteasy-hibernatevalidator-provider
resteasy-jackson-provider
resteasy-jaxb-provider
resteasy-jaxrs
resteasy-jettison-provider
resteasy-jsapi
resteasy-multipart-provider
resteasy-spring
resteasy-yaml-provider

Vaadin and Spring integration - #Inject doesn't work

I'm trying to integrate Vaadin with Spring. In my main Vaadin application class I have:
public class MyVaadinApplication extends UI {
#Inject
private PrivatePersonBo privatePersonBo;
#Override
public void init(VaadinRequest request) {
Layout layout = new FormLayout();
layout.setCaption("New Private Person");
setContent(layout);
ApplicationContext appContext = new ClassPathXmlApplicationContext("resources/spring/Context.xml");
appContext.getBean(MyVaadinApplication.class);
PrivatePersonBo privatePersonBo = (PrivatePersonBo) appContext.getBean("privatePersonBo");
PrivatePerson existingEmployee = privatePersonBo.findByName("TestUserName");
}
}
If I get bean from context directly I recieve bean, however If I comment line which recieves bean from appContext then #Inject annotation doesn't work and I get NullPointerException. My deployment descriptor is below:
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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_3_0.xsd"
version="3.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:resources/spring/Context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>VaadinApplicationServlet</servlet-name>
<servlet-class>com.vaadin.server.VaadinServlet</servlet-class>
<init-param>
<param-name>UI</param-name>
<param-value>pl.adamsalata.MyVaadinApplication</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>VaadinApplicationServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
Any suggestions please?
You can integrate Vaadin 7 with Spring in two simple steps:
1) Create a UIProvider that lookup UIs in spring context:
public class SpringUIProvider extends DefaultUIProvider {
#Override
public UI createInstance(UICreateEvent event) {
ApplicationContext ctx = WebApplicationContextUtils
.getWebApplicationContext(VaadinServlet.getCurrent().getServletContext());
return ctx.getBean(event.getUIClass());
}
}
2) Declare the UIProvider in web.xml:
<context-param>
<param-name>UIProvider</param-name>
<param-value>org.example.SpringUIProvider</param-value>
</context-param>
And remember to use prototype scope for UI classes.
If you want to integrate Vaadin with Spring then use #Configurable feature. Then all instantiated objects (even if you create them using new MyObject() code) will be integrated with Spring context. You can find more details about such setup in Spring documentation: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/aop.html#aop-using-aspectj

Does the new Spring MVC Test Framework released in Spring 3.2 test the web.xml configuration?

I've read the docs ( http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/testing.html#spring-mvc-test-framework ) several times and I can't confirm if the WebApplicationContext context that gets injected when you use the #WebApplicationContext annotation is actually looking at the web.xml.
In other words, I want to test my web.xml configuration. The filters and servlet path specifically. But when I configure my test it ignores the web.xml. (e.g. I try a get request on a URL like this /myServletPath/foo and it fails with a 404.)
My test:
#RunWith(SpringJUnit4ClassRunner.class)
#WebAppConfiguration
#ContextConfiguration({
"classpath*:WEB-INF/config/application-context.xml",
"classpath*:WEB-INF/oms-servlet.xml",
"classpath*:persistence-context.xml"
})
public class OrderSummaryControllerIntegrationTests {
#Autowired
private WebApplicationContext wac;
private MockMvc mockMvc;
#Before
public void setUp() throws Exception {
this.mockMvc = webAppContextSetup(this.wac).build();
}
#Test
public void testFindOrderSummariesExpectsSuccess() throws Exception {
mockMvc.perform(get("/oms/orders?user=1234&catalog=bcs"))
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON));
}
}
And my web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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"
version="2.5">
<display-name>OMS REST Services</display-name>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
<filter>
<filter-name>webappMetricsFilter</filter-name>
<filter-class>com.yammer.metrics.web.DefaultWebappMetricsFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>webappMetricsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/application-context.xml, classpath*:persistence-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>oms</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>oms</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
You are right, Spring-mvc-test does not read the web.xml file, but you can configure the filters this way:
webAppContextSetup(this.wac).addFilter(new DefaultWebappMetricsFilter(), "/*").build()

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