[英]Spring Boot with AngularJS
我有一個 Spring Boot 項目,使用 Jersey 作為我的 REST 服務並使用 AngularJS 進行前端開發。 當我在不使用任何控制器的情況下運行它並轉到 index.html (在resource/static/index.html 中)時,它工作正常。 當我添加一個控制器時,它會呈現字符串“index.html”作為輸出。 春季啟動配置:
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
注釋了您的控制器,這是@Controller
與@ResponseBody
的簡寫。 后一個注釋指示控制器將輸出按原樣直接渲染到響應中。
將@Controller
用於非RESTful 的控制器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.