I am new to Spring. I am stuck in one problem although I have looked this in various sites and followed the question that are here with this topic but still I am not able to find a solution.
I'm getting this error
No mapping found for HTTP request with URI [/ReadingsMVC/] in DispatcherServlet with name 'spring'
My web.xml is-
<?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_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Spring3MVC</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
My Spring-Servlet is-
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan
base-package="com.abc.reading.controller"/>
<bean id="viewResolver"
class="/WEB-INF/jsp/.*jsp">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
My Java Rest Controller code is
package com.lucidtechsol.reading.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.abc.reading.exception.InvalidRangeException;
import com.abc.reading.exception.NegativeNumberException;
import com.abc.reading.utilities.RandomNumberGenerator;
@RestController
public class ReadingController {
@RequestMapping("/reading")
public String getReading() throws NegativeNumberException, InvalidRangeException {
return new Integer(new RandomNumberGenerator().generateRandomNumber(100, -50)).toString();
}
}
My TestRandomNumberGenerator is
package com.abc.testcase;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.abc.reading.exception.InvalidRangeException;
import com.abc.reading.exception.NegativeNumberException;
import com.abc.reading.utilities.RandomNumberGenerator;
public class TestRandomNumberGenerator {
int upper;
int lower;
RandomNumberGenerator generator;
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
generator = new RandomNumberGenerator();
}
/**
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception {
generator = null;
}
@Test
public void testPositive() {
upper = 10;
lower = 5;
try {
assertTrue(upper >= generator.generateRandomNumber(lower, upper));
} catch (NegativeNumberException | InvalidRangeException e) {
e.printStackTrace();
}
}
@Test(expected = NegativeNumberException.class)
public void testNegativeNumberException() throws NegativeNumberException,
InvalidRangeException {
upper = 10;
lower = -5;
generator.generateRandomNumber(lower, upper);
}
@Test(expected = InvalidRangeException.class)
public void testInvalidRangeException() throws NegativeNumberException,
InvalidRangeException {
upper = 30;
lower = 10;
generator.generateRandomNumber(lower, upper);
}
}
My exception java file are-
InvalidException
/**
*
*/
package com.abc.reading.exception;
/**
* @author kuppusamys
*
*/
public class InvalidRangeException extends Exception {
/**
*
*/
private static final long serialVersionUID = 4348179544370409821L;
public InvalidRangeException() {
super();
}
public InvalidRangeException(String message) {
super(message);
}
}
NegativeException
/**
*
*/
package com.abv.reading.exception;
/**
* @author kuppusamys
*
*/
public class NegativeNumberException extends Exception {
/**
*
*/
private static final long serialVersionUID = -7903564384519771246L;
public NegativeNumberException() {
super();
}
public NegativeNumberException(String message) {
super(message);
}
}
RandomNumberGenerator is
import java.util.Random;
import com.abc.reading.exception.InvalidRangeException;
import com.abc.reading.exception.NegativeNumberException;
public class RandomNumberGenerator {
public int generateRandomNumber(int upper, int lower) throws NegativeNumberException, InvalidRangeException {
if (lower < 0) {
throw new NegativeNumberException(
"Lower bound of range is negative -->" + lower);
}
if (upper < 0) {
throw new NegativeNumberException(
"Upper bound of range is negative -->" + upper);
}
if (upper < lower) {
throw new InvalidRangeException("Upper bound of range -->" + upper
+ " is smaller than lower bound of range -->" + lower);
}
return lower + new Random().nextInt(upper - lower);
}
}
My .jsp file is- Hello
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>spring mvc</title>
</head>
<body>
${message}
</body>
</html>
I am not understanding what I am doing wrong. Please help me!
if you don't understanding please comment