Got a 404 error on a simple rest service - spring

i'm quite new to web developpement, I worked this summer on a web app on Spring, but the app was already set up, and was just doing some Java and AngularJS. But now i got some problem to start from scratch this type of application. I was trying to just test a simple web service, but can't reach the url I want, i get a 404 error. I'm working on Intellij with Tomcat 8.5.4 :
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>seb</groupId>
<artifactId>webapp</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<version>1.4.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.10.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0.2</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
application-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:beans="http://www.springframework.org/schema/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:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:context="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/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/context">
<jpa:repositories base-package="repository" />
<mvc:annotation-driven/>
<context:component-scan base-package="controller" />
</beans:beans>
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>
Main.java
#SpringBootConfiguration
#EnableJpaRepositories("repository")
public class Main {
public static void main (String[] args) {
SpringApplication.run(Application.class, args);
}
}
StudentController.java
#RestController
public class StudentController {
private final StudentService studentService;
public StudentController(final StudentService studentService) {
this.studentService = studentService;
}
#RequestMapping(value = "/student", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public void getSomeStudent() {
studentService.getSomeStudent();
System.out.println("Hello");
}
}
The url i'm trying to reach is : localhost:8080/webapp/student, but nothing happen, just have a 404 error. I guess i forgot something, but I looked at some tutorials and questions on the web, and can't figure how to fix my project. So if someone got some clues that can help me, that will be very helpful. Thanks by advance.

change #SpringBootConfiguration to #SpringBootApplication it configures following for you
#Configuration tags the class as a source of bean definitions for the application context.
#EnableAutoConfiguration tells Spring Boot to start adding beans based on classpath settings, other beans, and various property
settings. Normally you would add #EnableWebMvc for a Spring MVC app,
but Spring Boot adds it automatically when it sees spring-webmvc on
the classpath. This flags the application as a web application and
activates key behaviors such as setting up
#ComponentScan tells Spring to look for other components, configurations, and services in the the hello package, allowing it to
find the HelloController.
Since you are using spring-boot, add the following dependency,and remove web.xml where spring-boot automatically configures web dispatcher servlet for you
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
or you can still go ahead and configure one if you want.
Based on your initial settings, controller should be located at
http://localhost:8080/student (no 'webapp')

Well I stopped trying to deploy my app on tomcat, i'm just using spring now and i can reach my url. Anyway i will take deeper look at spring docs and tutorials, thanks for the different explications and link.

Use https://start.spring.io/ to create your Spring Boot Application project.
This will generate spring boot maven project for you.
Then add your StudentController there.
and afer that as #jahra specified, you have to trigger URL : http://localhost:8080/student to access your student webservice

Related

404 error springboot restcontroller requestmapping

