Rest 重新填充控制器测试导致错误404 istead为200。不是打字错误

Rest 重新填充控制器测试导致错误404 istead为200。不是打字错误,rest,spring-mvc,junit,Rest,Spring Mvc,Junit,我正在用java编写一个MVC应用程序。目前,我正在尝试为我的RESTfullController编写一个测试。当我试图让()工作时,问题就出现了。我得到的是404而不是202。为了弄清楚这一点,我花了很多时间,但没能做到。我将在这里发布我的控制器和控制器测试 package com.bestbuy.supportspace.videolibrary.web; import com.bestbuy.supportspace.videolibrary.config.WebAppInitializ

我正在用java编写一个MVC应用程序。目前,我正在尝试为我的RESTfullController编写一个测试。当我试图让()工作时,问题就出现了。我得到的是404而不是202。为了弄清楚这一点,我花了很多时间,但没能做到。我将在这里发布我的控制器和控制器测试

package com.bestbuy.supportspace.videolibrary.web;

import com.bestbuy.supportspace.videolibrary.config.WebAppInitializer;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.web.context.WebApplicationContext;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;


/**
 * User: nikhil.thakur
 * Date: 12/18/13
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {WebAppInitializer.class})
@WebAppConfiguration
public class RestfullControllerTest {

    private MockMvc mockMvc;


    @SuppressWarnings("SpringJavaAutowiringInspection")
    @Autowired
    protected WebApplicationContext wac;

    @Before
    public void setup() {
        this.mockMvc = webAppContextSetup(this.wac).build();
    }

    @Test
    public void testFindAll() throws Exception {
      mockMvc.perform(get("/rest/videos/"))
              .andExpect(status().isOk());
 //             .andExpect(jsonPath("$", hasSize(4)));

    }

 }
我尝试测试的控制器是 包com.bestbuy.supportspace.videolibrary.web

   import com.bestbuy.supportspace.videolibrary.entity.Video;
    import com.bestbuy.supportspace.videolibrary.services.LookupService;
    import com.bestbuy.supportspace.videolibrary.services.VideoService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.util.Assert;
    import org.springframework.web.bind.annotation.*;

    import java.util.ArrayList;
    import java.util.List;

    @Controller
    @RequestMapping("/")
    public class RESTfulController {

        @Autowired
        LookupService lookupService;

        @Autowired
        VideoService videoService;

        @RequestMapping("keywords")
        public
        @ResponseBody
        String findAllKeywords() {
            return lookupService.findAllKeywords().toString();
        }

        @RequestMapping("subjects")
        public
        @ResponseBody
        String findAllSubjects() {
            return lookupService.findAllSubjects().toString();
        }

        @RequestMapping("presenters")
        public
        @ResponseBody
        String findAllPresenters() {
            return lookupService.findAllPresenters().toString();
        }

        @RequestMapping(value = "videos", method = RequestMethod.GET, produces = "application/json")
        public
        @ResponseBody
        String findAllVideos() {
             return videoService.findAll().toString();
        }

        @RequestMapping(value = "videos/findVideosByKeywordsIdIn", method = RequestMethod.GET, produces = "application/json")
        public
        @ResponseBody
        String findVideosByKeywordsIdIn(@RequestParam(value = "keywords") String keywords) {

            return videoService.findVideosByKeywordsIdIn(getListOfIntegers(keywords)).toString();

        }

        @RequestMapping(value = "videos/findByPresentersIdIn", method = RequestMethod.GET, produces = "application/json")
        public
        @ResponseBody
        String findByPresentersIdIn(@RequestParam(value = "presenters") String presenters) {

            return videoService.findByPresentersIdIn(getListOfIntegers(presenters)).toString();

        }


        @RequestMapping(value = "videos/findBySubjectsIdIn", method = RequestMethod.GET, produces = "application/json")
        public
        @ResponseBody
        String findBySubjectsIdIn(@RequestParam(value = "subjects") String subjects) {

            return videoService.findBySubjectsIdIn(getListOfIntegers(subjects)).toString();

        }

        @RequestMapping(value = "videos/findByKeywordsIdInAndPresentersIdInAndSubjectsIdIn", method = RequestMethod.GET, produces = "application/json")
        public
        @ResponseBody
        String findByKeywordsIdInAndPresentersIdInAndSubjectsIdIn(@RequestParam(value = "keywords") String keywords, @RequestParam(value = "presenters") String presenters, @RequestParam(value = "subjects") String subjects) {


            return videoService.findByKeywordsIdInAndPresentersIdInAndSubjectsIdIn(getListOfIntegers(keywords), getListOfIntegers(presenters), getListOfIntegers(subjects)).toString();

        }

        @RequestMapping(value = "videos/findByKeywordsIdInAndPresentersIdIn", method = RequestMethod.GET, produces = "application/json")
        public
        @ResponseBody
        String findByKeywordsIdInAndPresentersIdIn(@RequestParam(value = "keywords") String keywords, @RequestParam(value = "presenters") String presenters) {

            return videoService.findByKeywordsIdInAndPresentersIdIn(getListOfIntegers(keywords),getListOfIntegers(presenters)).toString();

        }

        @RequestMapping(value = "videos/findBySubjectsIdInAndPresentersIdIn", method = RequestMethod.GET, produces = "application/json")
        public
        @ResponseBody
        String findBySubjectsIdInAndPresentersIdIn(@RequestParam(value = "subjects") String subjects, @RequestParam(value = "presenters") String presenters) {

            return videoService.findBySubjectsIdInAndPresentersIdIn(getListOfIntegers(subjects),getListOfIntegers(presenters)).toString();

        }

        @RequestMapping(value = "videos/findBySubjectsIdInAndKeywordsIdIn", method = RequestMethod.GET, produces = "application/json")
        public
        @ResponseBody
        String findBySubjectsIdInAndKeywordsIdIn(@RequestParam(value = "subjects") String subjects, @RequestParam(value = "keywords") String keywords) {

            return videoService.findBySubjectsIdInAndKeywordsIdIn(getListOfIntegers(subjects),getListOfIntegers(keywords)).toString();

        }

        @RequestMapping(value = "video/{videoId}", method = RequestMethod.GET, produces = "application/json")
        public
        @ResponseBody
        String readVideo(@PathVariable Integer videoId, Model model) {
            Assert.notNull(videoId);
            return videoService.findOne(videoId).toString();
        }

        @RequestMapping(value = "video/{videoId}", method = RequestMethod.PUT, consumes = "application/json", produces = "application/json")
        public
        @ResponseBody
        String updateVideo(@RequestBody Video video, @PathVariable Integer videoId, Model model) {
            Assert.notNull(video);
            return videoService.save(video).toString();
        }

        @RequestMapping(value = "video", method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
        public
        @ResponseBody
        String createVideo(@RequestBody Video video, Model model) {
            Assert.notNull(video);
            return videoService.save(video).toString();
        }

        private List<Integer> getListOfIntegers(String keywords) {
            List<Integer> ids = new ArrayList<Integer>();
            for (String id : keywords.split(";") ) {
                new Integer(id);
            }
            return ids;
        }
    }

我很确定在您的例子中,“rest”是应用程序的名称/url部分。这与考试无关。只需使用“/videos”。

我的WebAppInitializer如下所示。因此,我认为只需使用/videos就可以运行它,但事实并非如此。我试图找出这个问题的方法是调试测试方法,看看这个映射的前缀在哪里。我找不到那个too@NikhilThakur我认为servlet映射(“/rest/*”)与此无关,但我不确定。无论如何,我只是注意到实际上没有控制器方法映射到“videos/”,只映射到“videos”,没有尾随斜杠。这没有什么区别。我试着让它与/视频一起工作,但我仍然只能得到一个404@NikhilThakur你能确认你的方法在单元测试中得到映射吗?你检查控制台的输出了吗?
package com.bestbuy.supportspace.videolibrary.config;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.filter.HiddenHttpMethodFilter;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.FilterRegistration;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

/**
 * @author nikhil.thakur
 * @since 12/12/13
 */
public class WebAppInitializer implements WebApplicationInitializer {

    public static final String CONFIG_PACKAGE = "com.bestbuy.supportspace.videolibrary.config";

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.scan(CONFIG_PACKAGE);

        addHttpMethodFilter(servletContext);

        ServletRegistration.Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
        servlet.addMapping("/rest/*");
        servlet.setLoadOnStartup(1);

        servletContext.addListener(new ContextLoaderListener(ctx));
    }

    private void addHttpMethodFilter(ServletContext servletContext) {
        FilterRegistration.Dynamic httpMethodFilter = servletContext.addFilter("HttpMethodFilter", new HiddenHttpMethodFilter());
        httpMethodFilter.addMappingForUrlPatterns(null, false, "/*");
    }
}
mockMvc.perform(get("/rest/videos/"))