【发布时间】:2017-05-15 21:17:15
【问题描述】:
我正在开发一个 Angular2 + Spring Boot 应用程序,目前我有:
后端: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'{
path: '',
redirectTo: 'main',
pathMatch: 'full'
},
初始化后,我的 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);
}
}
当转到http://localhost:8090/ 时,应用程序会重定向到/main,并且波段会被组件正确传递和解析。但是每当我尝试重新加载页面 http://localhost:8090/main 时,我只会得到纯 JSON,这看起来我的组件没有正确初始化。
我希望 Spring Boot 忽略地址栏中的路径,因此它只与客户端相关。
问题是:这是服务器端的问题,还是我以不适当的方式初始化了我的组件?
更新
我为我的所有@RequestMappings 添加了前缀“/api”并添加了一个过滤器,如下所示('equals' 只是为了测试功能):
if(requestURI.equals("/main")){
RequestDispatcher requestDispatcher = httpServletRequest.getRequestDispatcher("/");
requestDispatcher.forward(req, res);
}
chain.doFilter(req, res);
之后,转发过程按预期进行。
【问题讨论】:
标签: spring spring-mvc angular spring-boot angular2-template