使用JAX-RS处理JSON响应 [英] Processing JSON response using JAX-RS
问题描述
I have a json payload like this:
{
"account": {
"sample_id": 1424876658095,
"parameters": [
{
"name": "email",
"value": "hello@xyz.com"
},
{
"name": "first_name",
"value": "FIRSTNAME"
},
{
"name": "last_name",
"value": "LASTNAME"
}
]
},
"assests": [ {
"tran_id": "1234567",
}]
}
以上json有效负载是在休息API调用的响应中生成的。
我想在java中处理这个响应,生成如下内容:
The above json payload is getting generated in the response of a rest API call. I would like to process this response in java to generate something like this:
{
"account": {
"sample_id": 1424876658095,
"email_id": "hello@xyz.com",
"first_name": "FIRSTNAME",
"last_name": "LASTNAME",
},
"assets": [ {
"tran_id": "1234567",
}]
}
我正在使用REST API的JAX-RS规范,但我不是能够找到任何库来处理响应。
I am using the JAX-RS specification for the REST API, but I am not able to find any library to process the response.
推荐答案
如果你想利用JAX-R中的Jackson序列化,你需要实现自定义序列化器。
If you want to leverage the Jackson serialization within JAX-Rs, you need to implement as custom serializer.
有两个步骤可以做到:
-
创建自定义序列化程序
以下是基于bean的自定义Jackson序列化程序示例 AccountBean
和 ParameterBean
:
Here is a sample of a custom Jackson serializer for your needs based on beans AccountBean
and ParameterBean
:
public class AccountBeanSerializer extends JsonSerializer<AccountBean> {
@Override
public void serialize(AccountBean accountBean, JsonGenerator jgen,
SerializerProvider provider) throws IOException,
JsonProcessingException {
jgen.writeStartObject();
jgen.writeNumberField("sample_id", accountBean.getSampleId());
List<ParameterBean> parameters = accountBean.getParameters();
for (ParameterBean parameterBean : parameters) {
jgen.writeStringField(parameterBean.getName(),
parameterBean.getValue());
}
jgen.writeEndObject();
}
}
我假设您的回复类是否类似:
I assume the class for your response if something like that:
class ResponseBean
field account = class AccountBean
field sampleId (long)
field parameters = class ParameterBean
(...)
注册自定义序列化程序
然后,您需要在上下文解析程序中提供自定义Jackson配置。为此,您可以创建接口 ContextResolver
的实现,并使用 Provider
进行注释。
You need then to provide a custom Jackson configuration within a context resolver. For this, you can create an implementation of the interface ContextResolver
annotated with Provider
.
@Provider
public class CustomJacksonConfig implements ContextResolver<ObjectMapper> {
private ObjectMapper objectMapper;
public JacksonConfig() {
this.objectMapper = new ObjectMapper();
SimpleModule module = new SimpleModule("MyModule", new Version(1, 0, 0, null));
module.addSerializer(AccountBean.class, new AccountSerializer());
this.objectMapper.registerModule(module);
}
public ObjectMapper getContext(Class<?> objectType) {
return objectMapper;
}
}
以下链接可以帮助您:
- http://www.baeldung.com/jackson-custom-serialization
- https://books.google.fr/books?id=cFBptKRXrk4C&pg=PA81&lpg=PA81&dq=jaxrs+ContextResolver& ;源= BL&安培; OTS = FlOzDeookf&安培; SIG = iY0dM8l5A0svwxB-hsfFO94eIqc&安培; HL = FR&安培; SA = X&安培; EI = B5vwVKSmO8bbat2BguAB&安培; VED = 0CEkQ6AEwBQ#v = onepage&安培; q = JAXRS%20ContextResolver&安培; F =假
- https://github.com/Fa sterXML / jackson-jaxrs-json-provider
- http://www.baeldung.com/jackson-custom-serialization
- https://books.google.fr/books?id=cFBptKRXrk4C&pg=PA81&lpg=PA81&dq=jaxrs+ContextResolver&source=bl&ots=FlOzDeookf&sig=iY0dM8l5A0svwxB-hsfFO94eIqc&hl=fr&sa=X&ei=B5vwVKSmO8bbat2BguAB&ved=0CEkQ6AEwBQ#v=onepage&q=jaxrs%20ContextResolver&f=false
- https://github.com/FasterXML/jackson-jaxrs-json-provider
希望这会有所帮助,
Thierry
Hope this helps, Thierry
这篇关于使用JAX-RS处理JSON响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!