0
votes

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
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 - Yannic Klem

1 Answers

2
votes

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.