使用单页 angular2 重定向的 Spring Boot [英] Spring Boot with redirecting with single page angular2
问题描述
我有一个带有 Spring Boot 的单页 Angular 应用程序.如下所示:
src主要的爪哇控制器家庭控制器客户控制器其他控制器网络应用程序js/angular-files.js索引.html
Spring Boot 正确默认为 webapp 文件夹并提供 index.html 文件.
我想做的是:
对于每个以
/api
开头的本地 REST 请求,不是覆盖并重定向到默认的 webapp/index.html.我计划为 spring 控制器提供任何/api
服务.有没有办法用 API 为所有控制器添加前缀,这样我就不必每次都编写 API?例如
@RequestMapping("/api/home") 可以在代码中写速记@RequestMapping("/home")
或
@RequestMapping("/api/other-controller/:id") 可以写简写@RequestMapping("/other-controller/:id")
我正在寻找每个 API 请求,例如1)
替代方法:尝试添加#ErrorViewResolver:Springboot/Angular2 - 如何处理 HTML5 网址?
对于每个不以/api 开头的本地 REST 请求,覆盖并重定向到默认的 webapp/index.html.我计划为 spring 控制器提供任何/api.
15/05/2017 更新
让我为其他读者重新表述您的问题.(纠正我,如果误解了)
背景
使用 Spring Boot 并从类路径中提供静态资源
要求
所有 404
非 api 请求都应该重定向到 index.html
.
NON API - 表示 URL 不以 /api
开头的请求.
API - 404 应该像往常一样抛出 404
.
示例响应/api/something
- 会抛出 404
/index.html
- 将服务器 index.html/something
- 将重定向到 index.html
我的解决方案
如果给定资源没有可用的处理程序,则让 Spring MVC 抛出异常.
将以下内容添加到 application.properties
spring.mvc.throw-exception-if-no-handler-found=truespring.resources.add-mappings=false
添加一个ControllerAdvice
如下
@ControllerAdvice公共类 RedirectOnResourceNotFoundException {@ExceptionHandler(value = NoHandlerFoundException.class)public Object handleStaticResourceNotFound(final NoHandlerFoundException ex, HttpServletRequest req, RedirectAttributes redirectAttributes) {如果 (req.getRequestURI().startsWith("/api"))返回 this.getApiResourceNotFoundBody(ex, req);别的 {redirectAttributes.addFlashAttribute("errorMessage", "我的自定义错误信息");返回重定向:/index.html";}}私有 ResponseEntitygetApiResourceNotFoundBody(NoHandlerFoundException ex, HttpServletRequest req) {return new ResponseEntity<>("Not Found !!", HttpStatus.NOT_FOUND);}}
您可以根据需要自定义错误消息.
<块引用>有没有办法用 api 为所有控制器添加前缀,这样我就不必每次都编写 api.
为此,您可以创建一个BaseController
并将RequestMapping 路径设置为/api
示例
import org.springframework.web.bind.annotation.RequestMapping;导入 org.springframework.web.bind.annotation.RestController;@RequestMapping("/api")公共抽象类 BaseController {}
并扩展此 BaseController
并确保您不要使用 @RequestMapping
import org.springframework.web.bind.annotation.RequestMapping;导入 org.springframework.web.bind.annotation.RestController;@RestController公共类 FirstTestController 扩展 BaseController {@RequestMapping(path = "/something")公共字符串 sayHello() {return "Hello World !!";}}
上一个答案
您可以创建一个 Filter
,如果请求路径没有以 /api
开头,它会重定向到 /index.html
.
//代码已删除.如果需要,请检查编辑历史记录.
I have a single page Angular app with Spring Boot. It looks like the following:
src
main
java
controller
HomeController
CustomerController
OtherController
webapp
js/angular-files.js
index.html
Spring boot correctly defaults to webapp folder and serves index.html file.
What I am looking to do is:
For every local REST request not starting with
/api
overwrite and redirect to default webapp/index.html. I plan to serve anything/api
to the spring controllers.Is there a way to prefix all controllers with API so that I do not have to write API every time? e.g.
@RequestMapping("/api/home") can write shorthand in code @RequestMapping("/home")
or
@RequestMapping("/api/other-controller/:id") can write shorthand @RequestMapping("/other-controller/:id")
I'm looking for every API request, e.g. 1) http://localhost:8080/api/home keep API with API and resolve to correct controller and return JSON, however if someone enters a URL like http:///localhost/some-url or http:///localhost/some-other/123/url then it will serve the index.html page and keep the URL.
Alternative ways to do it: try adding #ErrorViewResolver: Springboot/Angular2 - How to handle HTML5 urls?
For every local REST request not starting with /api overwrite and redirect to default webapp/index.html. I plan to serve anything /api to the spring controllers.
Update 15/05/2017
Let me re-phrase your query for other readers. (Correct me, if misunderstood)
Background
Using Spring Boot and Serving static resources from classpath
Requirement
All 404
non api requests should be redirected to index.html
.
NON API - means Requests in which URL doesn't start with /api
.
API - 404 should throw 404
as usual.
Sample Response
/api/something
- will throw 404
/index.html
- will server index.html
/something
- will redirect to index.html
My Solution
Let the Spring MVC throw exceptions, if any handler is not available for the given resource.
Add following to application.properties
spring.mvc.throw-exception-if-no-handler-found=true
spring.resources.add-mappings=false
Add a ControllerAdvice
as follows
@ControllerAdvice
public class RedirectOnResourceNotFoundException {
@ExceptionHandler(value = NoHandlerFoundException.class)
public Object handleStaticResourceNotFound(final NoHandlerFoundException ex, HttpServletRequest req, RedirectAttributes redirectAttributes) {
if (req.getRequestURI().startsWith("/api"))
return this.getApiResourceNotFoundBody(ex, req);
else {
redirectAttributes.addFlashAttribute("errorMessage", "My Custom error message");
return "redirect:/index.html";
}
}
private ResponseEntity<String> getApiResourceNotFoundBody(NoHandlerFoundException ex, HttpServletRequest req) {
return new ResponseEntity<>("Not Found !!", HttpStatus.NOT_FOUND);
}
}
You can customize the error message as you like.
Is there a way to prefix all controllers with api so that I do not have to write api every time.
For this, you can create a BaseController
and set the RequestMapping path to /api
Example
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/api")
public abstract class BaseController {}
And extend this BaseController
and make sure you do not annotate child class with @RequestMapping
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class FirstTestController extends BaseController {
@RequestMapping(path = "/something")
public String sayHello() {
return "Hello World !!";
}
}
Previous Answer
You can create a Filter
which redirects to /index.html
if request path doesn't startsWith /api
.
// CODE REMOVED. Check Edit History If you want.
这篇关于使用单页 angular2 重定向的 Spring Boot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!