1

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;
        }
    }
Uresh K
  • 1,136
  • 11
  • 16
Vimal Dhaduk
  • 994
  • 2
  • 18
  • 43

1 Answers1

1

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);
  }
}
kuhajeyan
  • 10,727
  • 10
  • 46
  • 71
  • If I put simple dispatcher.addMapping("/"); then how it is different than /* – Vimal Dhaduk Oct 15 '16 at 18:01
  • 1
    http://stackoverflow.com/questions/4140448/difference-between-and-in-servlet-mapping-url-pattern – kuhajeyan Oct 15 '16 at 18:05
  • I am not figure out which one is best for which situation. Can you please explain in answer in little bit? – Vimal Dhaduk Oct 15 '16 at 18:08
  • 1
    @VimalDhaduk, in your case / or /* should work. Usually you would have /* unless you do have setup some other listeners. /* would override other servlelts and any request will be passing through your dispatch before it reaches filter. But in your it is fine as it is simple straight forward rest application – kuhajeyan Oct 15 '16 at 18:14