我刚开始尝试使用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;
}
}
Country.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。
答案 0 :(得分:0)
首先,你的bas包必须是这样的东西
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/shop
RewriteCond www.%{HTTP_HOST} ^(?:www\.)?(www\..+)$ [NC]
RewriteRule ^ http://%1/shop%{REQUEST_URI} [NE,L,R]
而不是
<context:component-scan base-package="com.sample" />
答案 1 :(得分:0)
servlet映射也是错误的,而不是<url-pattern>/</url-pattern>
,它应该是指定here的<url-pattern>/*</url-pattern>
。