luongkhanh luongkhanh - 3 months ago 36
Java Question

No mapping found for HTTP request with URI in web.xml

In my project using Spring MVC and JSON, I configure my web.xml file following as:




<!-- Processes application requests -->

My Controller:

public class HomeController {

@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
return "home";
@RequestMapping(value = "/json", method = RequestMethod.GET, produces = "application/json")
public @ResponseBody List<Person> personsReturn(@RequestParam("name") String name, @RequestParam("age") int age) {
Person p1 = new Person("dat1", 11);
Person p2 = new Person("dat2", 22);

Person p3 = new Person("dat3", 33);
Person p4 = new Person("dat4", 44);

List<Person> lists = new ArrayList<Person>();

if (name.equals("dat")) {
} else {
return lists;

My Servlet-Context.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns=""

<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />

<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />

<context:component-scan base-package="com.luongkhanh.restfulspringmvc" />


I can get data from JSON by URL:


Result JSON:


But If I want to get the image by URL at location, it's unsuccessful:


Logger in eclipse alert an exception:

ARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/restfulspringmvc/images/avatar.jpg] in DispatcherServlet with name 'appServlet'

How to configure to get image from URL by Browser? Thank you so much !!


You have configured your DispatcherServlet to use the url-pattern / This overrides the default servlet mapping of the container.

The default servlet mapping is used to load any resources or requests without an explicit mapping in web.xml. Typically static resources are loaded using this. If you override it by specifying it as a pattern for your servlet in web.xml then you have to take care of loading the resources yourself

Spring MVC helps by providing resource handling via You have to specify the mapping which matches your static resources url pattern and the location where your resources reside physically

You did specify

<resources mapping="/resources/**" location="/resources/" />

but your image does not reside in a resources folder under webapp Maven folder. Instead its in images folder. Hence you have to add another resources tag like this

<resources mapping="/images/**" location="/images/" />

NB The mapping attribute and the location attribute does not have to correspond. For example you can have a url like


and your images are in /static-resources/images under webapp the your mapping will be

<resources mapping="/images/**" location="/static-resources/images/" />

You can also have multiple resources specification in the same application context