- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 Spring rest 应用程序中,每个 URL 都必须以应用程序 ID (appId) 开头。此 appId 必须在每个休息服务中进行验证。我没有复制代码,而是尝试使用 @Around 建议创建一个 @Aspect。这是在任何休息方法之前正确执行的。
但是,如果应用程序 ID 未知,我既不想创建堆栈跟踪,也不想返回 200(响应 OK)。相反,我确实想返回一个 BAD_REQUEST 响应代码。
如果我在我的建议中抛出异常,我会得到一个堆栈跟踪并且没有 HTTP 响应。另一方面,如果我返回任何其他内容(但不调用 pjp.proceed),我会得到 200 的返回代码。
任何人都可以帮助我将响应代码 400 返回给请求者吗?
到目前为止,在我的代码下方:
@Component
@Aspect
public class RequestMappingInterceptor {
@Autowired
ListOfValuesLookupUtil listOfValuesLookupUtil;
@Around("@annotation(requestMapping)")
public Object around(ProceedingJoinPoint pjp, RequestMapping requestMapping) throws Throwable {
Object[] arguments = pjp.getArgs();
if(arguments.length == 0 || !listOfValuesLookupUtil.isValidApplication(arguments[0].toString())) {
// toto : return bad request here ...
throw new BadRequestException("Application id unknown!");
} else {
return pjp.proceed();
}
}
}
最佳答案
您需要访问 HttpServletResponse
并使用它来发送错误代码。您可以通过 RequestContextHolder
执行此操作
@Around("@annotation(requestMapping)")
public Object around(ProceedingJoinPoint pjp, RequestMapping requestMapping) throws Throwable {
Object[] arguments = pjp.getArgs();
if(arguments.length == 0 || !listOfValuesLookupUtil.isValidApplication(arguments[0].toString())) {
HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse());
response.sendError(HttpStatus.PRECONDITION_FAILED.value(), "Application Id Unknown!");
return null;
} else {
return pjp.proceed();
}
}
关于spring - AOP @Around : return BAD_REQUEST response,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31075594/
我正在尝试购买测试项目 ID“android.test.purchased”,以检查我的账单集成。但是出现如下错误: com.android.vending.api.ApiException: Err
使用 Admin SDK 的 Directory API 更新用户信息时出现错误: 400 BAD_REQUEST { "code" : 400, "errors" : [ { "do
在 Spring rest 应用程序中,每个 URL 都必须以应用程序 ID (appId) 开头。此 appId 必须在每个休息服务中进行验证。我没有复制代码,而是尝试使用 @Around 建议创建
所以给出的例子如下 const translate = require('google-translate-api'); translate('Ik spreek Engels', {to: 'en'
我使用 Google App Engine (Java) 和 Google 帐户进行身份验证。 我创建了一个 servlet (processSignIn.jsp),它确保帐户因除有效 Google
我正在使用 Spring Boot 1.5.15 开发 REST API。我已经实现了一个客户 HandlerMethodArgumentResolver 来映射 HTTP header 。具体来说,
我想在 python 的 wunderlist api 中创建一个任务,这是我的代码: import requests, json access_token = "MY TOKEN" client_i
我一直在关注 super 分类帐网站上的教程:https://hyperledger-fabric.readthedocs.io/en/release/build_network.html#behin
我正在尝试将文档插入 CouchDB。执行此代码时,CouchDB 返回以下错误: STATUS: 400 BODY: {"error":"bad_request","reason":"inva
我尝试创建 first-network手动而不是使用 byfn.sh 脚本,以及当我尝试在 cli 容器中创建 channel 时。 peer channel create -o orderer.ex
您可以 use head to build header-only responses . Rails 指南非常有助于指出您可以发送 :bad_request 或 :created header 。所
我在 Go 中的 AppEngine DevServer 上收到以下错误: API error 1 (datastore_v3: BAD_REQUEST): ApplicationError: 1 a
我是一名优秀的程序员,十分优秀!