springboot@RequestMapping注释

springboot@RequestMapping注释,spring,spring-mvc,angular,spring-boot,angular2-template,Spring,Spring Mvc,Angular,Spring Boot,Angular2 Template,我正在开发一个Angular2+Spring引导应用程序,目前我有: 后端:1@RestController,方法用@RequestMapping注释,例如 @RequestMapping("/main") public Band[] main(@RequestParam(value="numberOfBands", required=false, defaultValue="3") int numberOfBands) throws ClientProtocolException, IOExc

我正在开发一个Angular2+Spring引导应用程序,目前我有:
后端:1@RestController,方法用@RequestMapping注释,例如

@RequestMapping("/main")
public Band[] main(@RequestParam(value="numberOfBands", required=false, defaultValue="3") int numberOfBands) throws ClientProtocolException, IOException {          
    return random(numberOfBands);
}  
前端:app.routing.ts,重定向到“/main”
{
路径:“”,
重定向到:“主”,
路径匹配:“已满”
},

初始化时,my main.component.ts向spring发送请求,并获取一个对象数组作为响应;该组件如下所示:

export class MainComponent  {
  bands: Band[];
  errorMessage: string;
  constructor(private bandService: BandService){
    //this.bands = new Array(3);
    bandService.getRandomBands(3)
    .subscribe(
       bands => this.bands = bands,
       error =>  this.errorMessage = <any>error);
  }
}    

之后,转发过程按预期进行。

刷新将始终向服务器发送请求。将所有REST API后端放在特定路径下,例如
/API/…
,以便angular应用程序使用的“假”URL不会与后端的真实URL冲突

然后配置一个过滤器,以便将所有请求转发到那些“伪”角度URL到
/index.html


因此,对
/main
的请求将被您的过滤器拦截,并转发到index.html。角度应用程序将启动。路由器将转到
/main
的路由。该组件将向
/api/main
发送一个AJAX请求。该请求将而不是被过滤器转发到/index.html,因此Spring REST控制器将处理该请求并提供JSON。

是否会将Spring作为视图返回null,从而导致客户端不会初始化?您的意思是,当过滤器转发到index.html时?你能澄清你的问题吗?我试着按照你的答案回答,但当spring boot应用程序将请求重定向到index.html时,该值没有requestMapping。所以我添加了requestMapping方法,它返回void,但是由于返回的视图为null,angular应用程序没有初始化。我说转发到/index.html。如果您需要更多帮助,请编辑您的问题,发布代码,并解释问题所在,以及所有必要的详细信息。
if(requestURI.equals("/main")){
        RequestDispatcher requestDispatcher = httpServletRequest.getRequestDispatcher("/");
        requestDispatcher.forward(req, res);
    }
    chain.doFilter(req, res);