我正在尝试执行我的rest api,它返回json中的数据,但不是映射url。
我收到的消息是:
PM org.springframework.web.servlet.PageNotFound noHandlerFound
ADVERTÊNCIA:在DispatcherServlet中找不到带有URI [/ ZombieNetwork /幸存者]的HTTP请求的映射,名称为“SpringDispatcher”
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>ZombieNetwork</display-name>
</web-app>
ApplicationContextConfig.java
package Config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("zombienetwork.*")
public class ApplicationContextConfig {
// No need ViewSolver
// Other declarations if needed ...
}
SpringWebAppInitializer.java
package Config;
import javax.servlet.FilterRegistration;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.DispatcherServlet;
public class SpringWebAppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
appContext.register(ApplicationContextConfig.class);
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("SpringDispatcher",
new DispatcherServlet(appContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
// UTF8 Charactor Filter.
FilterRegistration.Dynamic fr = servletContext.addFilter("encodingFilter", CharacterEncodingFilter.class);
fr.setInitParameter("encoding", "UTF-8");
fr.setInitParameter("forceEncoding", "true");
fr.addMappingForUrlPatterns(null, true, "/*");
}
}
WebMvcConfig.java
package Config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
@EnableWebMvc
public class WebMvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
SurvivorController.java
package controller;
import java.util.List;
import dao.SurvivorDAO;
import model.Survivor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
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 SurvivorController {
@Autowired
private SurvivorDAO survivorDAO;
@RequestMapping("/")
public String welcome() {
return "Welcome to RestTemplate Example.";
}
@RequestMapping(value = "/survivors", //
method = RequestMethod.GET, //
produces = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE })
public List<Survivor> getSurvivors() {
List<Survivor> list = survivorDAO.getSurvivors();
return list;
}
@RequestMapping(value = "/survivors/{id}", //
method = RequestMethod.GET, //
produces = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE })
public Survivor getEmployee(@PathVariable("id") long id) {
return survivorDAO.getSurvivor(id);
}
@RequestMapping(value = "/survivors", //
method = RequestMethod.POST, //
produces = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE })
public Survivor addEmployee(Survivor s) {
return survivorDAO.addSurvivor(s);
}
@RequestMapping(value = "/survivors/{id}/{latitude}/{longitude}", //
method = RequestMethod.PUT, //
produces = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE })
public Survivor updateLocation(@PathVariable("id") long id, @PathVariable("latitude")double latitude, @PathVariable("longitude")double longitude) {
survivorDAO.updateLocation(id, latitude, longitude);
return survivorDAO.getSurvivor(id);
}
@RequestMapping(value = "survivor/reportcontamination/{id}", //
method = RequestMethod.PUT, //
produces = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE })
public void reportContamination(@PathVariable("id")long id){
survivorDAO.reportContamination(id);
}
@RequestMapping(value = "/employees/{empNo}", //
method = RequestMethod.DELETE, //
produces = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE })
public void deleteEmployee(@PathVariable("id") long id) {
survivorDAO.deleteSurvivor(id);
}
}