Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 找不到Spring MVC资源404_Java_Angularjs_Rest_Spring Mvc - Fatal编程技术网

Java 找不到Spring MVC资源404

Java 找不到Spring MVC资源404,java,angularjs,rest,spring-mvc,Java,Angularjs,Rest,Spring Mvc,当我尝试从AngularJS调用RESTful Web服务时,没有找到404响应。以下是代码示例: WebAppConfiguration.java public class WebAppConfiguration extends WebMvcConfigurerAdapter { @Override public void configureViewResolvers(ViewResolverRegistry registry) { InternalResour

当我尝试从AngularJS调用RESTful Web服务时,没有找到404响应。以下是代码示例:

WebAppConfiguration.java

public class WebAppConfiguration extends WebMvcConfigurerAdapter {

    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
       InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
       viewResolver.setViewClass(JstlView.class);
       viewResolver.setPrefix("/WEB-INF/");
       viewResolver.setSuffix(".html");
       registry.viewResolver(viewResolver);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
       registry.addResourceHandler("/app/**").addResourceLocations("/app/");
    }
}
WebAppInitializer.java

public class WebAppInitializer implements WebApplicationInitializer {
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {  
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();        
        ctx.register(WebBeanConfig.class);
        ctx.setServletContext(servletContext);

        DispatcherServlet dispatcherServlet = new DispatcherServlet(applicationContext);
        ServletRegistration.Dynamic servlet = servletContext.addServlet("mvc-dispatcher", dispatcherServlet);
        servlet.addMapping("/");
        servlet.setAsyncSupported(true);
        servlet.setLoadOnStartup(1);
    }
}
WebBeanConfig.java

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.rvc.releaseSessionDemo")
public class WebBeanConfig {
}
SessionController.java

@RestController
@RequestMapping("/session")
public class SessionController {
   @Autowired
   private ISessionService sessionService;

   @RequestMapping(value = "/releaseSession", method = RequestMethod.POST,  produces = MediaType.APPLICATION_JSON_VALUE)
   public ResponseEntity<Boolean> releaseSession(@RequestBody String sessionId) throws Exception {
       Boolean response = sessionService.releaseSession(sessionId());
       return new ResponseEntity<Boolean>(response, HttpStatus.OK);
   }
}

您应该为
@ResponseBody
使用包装器类

使用类似以下内容:

public class Session {
    private String sessionId;
    //getter setter
}

并将其传递给控制器
@RequestBody会话
。然后您可以通过getter获取会话id。

我想自己回答这个问题

问题在于servlet映射,即“/”,它只在基于XML的配置中起作用。如果我们使用的是基于注释的配置,那么servlet映射应该是初始化器类中的“/*”


servlet.addMapping(“/*”)

404表示您尝试访问的RequestMapping不可用。您可以转到日志并检查那里的请求映射,或者如果日志中没有打印任何内容,您可以尝试以编程方式执行此操作:我签入了日志。资源正在映射到那里。但当我尝试访问资源时,仍然会显示404错误。是否尝试通过broswer发布?检查是否接受内容类型集?@RahulChandwani您是否尝试通过邮递员发送请求,例如标题为“接受应用程序/json”?@nikita_pavlenko是的。仍然出现相同的错误。我尝试添加会话类,但仍然出现相同的错误。问题是我在回答中提到的servlet映射。实际上,您能解释一下xml和应用程序的注释配置之间的区别吗?这对我来说是新的东西。请参考下面的答案了解XML和注释配置之间的区别。谢谢你的链接。但我看不到对servlet映射的任何影响。也许我错过了什么?
public class Session {
    private String sessionId;
    //getter setter
}