gpt4 book ai didi

java - 如何使用自定义参数注释返回自定义 spring 错误响应?

转载 作者:行者123 更新时间:2023-12-01 16:50:45 24 4
gpt4 key购买 nike

我是 Spring 新手,所以请原谅我的无知。当给定的方法参数(“必需”)为空时,我试图“返回”自定义响应。来自 spring 的当前响应是:

{
"timestamp": 1477060294961,
"status": 400,
"error": "Bad Request",
"exception": "org.springframework.web.bind.MissingServletRequestParameterException",
"message": "Required String parameter 'bookname' is not present",
"path": "/getbook"
}

我试图达到它“返回”的地步:

{
"status": 400,
"error": {
// custom error body
}
}

我认为一个很好的方法是使用自定义的“参数注释”。这也将使代码更具可读性并存储有关此端点参数的有用信息。

我按照给出的例子 here ,但我不确定在哪里或如何返回自定义响应?

到目前为止我有注释:

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Component
public @interface customParameter {

String value() default "";
boolean required() default false;
String defaultValue() default ValueConstants.DEFAULT_NONE;
String customInfo() default "blar blar";
}

“端点”:

  @RequestMapping(value = "/getbook", method = {RequestMethod.POST})
public ResponseEntity<BookResponse> getBookInfo(
@customParameter(value = "bookname", required = true, customInfo = "extremely valuable book")
final String bookname
) {
return new bookRequest(bookname).getResponse;
}

并有一个自定义解析器:

public class CustomAnnotationResolver implements HandlerMethodArgumentResolver {


@Override
public boolean supportsParameter(MethodParameter parameter) {
return parameter.getParameterAnnotation(customParameter.class) != null;
}


@Override
public Object resolveArgument(MethodParameter methodparameter, ModelAndViewContainer mavContainer,
NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {

CustomParameter customParameter = methodparameter.getParameterAnnotation(CustomParameter.class);

String parameter = webRequest.getParameter(CustomParameter.value());

// todo: do Validation here
if (customParameter == null) {
if (Parameter.required()) {
String customInfo = customParameter.customInfo();
String body getBody(customInfo);
new ResponseEntity(body, 400); // so the problem is here!! how do I return this response??
}
}

return webRequest.getParameter(customParameter.value());

}
}

我还使用 webConfig“注册”了该解析器:

@EnableWebMvc
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
argumentResolvers.add(new CustomAnnotationResolver());
}
}

任何有关此实现的帮助或任何其他有关如何执行此操作的建议都将非常棒。谢谢大家:)

最佳答案

谢谢@growlingchaos,太棒了,这就是解决方案。

@ControllerAdvice
@RestController
public class customExceptionAdvice {

@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ResponseEntity handleConflict(BadRequestException e, HttpServletResponse response)
throws IOException {

return new ResponseEntity(e.getErrorBody(), HttpStatus.BAD_REQUEST);
}

关于java - 如何使用自定义参数注释返回自定义 spring 错误响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40652725/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com