簡體   English   中英

Spring REST始終返回404

[英]Spring REST always return 404

我使用spring mvc創建一個rest Web服務。 但是,我總是得到404。我不知道哪里出了問題。

這是我的初始化器:

public class Initializer extends AbstractAnnotationConfigDispatcherServletInitializer {

@Override
protected Class<?>[] getRootConfigClasses() {
    // TODO Auto-generated method stub
    return new Class<?>[] { RootConfig.class };
}

@Override
protected Class<?>[] getServletConfigClasses() {
    // TODO Auto-generated method stub
    return new Class<?>[] { WebConfig.class };
}

@Override
protected String[] getServletMappings() {
    // TODO Auto-generated method stub
        return new String[] { "/" };
    }

}

這是我的Webconfig:

@Configuration
@EnableWebMvc
@ComponentScan("com.jh.dummy.web")
public class WebConfig extends WebMvcConfigurerAdapter {
    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver viewResovler = new InternalResourceViewResolver();
        viewResovler.setPrefix("/");
        viewResovler.setSuffix(".html");
        viewResovler.setExposeContextBeansAsAttributes(true);
        return viewResovler;
    }

    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

}

這是我的其余服務器文件:

    @RestController
@RequestMapping("/v1/")
public class PositionService {
    @RequestMapping(value = "position", consumes = MediaType.APPLICATION_JSON_VALUE, method = POST)
    public void create(List<Map<String, String>> data) {
    }

    @RequestMapping(value = "position/{id:\\d+}", produces = MediaType.APPLICATION_JSON_VALUE, method = GET)
    public void getById(@PathVariable("id") Long id) {
    }

    @RequestMapping(value = "position/title/{title:.+}/{start:\\d{1,}}", produces = MediaType.APPLICATION_JSON_VALUE, method = GET)
    public void getByTitle(@PathVariable("title") String title,
            @PathVariable("start") int start) {
    }

@RequestMapping(value = "position/title/{title:.+}/address/{address:\\w{1,}}/{start:\\d{1,}}", produces = MediaType.APPLICATION_JSON_VALUE, method = GET)
public void getByTitleAndLocation(@PathVariable("title") String title,
        @PathVariable("address") String address, @PathVariable("start") int start) {

    }

    @RequestMapping(value = "position/address/{address:\\w{1,}}/{start:\\d{1,}}", produces = MediaType.APPLICATION_JSON_VALUE, method = GET)
    public void getByLocation(@PathVariable("address") String address,
            @PathVariable("start") int start) {
    }

    @RequestMapping(value = "position/company/{company:.{1,}}/{start:\\d{1,}}", produces = MediaType.APPLICATION_JSON_VALUE, method = GET)
    public void getByCompany(@PathVariable("company") String company,
                @PathVariable("start") int start) {
        }

    }

這是請求:

在此處輸入圖片說明

當我使用curl測試時,我總是得到404。我找不到哪里出錯了。 任何想法?

我解決了問題。 事實證明,我需要在URL中添加/ dummy /。 因此,該URL將為“ localhost:8080 / dummy / v1 / position / v1”。 但是在其他項目情況下,我不需要將項目名稱添加到虛擬對象中。 我仍然不知道為什么這次需要這樣做。 當我發現后,我會通知你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM