Vimal Dhaduk Vimal Dhaduk - 1 month ago 5
reST (reStructuredText) Question

Is there anyway to call rest api without extension of .html in sping mvc

I wrote Code for restful api. I have to call this api by,
localhost:8099/demoproject/restcall.html

Every time I have to append .html, Is there any way without extension I can call this method?

Here is a my code
Controller

@RestController
public class demoAPIController {

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


WebAppInitializer

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;
}

}


here 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;
}
}

Answer

change

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("/*");
}

WebConfig =>

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {


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