1

I have a Spring Boot project using Jersey as my REST service and using AngularJS for my front end development. While I run it without using any controller and go to index.html (which is in resource/static/index.html) it works fine. When I add a controller it renders gives the string "index.html" as an output. Spring Boot Configuration:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

 @SpringBootApplication
 @ComponentScan(basePackages = {"com.cst.interfaces","com.cst.configuration","com.cst.application","com.cst.application.implmentation"})
 @EnableAutoConfiguration
 public class ApplicationConfiguration {
    public static void main(String args[]) throws Exception{
        SpringApplication.run(ApplicationConfiguration.class, args);
    }
    public ServletRegistrationBean jerseyServlet(){
        ServletRegistrationBean register = new ServletRegistrationBean(new ServletContainer(),"/*");
        register.addInitParameter(ServletProperties.JAXRS_APPLICATION_CLASS, JerseyInitalize.class.getName());
        return register;
    }
}

JerseyConfiguration:

import org.glassfish.jersey.server.ResourceConfig;
import org.springframework.stereotype.Component;
@Component
public class JerseyInitalize extends ResourceConfig{
    public JerseyInitalize(){
        super();
        this.packages("com.cst.interfaces");
    }
}

Controller Class:

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import org.springframework.web.bind.annotation.RestController;
@RestController
@Path("/home")
public class HomeResource {
    @GET
    @Produces("application/json")
    public String getString(){
        return "index.html";
    }
}
1
  • 1
    The Answer of @kryger is correct but you may also have a look at how to serve static content in a spring boot application. I mentioned this in another answer as well: stackoverflow.com/a/34382229/2576531 Commented Feb 17, 2016 at 21:04

1 Answer 1

3

This is because you annotated your controller with @RestController, which is a shorthand for @Controller with @ResponseBody. The latter annotation instructs the controller to render the output as-is directly into the response.

Use @Controller for controllers that are not RESTful instead.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks kryger, but can i get a sample program, which will be clear to understand.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.