带AngularJS的弹簧靴
我有一个SpringBoot项目,使用Jersey作为我的REST服务,使用AngularJS进行前端开发。当我在不使用任何控制器的情况下运行它并转到index.html(位于resource/static/index.html中)时,它工作正常。当我添加一个控制器时,它会将字符串“index.html”作为输出。Spring启动配置:带AngularJS的弹簧靴,angularjs,spring-boot,Angularjs,Spring Boot,我有一个SpringBoot项目,使用Jersey作为我的REST服务,使用AngularJS进行前端开发。当我在不使用任何控制器的情况下运行它并转到index.html(位于resource/static/index.html中)时,它工作正常。当我添加一个控制器时,它会将字符串“index.html”作为输出。Spring启动配置: import org.springframework.boot.SpringApplication; import org.springframework.bo
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;
}
}
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");
}
}
球衣配置:
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;
}
}
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");
}
}
控制器类:
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";
}
}
这是因为您用
@RestController
注释了控制器,这是用@ResponseBody
注释@controller
。后一个注释指示控制器将输出直接呈现到响应中
对非RESTful的控制器使用
@Controller
。这是因为您使用@RestController
注释了控制器,这是针对@Controller
的@ResponseBody
。后一个注释指示控制器将输出直接呈现到响应中
将
@Controller
用于非RESTful的控制器。对@kryger的回答是正确的,但您也可以了解如何在spring引导应用程序中提供静态内容。我在另一个答案中也提到了这一点:@kryger的答案是正确的,但您也可以看看如何在spring引导应用程序中提供静态内容。我在另一个回答中也提到了这一点:谢谢kryger,但是我可以得到一个清晰易懂的示例程序吗。