我是 Spring 新手,尝试使用 Spring(使用 Maven)开发基本的 Web 服务。我无法弄清楚错误是什么: 这是我拥有的文件:
web.xml(位于 WEB-INF 下)
<!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>
<小时/>
springrest-servlet.xml(位于WEB-INF下)
<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.sample." />
CountryController.java
package com.sample.controller;
import java.util.ArrayList;
import java.util.List;
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.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.sample.model.Country;
@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;
}
}
国家.java
package com.sample.model;
public class Country {
int id;
String name;
public Country(int id, String name) {
super();
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
我在 tomcat 上运行相同的操作,然后,响应显示 No Data Found。
最佳答案
首先你的bas包必须是这样的
<context:component-scan base-package="com.sample" />
而不是
<context:component-scan base-package="com.sample." />
关于java - Spring 休息服务不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36246919/