I have a simple web application with springboot (netbeans 8.2), but resource mapping does not work, I have 2 days with the problem. any ideas?
ApplicationContext://///////////////////////////////////////////////
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/springXML8"/>
WEB.XML ////////////////////////////////////////////////////////////
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>example</display-name>
<absolute-ordering />
<servlet>
<servlet-name>ServletCentral</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ServletCentral</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
ServletConfig:////////////////////////////////////////////////////////////////
<?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">
<!-- paquete a escanear en busca de componentes -->
<mvc:annotation-driven/>
</beans>
Application://////////////////////////////////////////////////////////////
#SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
Controller:///////////////////////////////////////////////////////////////
#RestController
public class controller1 {
#RequestMapping("/")
public String hello(){
return "Lain love this service";
}
}
POM://///////////////////////////////////////////////////////////////////
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>beans</groupId>
<artifactId>beans</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<name>SpringXML8</name>
<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<compilerArguments>
<endorseddirs>${endorsed.dir}</endorseddirs>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<outputDirectory>${endorsed.dir}</outputDirectory>
<silent>true</silent>
<artifactItems>
<artifactItem>
<groupId>javax</groupId>
<artifactId>javaee-endorsed-api</artifactId>
<version>7.0</version>
<type>jar</type>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Ok.
In itself what is your problem? How u are testing? Are you using postman, soap ui?
Based on your mapping your "url-pattern"/* "url-pattern" in web.xml is empty, so you donĀ“t need anymore in your URL Request. But you have little issues. Why do you have two Spring appCtx (ApplicationContext & ServletConfig)??
You do not provide more information. I don't know if you are trying to raise a Tomcat server or simple spring boot server default. You could stay just with ServletConfig, you shouldn't have -Context path="/springXML8"-
You could replace this with a #ResquestMapping global in your RestController. i.e.
#RestController
#RequestMapping("initialLoginOrSpringXML8")
public class PersonalController {
#RequestMapping("/getHello")
public String hello(){
return "Lain love this service";
}
}
Besides I suggest to add some word in your local #RequestMapping by method.
Remember if you use #RequestParams or #PathVariable, you must enter them especially depending on the test client you are using. As well as the type of request Http
#Diego Caballero
There are many options for which you have this error. The most common is a bad mapping of the URL.
The server may be confused by the 2 settings, it may not know which context path to respect. Verify the URL you are using, try different options, combinations.
Change
<url-pattern>/*</url-pattern>
to
<url-pattern>/</url-pattern>
Currently, your mapped DispatcherServlet is marked as handling all requests because of /*. This means that it will also attempt to handle a request dispatched to /WEB-INF/*** It obviously doesn't have a handler for that and will fail.
The pattern / is special in that it is the default fallback if no other pattern matches. If there's a Servlet that maps to the request path, that Servlet will be chosen before your mapped DispatcherServlet. (This maybe is your case).
Try to log everything you can. Use level of info, error and debug, of log4j. Also try to read the server logs (this is located in the folder where you downloaded it). Not only the console of your actual IDE.
Verify if you are deploying correctly the application. Maven cycle, etc. WAR or JAR?
Are you using the correct domain? localhost? or maybe is an ip or a diffente domain setted in the server options deploy
I hope it was a little help. Regards

Unable to access the spring boot rest service from the WL server

I am very new to spring boot stuff. My question is about how to access a spring boot rest service deployed on weblogc server. I have successfully deployed (at least it seems from the WL console) the spring boot war file to the weblogic 12c container. But I am not able to access the service.
I am trying to use the following url to access:
http://host:port/myweb/resource/hello -- I am also not sure whether I should use WL server port on which the war is deployed or 8080 (default spring-boot port)
I have given all the code below. Am i missing anything? I would greatly appreciate any help
POM file:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example.demo</groupId>
<artifactId>SpringBootWL1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>SpringBootWL1</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<start-class>com.example.demo.SpringBootWl1Application</start-class>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Rest Controller:
#RestController
#RequestMapping("/resource")
public class ResourceController {
#GetMapping("/hello")
String home() {
return "Hello World, How are you!";
}
Spring Boot Application
#SpringBootApplication
public class SpringBootWl1Application extends SpringBootServletInitializer implements WebApplicationInitializer {
public static void main(String[] args) {
SpringApplication.run(SpringBootWl1Application.class, args);
}
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(SpringBootWl1Application.class);
}
}
Weblogic.xml
<?xml version="1.0" encoding="UTF-8"?>
<wls:weblogic-web-app
xmlns:wls="http://xmlns.oracle.com/weblogic/weblogic-web-app"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.oracle.com/weblogic/weblogic-web-app
http://xmlns.oracle.com/weblogic/weblogic-web-app/1.4/weblogic-web-app.xsd">
<wls:context-root>/myweb</wls:context-root>
<wls:container-descriptor>
<!-- <wls:prefer-web-inf-classes>true</wls:prefer-web-inf-classes> -->
<wls:prefer-application-packages>
<wls:package-name>org.slf4j.*</wls:package-name>
<wls:package-name>org.springframework.*</wls:package-name>
<wls:package-name>com.fasterxml.jackson.*</wls:package-name>
</wls:prefer-application-packages>
</wls:container-descriptor>
dispatcherServlet-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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
Your configuration seems ok. The default port of Weblogic is 7001, and every web application is deployed using this port.
To verify the deployment you can access to the console from (http://host:7001/console) and go to the Deployments section, there you must find your application deployed and it must be in State OK. Your can check there your web application context there.
One thing I noticed is your file is called: Weblogic.xml, but it must be called weblogic.xml all lower case.
Let me know if it is not working.

I can't make reasteasy work with maven and wildfly

I've been trying hard during an entire week to make it work but there no way I could make wildfly work even with a simple rest hello world app...
Went through all Resteasy doc, wildfly resteasy examples, several tutorial, maven doc linked to wildfly and resteasy and still...
I use JDK 1.8, wildfly 10.x, and resteasy 3.0.19 (which is my wildfly default version).
It seems like wildfly just don't want to see the content of the war. I mean web.xml is ok but my classes are totaly transparent. I get classnotfound on my application class when trying to define resteasy servlet localy and just nothing when using wildfly builtin servlet.
Here are my last pom.xml, web.xml and one and only java class (I tried already with javax.ws.rs.Application class and annotation it's same result. I tried also with custom servlet mapping and all...).
Pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.me.test</groupId>
<artifactId>testrest</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>testrest Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>sun.jdk</groupId>
<artifactId>jconsole</artifactId>
<version>jdk</version>
<scope>system</scope>
<systemPath>C:/Program Files/Java/jdk1.8.0_112/lib/jconsole.jar</systemPath>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jboss.resteasy/resteasy-jaxrs -->
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>3.0.19.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>testrest</finalName>
</build>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>
Web.xml
<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">
<display-name>Archetype Created Web Application</display-name>
<!-- this tells RESTEasy to load resource classes -->
<context-param>
<param-name>resteasy.scan</param-name>
<param-value>true</param-value>
</context-param>
<servlet-mapping>
<servlet-name>javax.ws.rs.core.Application</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
and my only class :
package com.me.services;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
#Path("/hello")
public class HelloService {
#GET
#Produces("text/plain")
public String sayHello() {
return "Hello World";
}
}
And when I try to access my service once deployed I got :
http://localhost:8080/testrest/ : Hello world! (From my index.html file)
http://localhost:8080/testrest/rest : Not Found
localhost:8080/testrest/rest/hello : Not Found
When I check my deployment in wildfly i can see my application but no servlet in undertow...
As explained before I tried many things like when trying to declare my own servlet in web.xml it works but then it can't find the application class in the war though it's actually there... Really seems like Wildfly doesn't see anything appart from my web.xml :/
use http://localhost:8080/testrest/rest/hello/ and also add path to your sayHello method

Launch Spring web application using Spring Boot

I am trying to launch an existing web application using Spring Boot, without changing my existing application at all and keeping the spring-boot specific stuff in a separate module. I have the following maven structure:
parent
web-app
boot-app
I want to launch the web-app from the boot-app.
My boot module
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<dependencies>
<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Web Application -->
<dependency>
<groupId>my.group.id.</groupId>
<artifactId>web-app</artifactId>
<version>1.0-SNAPSHOT</version>
<classifier>classes</classifier>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.2.5.RELEASE</version>
</plugin>
</plugins>
</build>
</project>
Application.java:
#EnableAutoConfiguration
#ImportResource("classpath:WEB-INF/myApp-servlet.xml")
public class Application extends SpringBootServletInitializer{
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
My web module
Structure:
src/main/
java/ --> Rest Endpoints etc.
webapp/ --> web resources, only important files shown
WEB-INF/
myApp-servlet.xml
web.xml
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>web-app</artifactId>
<packaging>war</packaging>
<name>some-name</name>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.5</version>
<configuration>
<attachClasses>true</attachClasses>
</configuration>
</plugin>
</plugins>
</build>
</project>
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 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">
<display-name>My Web App</display-name>
<!-- Servlet Configuration -->
<servlet>
<servlet-name>myApp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>myApp</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
</web-app>
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.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">
<context:component-scan base-package="some.corp.name.space"/>
<mvc:annotation-driven />
</beans>
My Problem
When I run Application.java I get the following error:
Caused by: java.io.FileNotFoundException: class path resource [WEB-INF/myApp-servlet.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:329)
... 24 more
From what I can see, this is because the maven-war-plugin only includes classes in the jar it creates and does not include the webapp directory.
My Question
Is there any way for me to achieve this, with or without the maven-war-plugin?
The most important thing for me is to have the boot-app as a separate module.

No mapping found for HTTP request with URI [/MyTesting/] in DispatcherServlet with name 'mvc-dispatcher'

I am new to spring mvc and Maven. I am trying to get a simple hello world example working. I don't see where I am going wrong it should be straight forward. I am using Maven 3, Eclipse Kepler, Tomcat 6 on a MAC OS.
Can Someone please see the below code and see what is wrong? When I hit localhost:8080/MyTesting I expect to get my welcome file on browser but I get this on the browser:
HTTP Status 404 -
type Status report
message
description The requested resource is not available.
on Eclipse console I get:
WARNING: No mapping found for HTTP request with URI [/MyTesting/] in DispatcherServlet with name 'mvc-dispatcher'
POM.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven 4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>MyTesting</groupId>
<artifactId>MyTesting</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<spring.version>3.1.1.RELEASE</spring.version>
</properties>
<dependencies>
<!-- Spring 3 dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
</project>
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>MyTesting</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<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>/</url-pattern>
</servlet-mapping>
</web-app>
mvc-dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
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-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<context:component-scan base-package="main.com.mytesting" />
<bean class= "org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
Start from this example from Spring developers and tweak it for yourself. It uses the newer, simpler Java Config (annotation-based) configuration.
If you still want to debug your current code, can you push it to github? Your requestMapping in the controller may not be right.
From your post I did not saw any controller configuration, and the error 404 means the required resource is not there.
There are two ways to develop Spring web MVC projects annotation based or programatically. This is a tutorial on the programatic approach, and this is a tutorial on the annotation based approach.
I think you don't create controller for dispatcher map.
I found this tutorial very helpful about create spring mvc.
Thanks Guys,
Its all good now after wasting a few hours of my dev time. I actually had a typo in my folder structures. My folder structure was main.com.mymesting.controllers hence the component scan was failing. I changed it to main.com.mytesting.controllers. It works fine now.
Check your spring configuration file and folder structure "src/main/java"
<context:component-scan base-package="com.wenzinsapp.testapp.controller" /> is resembles as your folder structure
it could be the problem.

Resources