java - JAX-RS 原始资源返回类型的自定义 JSON 表示

标签 java jakarta-ee jax-rs

假设我们有一项具有以下契约(Contract)的服务:

public interface CategoryService {

    public int createNew(String languageCode, String name, String descriptionMarkdown, Integer parentCategoryID, String createdByUserName) throws ServiceException;

};

如何映射 int 返回类型以生成如下所示的 JSON 值?

PS:我知道 createNew 方法必须是 HTTP POST 请求(注释 @POST)。假设有注释。我只需要响应表示。

{"id": 1}

最佳答案

不确定这是否是个好主意。如果您创建新的 JAX-RS 资源并指定用于响应的实体类,那就更好了。
不过,如果您想将编码与模型混合在一起,您可以编写自己的 MessageBodyWriter。例如:

@Produces("application/json")
public class IntWriter implements MessageBodyWriter<Integer> {

    @Override
    public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
        boolean intAsID = false;
        for (Annotation a : annotations) {
            if (a.annotationType().getCanonicalName().equals("com.blabla.IntAsID")) {
                intAsID = true;
                break;
            }
        }
        return intAsID && (type == Integer.class);
    }

    @Override
    public long getSize(Integer integer, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
        return 0;
    }

    @Override
    public void writeTo(Integer integer, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
        JsonGenerator generator = Json.createGenerator(entityStream);
        generator.writeStartObject()
                .write("id", integer)
                .writeEnd()
                .flush();
    }
}

这里有几个要点。
1.

不要忘记在您的配置中注册此编写器。

public class ServerConfig extends Application {

    private static final Set<Class<?>> classes
            = new HashSet<>();

    static {
        //register your resources
        classes.add(Test.class);
        //register message body writer
        classes.add(IntWriter.class);
    }

    @Override
    public Set<Class<?>> getClasses() {
        return classes;
    }

}

2.

如果您不想对每个返回 int 的资源使用此编写器。为您的资源创建一些特殊注释(例如 IntAsID)。

@Retention(RetentionPolicy.RUNTIME)
public @interface IntAsID {}

不要忘记设置正确的保留策略。并在 isWriteable 方法中检查此注释是否存在。就像我在示例中所做的那样。
是的,将此注释添加到您的资源中:

public interface CategoryService {

    @IntAsID
    public int createNew(String languageCode, String name, String descriptionMarkdown, Integer parentCategoryID, String createdByUserName) throws ServiceException;

};

3.

使用@Produces注释。这将帮助您的 JAX-RS 提供程序在资源不应生成 JSON 而应生成其他内容的情况下不检查此编写器。

4.

不关心getSize()方法。现在它的结果被忽略(至少在 Jersey )。

5.

不要在 writeTo 方法中关闭 entityStream

关于java - JAX-RS 原始资源返回类型的自定义 JSON 表示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30699635/

相关文章:

java - 单元测试resteasy时未注入(inject)@Context对象

http - Tomcat 中的 WebDAV 服务器端实现

java - 关闭 Java EE 应用程序时内存泄漏未卸载单例

java - 带有正则表达式的 JAX-RS @Path 注释与预期的 URL 不匹配

java - JVM 一遍又一遍地进行次要垃圾收集,从不抛出 OutOfMemoryError

java - 如何自动调用方法来停止线程?

java - 包含 switch case 语句并插入到数据库的方法的单元测试

java - Android Gradle:如何分别运行集成和单元测试?

java - Wildfly 中的 WAR 扩展策略

java - 处理 Jersey 中不匹配的内容类型