簡體   English   中英

無論如何在spring mvc中調用rest api而不擴展.html

[英]Is there anyway to call rest api without extension of .html in spring mvc

我為restful api編寫了代碼。 我必須通過 localhost:8099/demoproject/restcall.html 調用這個 api

每次我必須附加 .html 時,有沒有沒有擴展名的方法可以調用這個方法?

這是我的代碼控制器

@RestController
public class demoAPIController {

    @RequestMapping(value = "/restcall", method = RequestMethod.GET, produces = "application/json")
    public ResponseEntity<String> GetParseResume() {
        return new ResponseEntity("hello", HttpStatus.OK);
    }
}

網絡應用初始化器

public class WebAppInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        WebApplicationContext context = getContext();
        servletContext.addListener(new ContextLoaderListener(context));
        ServletRegistration.Dynamic dispatcher = servletContext.addServlet("DispatcherServlet", new DispatcherServlet(context));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("*.html");
        dispatcher.addMapping("*.pdf");
        dispatcher.addMapping("*.json");
    }

    private AnnotationConfigWebApplicationContext getContext()
    {
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();    
        context.register(WebConfig.class);
        return context;
    }

}

這里 WebConfig.java

    @Configuration
    @EnableWebMvc
    @ComponentScan(basePackages = "com.demo")
    public class WebConfig extends WebMvcConfigurerAdapter {
        @Bean
        public InternalResourceViewResolver getInternalResourceViewResolver() {
            InternalResourceViewResolver viewResolve = new InternalResourceViewResolver();
            viewResolve.setPrefix("/WEB-INF/jsp/");
            viewResolve.setSuffix(".jsp");
            return viewResolve;
        }
    }

改變

WebAppInitializer -> onStartup =>

@Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        WebApplicationContext context = getContext();
        servletContext.addListener(new ContextLoaderListener(context));
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("DispatcherServlet", new DispatcherServlet(context));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/*");
}

網絡配置 =>

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {


  @Override
  public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    configurer.defaultContentType(MediaType.APPLICATION_XML);
  }
}

暫無
暫無

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

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