Bonjour,
je suis novice en création d'API et en spring et je galère à comprendre mon erreur.
Je souhaite réaliser une simple API REST avec Spring MVC et j'ai suivi le tutorial http://www.java2blog.com/2015/09/spr...n-example.html
Cependant j'ai un souci lorsque je souhaite accéder à l'URL localhost:8080/mavenWebApp/countries par exemple. J'obtiens une erreur 404 car j'ai un no mapping found for [/mavenWebApp/countries].
Voici mes fichiers :
web.xml
springrest-servlet.xml :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 <!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>
Et mon controller CountryController :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" 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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <mvc:annotation-driven /> <context:component-scan base-package="com.projectmarket.controller" /> </beans>
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 package com.projectmarket.controller; import java.util.ArrayList; import java.util.List; import com.projectmarket.bean.Country; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController public class CountryController { @RequestMapping(value = "/countries", method = RequestMethod.GET, headers = "Accept=application/json") public List<Country> getCountries() { List<Country> listOfCountries = new ArrayList<Country>(); listOfCountries = createCountryList(); return listOfCountries; } @RequestMapping(value = "/country/{id}", method = RequestMethod.GET, headers = "Accept=application/json") public Country getCountryById(@PathVariable int id) { List<Country> listOfCountries = new ArrayList<Country>(); listOfCountries = createCountryList(); for (Country country : listOfCountries) { if (country.getId() == id) return country; } return null; } // Utiliy method to create country list. public List<Country> createCountryList() { Country indiaCountry = new Country(1, "India"); Country chinaCountry = new Country(4, "China"); Country nepalCountry = new Country(3, "Nepal"); Country bhutanCountry = new Country(2, "Bhutan"); List<Country> listOfCountries = new ArrayList<Country>(); listOfCountries.add(indiaCountry); listOfCountries.add(chinaCountry); listOfCountries.add(nepalCountry); listOfCountries.add(bhutanCountry); return listOfCountries; } }
EDIT : j'ai solutionné mon problème. Mes packages étaient situés sous src/main/resources au lieu de src/main/java. Merci l'IDE ...
Partager