I am fairly new to Apache CXF and tomcat. I am trying to build a simple web service and deploy it on tomcat. below is my web.xml
However when I try to access the 'services' folder using my browser it says No services have been found. I tried creating java web service client but it is not able to locate the service either. What could be wrong in this?
<?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">
<display-name>Sample web service provider</display-name>
<listener>
<!-- For Metro, use this listener-class instead:
com.sun.xml.ws.transport.http.servlet.WSServletContextListener -->
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- Remove below context-param element if using Metro -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:META-INF/cxf/cxf.xml
</param-value>
</context-param>
<servlet>
<servlet-name>WebServicePort</servlet-name>
<!-- For Metro, use this servlet-class instead:
com.sun.xml.ws.transport.http.servlet.WSServlet -->
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>WebServicePort</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>60</session-timeout>
</session-config>
</web-app>
This means that you don't have any services exposed in your application. Your web.xml seems to be correct but I've just missed one thing, your Spring configuration. Add your Spring config location in your web.xml, for e.g.:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/applicationContext.xml</param-value>
</context-param>
Also, you have to create a class which will implement your web service interface and expose it as the CXF endpoint in your Spring applicationContext.xml configuration file. For e.g.:
<bean id="candidateImpl" class="some.pckg.CandidateImpl"/>
<jaxws:endpoint id="candidateEndpoint"
implementor="#candidateImpl"
address="/Candidate"
/>
Your CandidateImpl class should have #WebService annotation. For e.g.:
#WebService(targetNamespace = "http://something.com/ws/candidate",
portName = "CandidateService",
serviceName = "Candidate",
endpointInterface = "some.pckg.types.CandidateService",
wsdlLocation = "WEB-INF/wsdl/CandidateService.wsdl")
public class CandidateImpl implements CandidateService {
//Implementation of all methods from CandidateService.
}
If you've done everything correctly you should see that there is one service available under:
http(s)://whateverhost.com:<somePort>/SomeContextPath/services
And you should be able to get the WSDL file like this:
http(s)://whateverhost.com:<somePort>/SomeContextPath/services/Candidate?wsdl
See also:
Writing a Web Service with Spring
You need to set the spring configuration file location to make this work. You can set it as follows.
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/applicationContext.xml</param-value>
</context-param>
You need to configure a servlet in your web.xml. Below an example.
<?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>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<display-name>CXF Servlet</display-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<init-param>
<param-name>config-location</param-name>
<param-value>/WEB-INF/spring-ws-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
</web-app>
Now you need to define a file named spring-ws-servlet.xml under WEB-INF folder. Below an example of the content of spring-ws-servlet.xml, which contains the actual configuration for your web service. This depends on your logic, of course:
<?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:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="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://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<context:component-scan base-package="com.sample.service"/>
<!-- JAX-WS Service Endpoint -->
<bean id="personImpl" class="com.sample.service.impl.PersonServiceImpl"/>
<jaxws:endpoint id="personEndpoint"
implementor="#personImpl"
address="/person">
<jaxws:properties>
<entry key="schema-validation-enabled" value="true"/>
</jaxws:properties>
</jaxws:endpoint>
<!-- JAX-WS Service Endpoint End-->
</beans>
With this, you can access your web service under http://localhost:8080/services/person?wsdl
This is taken from this post. It is a tutorial about creating a Cxf service with IntelliJ Idea and Spring
https://aldavblog.wordpress.com/2015/01/22/creating-a-web-service-from-scratch-using-spring-maven-apache-cxf/
Related
I have made a Java project having spring controller that handle url
Here is the controller code
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
#EnableWebMvc
#Controller
public class SmartContentValidator {
#RequestMapping(value = "/validate")
public String validate() {
System.out.println("Yo");
return "YO";
}
}
Then I exported above java project as jar file.
Then created a new web project and added the above jar in build path. Here is the web.xml and dispatcher-servlet.xml
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>SmartContentValidatorTest</display-name>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
dispatcher-servlet.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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan base-package="com" />
<context:annotation-config/>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
Now when I try to hit http://localhost:8080/project_name/validate
it shows 404-not found on browser and
WARNING: No mapping found for HTTP request with URI [/SmartContentValidatorTest/validate/] in DispatcherServlet with name 'dispatcher'
in my eclipse console.
What is the issue? Am I missing something?
Edit
Project Structure
I am not sure if it helps with your case but I think that in standalone application there would be an issue as #EnableWebMvc should be used with Java config - class annotated with #Configuration, not with #Controller bean.
As you use xml config you can try put <mvc:annotation-driven />, see https://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-config-enable for more details.
You would also need to provide your application context as dispatcher servlet param like in example http://docs.spring.io/spring-flex/docs/1.0.x/reference/html/ch02s02.html:
<servlet>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/web-application-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
Normally this is caused by Spring not correctly finding your controller. If you enable DEBUG logging Spring will log what beans are registered when you start the application.
If it is correctly loading your controller then check that your root path is correct (I think this is your war name by default) which again should be displayed in the logging during startup.
To publish one java-web application in tomcat, I copy the project that named 'demo-mvc' to the tomcat's webapps folder.Then I visit "http://localhost:8080/demo-mvc/xx.jsp" in the chorme browser after starting tomcat but it prompts "The requested resource is not available".I tried editing the server.xml as follows
<Context docBase="D:\apache-tomcat-7.0.57\webapps\demo-mvc" path="/demo-mvc" reloadable="true" source="org.eclipse.jst.jee.server:website"/>
Finally it still does no effect.I am confused about where the problem is.
Try to clean your project from Project clean option and make sure you are mapping all your resources
In your web.xml the code should be like this:
<?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">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
I'm running Spring 4 and am trying to build a VERY basic REST web service as a proof of concept. Here is my 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 id="poc" 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>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>ws</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ws</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/ws-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
Here's the definition of my servlet in my application context:
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<bean name="/rememberName" class="com.corrisoft.air.ws.RememberNameController" init-method="init">
<property name="personService" ref="personService"/>
</bean>
Here are the messages I'm getting from Spring:
INFO: Mapped URL path [/rememberName] onto handler '/rememberName'
....
WARNING: No mapping found for HTTP request with URI [/springpoc/ws/rememberName] in DispatcherServlet with name 'ws'
Where springpoc is the name of the war file I'm deploying under tomcat. RememberNameController directly extends HttpServlet.
Can someone tell me what I'm doing wrong with my mapping?
I think this:
<servlet-mapping>
<servlet-name>ws</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
needs to be this:
<servlet-mapping>
<servlet-name>ws</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
I think the problem is that you are mixing what is intended to be a Spring Controller with plain Servlets.
Spring Controllers are not Servlets, they are regular POJOs that are used by Spring MVC for handling the requests (called by the Dispatcher Servlet)
If you want to see a modern way of creating a REST Service using Spring 4, check out this guide
Spring controllers should not extend HttpServlet, they should be built with Spring annotations. e.g.
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
public class HelloWorldController {
#RequestMapping("/hello")
public String hello() {
return "helloworld";
}
}
See the following tutorial for details http://javahash.com/spring-4-mvc-hello-world-tutorial-full-example/
This issue is solved. Someone created a file called mvc-dispatcher.xml and it had an incorrect configuration. That file is loaded automatically because it's called the same as a servlet.
I want to thank everyone who tried to help me fix this issue. I'm keeping this question here, since it actually explains how to create a REST interface. It works perfectly.
I'm trying to set up a RESTful interface with Spring-MVC. The server starts without issue, but any time I try to call the REST interface I get the message:
41 WARN [springframework.web.servlet.PageNotFound] No mapping found for HTTP request with URI [/myweb/rest/asd/aaa] in DispatcherServlet with name 'mvc-dispatcher'
It seems that the URL I am sending (http://localhost:8080/myweb/rest/asd/qwe for example) is not 'captured' by any controller. What am I doing wrong?
I do not know what else I could try. I'm using Java 1.7.0_15, Tomcat 7.0.34 and Spring 3.1.4.RELEASE
In 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" 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">
<display-name>MyWeb</display-name>
<welcome-file-list>
<welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
<!-- Listener for MVC spring -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<!-- Servlet for MVC spring -->
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<!-- Loading web properties -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>
In my applicationContext.xml:
<context:component-scan base-package="com.myweb.*" />
<context:annotation-config/>
<tx:annotation-driven/>
<mvc:annotation-driven />
And finally, my controller class:
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
public class RestController {
private static Logger LOG = Logger.getLogger(RestController.class);
#RequestMapping("/asd/{test}")
public void test(#PathVariable String test) {
LOG.info("You sent "+test);
}
}
Tried changing the method's #RequestMapping but still didn't work:
#RequestMapping(method=RequestMethod.GET)
public void test() {
LOG.info("You sent something");
}
Your mapping is wrong.
You are requesting /rest/asd/aaa but your mapping is for /asd/{test}.
You need to change #RequestMapping("/asd/{test}") to #RequestMapping("/rest/asd/{test}")
Or add #RequestMapping("/rest") to your controller class
Try annotate your controller class also with #RequestMapping("/rest") to handle all requests with url
/webapp/rest/
You should also configure your context via org.springframework.web.context.ContextLoaderListener listener. I think You are not loading applicationContext.xml
Add this part to your web.xml
<!--spring bootstrap listener -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
I'm trying to improve my spring mvc configuration so as to not require a new config file for every servlet I add, but I'm running into problems. I've tried using this tutorial as a starting point, but I'm running into an issue that I can't figure out.
The problem is that when I do a GET to my servlet, I get back a 404 error. Here's my config and a representative java snippet from a Controller:
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">
<display-name>SightLogix Coordination System</display-name>
<description>SightLogix Coordination System</description>
<servlet>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/application-context.xml
/WEB-INF/application-security.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<url-pattern>/slcs/*</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/application-context.xml
/WEB-INF/application-security.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
application-context.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"
default-init-method="init" default-destroy-method="destroy">
<mvc:annotation-driven />
<context:component-scan base-package="top.level" />
</beans>
application-security.xml:
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="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
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd">
<http>
<intercept-url pattern="/**" access="ROLE_MANAGER" requires-channel="https" />
<http-basic />
</http>
<authentication-manager>
<authentication-provider user-service-ref="myUserDetailsService">
<password-encoder hash="sha"/>
</authentication-provider>
</authentication-manager>
<beans:bean id="myUserDetailsService"
class="path.to.my.UserDetailsServiceImpl">
</beans:bean>
</beans:beans>
Snippet of a Controller class (one of many, but they all look essentially like this):
#Controller
#RequestMapping("/foo.xml")
public class FooController
{
#RequestMapping(method=RequestMethod.GET)
public void handleGET(HttpServletRequest request, HttpServletResponse response) throws IOException
{
...
Can anyone tell me what I'm doing incorrectly?
Thanks!
The only thing out of place here is that you've used the same context config files for both the root webapp context and your servlet context. This is almost guaranteed to be a bad idea, and will result in a lot of weird behaviour. This may well be the cause of your problem.
The ContextLoaderListener is configured with the contextConfigLocation <context-param>, and creates and manages the root WebApplicationContext.
The ServletDispatcherServlet is configured with the contextConfigLocation <init-param>, and creates and manages the servlet WebApplicationContext.
The root WebApplicationContext is the parent of the servlet appcontext, i.e any beans in the root WebApplicationContext are visible to those beans in the servlet WebApplicationContext.
Your first step should be to separate those configurations. With the correct beans in the correct places (e.g. all MVC stuff has to go in the servlet context). Do not share bean definitions between the two, it'll just get confusing and/or broken.
This doesn't answer your question directly, but I've always found it helpful to enable debug logging in Spring when I can't figure out what is going wrong.
Below are two that I commonly use in my logging.properties file:
org.springframework.beans.factory.support.level=FINEST
org.springframework.security.level=FINEST
Do you have more than one GET RequestMapping in your controller by any chance? If there are more than one and there is ambiguity in resolving them to a particular request Spring does not map to any of the ambiguous GET mappings.