假设我们有一项具有以下契约(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/