使用spring mvc實現restful api時遇到了問題。404未找到使用郵遞員的Spring MVC
我用郵遞員來測試一個方法是這樣的:
http://localhost:8080/Test/countryController/getAllCountries
- 測試是項目的名稱
- countryController是@RequestMapping的控制器類的價值
- getAllCountries是get方法
但是,我得到一個錯誤信息:
源服務器沒有找到當前表示目標 資源或者是不願意透露的一個存在。
我搜索了很多帖子,但仍然無法解決它。希望任何人都可以幫助我。
以下是下面的代碼。
彈簧servlet.xml中
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
<annotation-driven />
<resources mapping="/resources/**" location="/resources/" />
<beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<beans:property name="driverClassName" value="com.mysql.jdbc.Driver" />
<beans:property name="url"
value="jdbc:mysql://localhost:3306/country" />
<beans:property name="username" value="root" />
<beans:property name="password" value="12341234" />
</beans:bean>
<!-- Hibernate 4 SessionFactory Bean definition -->
<beans:bean id="hibernate4AnnotatedSessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<beans:property name="dataSource" ref="dataSource" />
<!-- <beans:property name="annotatedClasses"> -->
<!-- <beans:list> -->
<!-- <beans:value>com.system.model.Country</beans:value> -->
<!-- </beans:list> -->
<!-- </beans:property> -->
<beans:property name="packagesToScan">
<beans:list>
<beans:value>com.system.model</beans:value>
</beans:list>
</beans:property>
<beans:property name="hibernateProperties">
<beans:props>
<beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect
</beans:prop>
<beans:prop key="hibernate.show_sql">true</beans:prop>
</beans:props>
</beans:property>
</beans:bean>
<context:component-scan base-package="com.system.*" />
<tx:annotation-driven transaction-manager="transactionManager" />
<beans:bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<beans:property name="sessionFactory"
ref="hibernate4AnnotatedSessionFactory" />
</beans:bean>
的web.xml
<!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>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-servlet.xml</param-value>
</context-param>
<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>/country</url-pattern>
</servlet-mapping>
</web-app>
Country.java
package com.system.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="country")
public class Country {
@Id
@Column(name="id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
int id;
@Column(name="countryName")
String countryName;
@Column(name="population")
int population;
public Country() {
super();
}
public Country(int i, String countryName, int population) {
super();
this.id = i;
this.countryName = countryName;
this.population = population;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCountryName() {
return countryName;
}
public void setCountryName(String countryName) {
this.countryName = countryName;
}
public long getPopulation() {
return population;
}
public void setPopulation(int population) {
this.population = population;
}
}
CountryController.java
@RestController("country")
@RequestMapping(value="/countryController")
public class CountryController {
@Autowired
CountryService countryService;
@RequestMapping(value = "/getAllCountries", method = RequestMethod.GET, headers = "Accept=application/json")
public List<Country> getCountries() {
List<Country> listOfCountries = countryService.getAllCountries();
return listOfCountries;
}
@RequestMapping(value = "/getCountry/{id}", method = RequestMethod.GET, headers = "Accept=application/json")
public Country getCountryById(@PathVariable int id) {
return countryService.getCountry(id);
}
@RequestMapping(value = "/addCountry", method = RequestMethod.POST, headers = "Accept=application/json")
public void addCountry(@RequestBody Country country) {
countryService.addCountry(country);
}
@RequestMapping(value = "/updateCountry", method = RequestMethod.PUT, headers = "Accept=application/json")
public void updateCountry(@RequestBody Country country) {
countryService.updateCountry(country);
}
@RequestMapping(value = "/deleteCountry/{id}", method = RequestMethod.DELETE, headers = "Accept=application/json")
public void deleteCountry(@PathVariable("id") int id) {
countryService.deleteCountry(id);
}
}
感謝。
哪個網址你是哪位? – Jens
在添加更多信息時,如上面的評論,最好編輯您的問題,而不是在評論中回覆,因爲它可以讓人們更容易地看到問題的全部細節。我會建議編輯以包含您要調用的URL,並且還要包含運行代碼的服務器? – DaveyDaveDave