Java Spring Rest API-在名为';SpringDispatcher';
我正在尝试执行RESTAPI,它返回json格式的数据,但不映射url 我得到的信息是: PM org.springframework.web.servlet.PageNotFound noHandlerFound ADVERTÊNCIA:在名为“SpringDispatcher”的DispatcherServlet中找不到URI为[/ZombieNetwork/Survives]的HTTP请求的映射 web.xmlJava Spring Rest API-在名为';SpringDispatcher';,java,json,eclipse,rest,spring-mvc,Java,Json,Eclipse,Rest,Spring Mvc,我正在尝试执行RESTAPI,它返回json格式的数据,但不映射url 我得到的信息是: PM org.springframework.web.servlet.PageNotFound noHandlerFound ADVERTÊNCIA:在名为“SpringDispatcher”的DispatcherServlet中找不到URI为[/ZombieNetwork/Survives]的HTTP请求的映射 web.xml <?xml version="1.0" encoding="UTF-8"
<?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>
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.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("zombienetwork.*")
public class ApplicationContextConfig {
// No need ViewSolver
// Other declarations if needed ...
}
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);
}
}
包装控制器;
导入java.util.List;
导入dao.SurvivorDAO;
导入模型。幸存者;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.http.MediaType;
导入org.springframework.web.bind.annotation.PathVariable;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.RequestMethod;
导入org.springframework.web.bind.annotation.RestController;
@RestController
公共类生存控制器{
@自动连线
私人救生索;
@请求映射(“/”)
公共字符串欢迎(){
return“欢迎使用RestTemplate示例。”;
}
@请求映射(value=“/survives”//
method=RequestMethod.GET//
产生={MediaType.APPLICATION\u JSON\u VALUE,MediaType.APPLICATION\u XML\u VALUE})
公共列表{
List List=survivorDAO.getsurvives();
退货清单;
}
@RequestMapping(value=“/survives/{id}”//
method=RequestMethod.GET//
产生={MediaType.APPLICATION\u JSON\u VALUE,MediaType.APPLICATION\u XML\u VALUE})
公共幸存者getEmployee(@PathVariable(“id”)长id){
返回survivorDAO.getSurvivor(id);
}
@请求映射(value=“/survives”//
method=RequestMethod.POST//
产生={MediaType.APPLICATION\u JSON\u VALUE,MediaType.APPLICATION\u XML\u VALUE})
公共幸存者增聘员工(幸存者s){
返回survivorDAO.addSurvivor(s);
}
@RequestMapping(value=“/survives/{id}/{latitude}/{longitude}”//
方法=RequestMethod.PUT//
产生={MediaType.APPLICATION\u JSON\u VALUE,MediaType.APPLICATION\u XML\u VALUE})
公共幸存者更新位置(@PathVariable(“id”)长id、@PathVariable(“纬度”)双纬度、@PathVariable(“经度”)双经度){
更新位置(id、纬度、经度);
返回survivorDAO.getSurvivor(id);
}
@RequestMapping(value=“survivor/reportinclution/{id}”//
方法=RequestMethod.PUT//
产生={MediaType.APPLICATION\u JSON\u VALUE,MediaType.APPLICATION\u XML\u VALUE})
公共无效报告污染(@PathVariable(“id”)长id){
幸存者报告污染(id);
}
@RequestMapping(value=“/employees/{empNo}”//
方法=RequestMethod.DELETE//
产生={MediaType.APPLICATION\u JSON\u VALUE,MediaType.APPLICATION\u XML\u VALUE})
public void deleteEmployee(@PathVariable(“id”)长id){
survivorDAO.deleteSurvivor(id);
}
}
/ZombieNetwork
是您的Web应用程序的上下文吗?如果没有,您应该使用@RequestMapping(“/ZombieNetwork”)
对控制器进行注释。您是否检查了此项?如果您使用Eclipse作为Riverside sugested,这可能会有所帮助。如果您希望拥有yourSite.com/ZombieNetwork/survives,您应该将注释添加到控制器中。对于您当前的配置,您可以使用yourSite.com/survivorsI在我的控制器中添加了注释,但仍然得到相同的错误