我想编写一个 clojure lib 并公开生成的类,以便其他 java 项目可以使用它。我阅读并遵循了 gen-class 文档,一切都按我的预期工作,除了带有枚举参数的类注释。
(ns common.exception.unauthorized
(:gen-class :name
^{org.springframework.web.bind.annotation.ResponseStatus
org.springframework.http.HttpStatus/UNAUTHORIZED} ; <- here
com.lalala.common.exception.Unauthorized
:extends java.lang.RuntimeException
:init init
:constructors {[String] [String]}
:main false
:prefix "unauthorized-"))
(defn unauthorized-init [^String message]
[[message] nil])
该异常类生成时没有任何错误,也可以用作Exception
. 但是,此异常旨在与 spring web 一起用作 http 响应。spring框架读取注解,发现是HttpStatus/UNAUTHORIZED
响应401,但是spring框架抛出异常,报错java.lang.EnumConstantNotPresentException: org.springframework.http.HttpStatus
。
我查看了生成的类,它是这样的:
@ResponseStatus(HttpStatus.401)
public class Unauthorized extends RuntimeException {
private static final Var init__var = Var.internPrivate("common.exception.unauthorized", "unauthorized-init");
private static final Var getStackTrace__var = Var.internPrivate("common.exception.unauthorized", "unauthorized-getStackTrace");
// ...... ellipsis
}
如图所示,HttpStatus/UNAUTHORIZED
编译成HttpStatus.401
哪个无效。
我也试过了{:code org.springframework.http.HttpStatus/UNAUTHORIZED}
,{:value org.springframework.http.HttpStatus/UNAUTHORIZED}
可以编译成@ResponseStatus(code/value = HttpStatus.401)
,但是枚举值本身还是无效的形式HttpStatus.401
。
我是否以错误的方式对 gen-class 使用类注释?或者只是 Clojure 编译器有这个错误?
PS 尝试使用 Clojure 1.9、1.10、1.10.1