Halarious - 嵌入式资源字段名称未使用自定义命名策略进行转换
Halarious - Embedded resource field names not being converted with custom naming strategy
我正在使用 HAL 规范框架:
https://github.com/surech/halarious
http://confluence.surech.ch/display/HAL/halarious+Home
http://stateless.co/hal_specification.html
使用自定义命名策略时,嵌入式资源字段名称似乎未正确转换。只有 'non-HAL' 个字段正在转换。
问题:有没有办法确保自定义命名约定应用于嵌入式资源而不仅仅是常规字段?
我在自定义 MessageBodyWriter 中有以下 Gson 配置...
GsonBuilder builder = new GsonBuilder();
builder.setFieldNamingStrategy(new AllLowercaseFieldNamingStrategy());
builder.registerTypeAdapter(HalResource.class, new HalSerializer());
builder.setExclusionStrategies(new HalExclusionStrategy());
Gson gson = builder.create();
try (OutputStreamWriter writer = new OutputStreamWriter(entityStream)) {
writer.write(gson.toJson(resource, HalResource.class));
}
AllLowercaseFieldNamingStrategy
实现只是将字段名称转换为小写,就像这样 @Override public String translateName(Field f) { return f.getName().toLowerCase(); }
例如:
public class MyResource implements HalResource {
private final String myField;
private final MyResource myEmbeddedResource;
...
}
导致字段名称不一致(根据命名策略应全部小写)...
{
"myfield": "...",
"_embedded": {
"myEmbeddedResource": {...}
}
}
我的解决方案是使用 @HalEmbedded
注释并直接分配一个小写名称。
public class MyResource implements HalResource {
private final String myField;
@HalEmbedded(name = "myembeddedresource")
private final MyResource myEmbeddedResource;
...
}
我正在使用 HAL 规范框架:
https://github.com/surech/halarious
http://confluence.surech.ch/display/HAL/halarious+Home
http://stateless.co/hal_specification.html
使用自定义命名策略时,嵌入式资源字段名称似乎未正确转换。只有 'non-HAL' 个字段正在转换。
问题:有没有办法确保自定义命名约定应用于嵌入式资源而不仅仅是常规字段?
我在自定义 MessageBodyWriter 中有以下 Gson 配置...
GsonBuilder builder = new GsonBuilder();
builder.setFieldNamingStrategy(new AllLowercaseFieldNamingStrategy());
builder.registerTypeAdapter(HalResource.class, new HalSerializer());
builder.setExclusionStrategies(new HalExclusionStrategy());
Gson gson = builder.create();
try (OutputStreamWriter writer = new OutputStreamWriter(entityStream)) {
writer.write(gson.toJson(resource, HalResource.class));
}
AllLowercaseFieldNamingStrategy
实现只是将字段名称转换为小写,就像这样 @Override public String translateName(Field f) { return f.getName().toLowerCase(); }
例如:
public class MyResource implements HalResource {
private final String myField;
private final MyResource myEmbeddedResource;
...
}
导致字段名称不一致(根据命名策略应全部小写)...
{
"myfield": "...",
"_embedded": {
"myEmbeddedResource": {...}
}
}
我的解决方案是使用 @HalEmbedded
注释并直接分配一个小写名称。
public class MyResource implements HalResource {
private final String myField;
@HalEmbedded(name = "myembeddedresource")
private final MyResource myEmbeddedResource;
...
}