Я не понимаю, почему я получаю сериализованный JSON для данного класса, как показано ниже.
Это сгенерированный класс из WSDL, поэтому я не могу его изменить:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Lawyer")
public class Lawyer extends Person {
@XmlElementWrapper(required = true)
@XmlElement(name = "lawyerOffice", namespace = "http://xxx/addressbook/external/v01/types")
protected List<LawyerOffice> lawyerOffices;
public List<LawyerOffice> getLawyerOffices() {
if (lawyerOffices == null) {
lawyerOffices = new ArrayList<LawyerOffice>();
}
return lawyerOffices;
}
public void setLawyerOffices(List<LawyerOffice> lawyerOffices) {
this.lawyerOffices = lawyerOffices;
}
}
Когда экземпляр класса сериализуется с помощью fastxml.jackson, я получаю:
{
"ID": "e0d62504-4dfb-4c92-b70b-0d411e8ed102",
"lawyerOffice": [
{
...
}
]
}
Итак, массив называется lawyerOffice. Я ожидаю lawyerOffices.
Это реализация, которую я использую:
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.8.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
Это моя конфигурация ObjectMapper (внедрена в CXF):
@Provider
@Consumes({ MediaType.APPLICATION_JSON, "text/json" })
@Produces({ MediaType.APPLICATION_JSON, "text/json" })
public class JsonProvider extends JacksonJsonProvider {
public static ObjectMapper createMapper() {
ObjectMapper mapper = new ObjectMapper();
AnnotationIntrospector primary = new DPAJaxbAnnotationIntrospector(mapper.getTypeFactory());
AnnotationIntrospector secondary = new JacksonAnnotationIntrospector();
AnnotationIntrospector pair = AnnotationIntrospector.pair(primary, secondary);
mapper.setAnnotationIntrospector(pair);
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
mapper.disable(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED);
mapper.disable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
return mapper;
}
public JsonProvider() {
super();
this.setMapper(createMapper());
}
}
Как я могу получить «правильное» имя списка?
@XmlElement(name = "lawyerOffice"
..., мне кажется, чтоlawyerOffice
"правильное" имя. Тем не менее, учитывая ваш собственный ответ, вам действительно следует включить исходный код конфигурацииObjectMapper
, потому что похоже, что ваша проблема на самом деле вызвана тем, что вы явно используете поддержку аннотаций JAXB, что является довольно важной деталью, которую следует исключить из вашего вопроса. - person Mark Rotteveel   schedule 15.02.2017