JAX-RS 自定义 JSON 原始资源 return 类型的表示
JAX-RS Custom JSON representation of primitive Resource return type
假设我们有一个具有以下合同的服务:
public interface CategoryService {
public int createNew(String languageCode, String name, String descriptionMarkdown, Integer parentCategoryID, String createdByUserName) throws ServiceException;
};
如何映射 int
return 类型以生成如下所示的 JSON 值?
PS:我知道createNew
方法必须是一个HTTPPOST请求(注释@POST
)。假设注释在那里。我只需要响应表示。
{"id": 1}
不确定这是否是个好主意。如果您创建新的 JAX-RS 资源和指定的实体-class 来响应会更好。
尽管如此,如果您想将编组与模型混合使用,您可以编写自己的 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()
方法。它的结果现在被忽略了(至少在泽西岛)。
5.
不要在 writeTo
方法中关闭 entityStream
。
假设我们有一个具有以下合同的服务:
public interface CategoryService {
public int createNew(String languageCode, String name, String descriptionMarkdown, Integer parentCategoryID, String createdByUserName) throws ServiceException;
};
如何映射 int
return 类型以生成如下所示的 JSON 值?
PS:我知道createNew
方法必须是一个HTTPPOST请求(注释@POST
)。假设注释在那里。我只需要响应表示。
{"id": 1}
不确定这是否是个好主意。如果您创建新的 JAX-RS 资源和指定的实体-class 来响应会更好。
尽管如此,如果您想将编组与模型混合使用,您可以编写自己的 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()
方法。它的结果现在被忽略了(至少在泽西岛)。
5.
不要在 writeTo
方法中关闭 entityStream
。