In a personal project i have the following xml structures to make my own java classes:
first:
<response>
<action>getcredits</action>
<data>
<account>
<username>aptalaser</username>
<balance>193</balance>
</account>
</data>
second:
<response>
<action>getMsgInfoByID</action>
<data>
<messageid>c36d7ee5-16f9-4193-9a75-0537e590e9d3</messageid>
<originator>+17036231081 [4]</originator>
<recipient>10958</recipient>
<folder>INBOX</folder>
<senttime>2011.10.17 13:10:26</senttime>
<receivedtime>2011.10.17 13:10:26</receivedtime>
<creationtime>2011.10.17 13:10:26</creationtime>
<callbackid/>
<state>0</state>
<operatornames>
<operatorname>SMPP0</operatorname>
</operatornames>
<routes>
<route>defin_admin</route>
</routes>
<optionalfields>
<optionalfield name="30" value="35333131572D31303133322D303530364E2D333434544600"/>
<optionalfield name="8528" value="017F"/>
</optionalfields>
<messagetype>SMS:TEXT</messagetype>
<messagedata>Test message</messagedata>
</data>
third:
<response>
<action>sendmessage</action>
<data>
<acceptreport>
<statuscode>0</statuscode>
<statusmessage>Message accepted for delivery</statusmessage>
<messageid>0f06bbd9-0894-4fb4-9c4b-68e29363d299</messageid>
<originator>aptalaser</originator>
<recipient>8588430260</recipient>
<messagetype>SMS:TEXT</messagetype>
<messagedata>Bom dia cara.</messagedata>
</acceptreport>
</data>
The structures are divided in two places: a action to indicating the method acessed and the data section: a generic portion with will change the content dinamically in response to access a different method.
Following this idea i decide create a Response class with two field: a String action and a field Data:
The Response class:
/* omite desnecessary imports */
#XmlRootElement(name = "response")
#XmlAccessorType(XmlAccessType.PROPERTY)
public class Response {
private String action;
private Data data;
public String getAction() {
return action;
}
/* omitted set methds */
#XmlElementRef
public Data getData() {
return data;
}
#Override
public String toString() {
String template = "( action: %s, data: %s )";
return String.format(template, this.action, this.data);
}
}
The Data class, the base class for all Data sections
#XmlSeeAlso({ GetInfoMessageData.class, GetAccountData.class, SendMessageData.class })
public class Data {
}
The GetAccountClass to represent account retrieve information
/* omite desnecessary imports */
#XmlRootElement(name = "data")
#XmlAccessorType(XmlAccessType.PROPERTY)
public class GetAccountData extends Data {
private List<Account> account;
public List<Account> getAccount() {
return account;
}
/* omitted set methos */
#Override
public String toString() {
return String.format("Account( %s )", this.account);
}
public static class Account {
private String username;
private Long balance;
public String getUsername() {
return username;
}
public Long getBalance() {
return balance;
}
/* omitted set methods */
#Override
public String toString() {
return String.format("[ usr: %s, credit: %d ]", this.username, this.balance);
}
}
}
The class represents the message data
/* omite desnecessary imports */
#XmlRootElement(name = "data")
#XmlAccessorType(XmlAccessType.PROPERTY)
public class GetInfoMessageData extends Data {
private String messageId;
private String destino;
private String recipiente;
private String folder;
private Date dataCricao;
private Date dataEnvio;
private Date dataRecebimento;
private Integer status;
private String tipoMensagem;
private String mensagem;
private List<Protocolo> protocolos;
private List<Route> rotas;
private List<Field> optionalFields;
private Error error;
#XmlAccessorType(XmlAccessType.PROPERTY)
public static class Protocolo {
private String nomeProtocolo;
#XmlElement(name = "operatorname", defaultValue = "")
public String getNomeProtocolo() {
return nomeProtocolo;
}
/* omitted set methods */
}
#XmlAccessorType(XmlAccessType.PROPERTY)
public static class Error {
private String errorMessage;
#XmlElement(name = "errormessage")
public String getErrorMessage() {
return errorMessage;
}
/* omitted set methods */
}
#XmlAccessorType(XmlAccessType.PROPERTY)
public static class Route {
private String route;
#XmlElement(defaultValue = "")
public String getRoute() {
return route;
}
/* omitted set methods */
}
#XmlAccessorType(XmlAccessType.PROPERTY)
public static class Field {
private String name;
private String value;
#XmlAttribute
public String getName() {
return name;
}
#XmlAttribute
public String getValue() {
return value;
}
/* omitted set methods */
}
#XmlElement(name = "messageid", required = true)
public final String getMessageId() {
return messageId;
}
#XmlElement(name = "originator", required = true)
public final String getDestino() {
return destino;
}
#XmlElement(name = "recipient", defaultValue = "")
public final String getRecipiente() {
return recipiente;
}
#XmlElement(name = "folder", defaultValue = "")
public final String getFolder() {
return folder;
}
#XmlElement(name = "creationtime")
#XmlJavaTypeAdapter(type = Date.class, value = JavaDateAdapter.class)
public final Date getDataCricao() {
return dataCricao;
}
#XmlElement(name = "senttime")
#XmlJavaTypeAdapter(type = Date.class, value = JavaDateAdapter.class)
public final Date getDataEnvio() {
return dataEnvio;
}
#XmlElement(name = "receivedtime")
#XmlJavaTypeAdapter(type = Date.class, value = JavaDateAdapter.class)
public final Date getDataRecebimento() {
return dataRecebimento;
}
#XmlElement(name = "state", required = true)
public final Integer getStatus() {
return status;
}
#XmlElement(name = "messagetype", required = true)
public final String getTipoMensagem() {
return tipoMensagem;
}
#XmlElement(name = "messagedata")
public final String getMensagem() {
return mensagem;
}
#XmlElement(name = "operatornames")
public final List<Protocolo> getProtocolos() {
return protocolos;
}
#XmlElement(name = "routes")
public final List<Route> getRotas() {
return rotas;
}
#XmlElement(name = "optionalfield")
#XmlElementWrapper(name = "optionalfields")
public List<Field> getOptionalFields() {
return optionalFields;
}
#XmlElement(name = "error")
public Error getError() {
return error;
}
/* omitted set methods */
}
The class represent the sendMessage response operation
/* omite desnecessary imports */
#XmlRootElement(name = "data")
#XmlAccessorType(XmlAccessType.PROPERTY)
public class SendMessageData extends Data {
private AcceptReport acceptReport;
#XmlElement(name = "acceptreport")
public AcceptReport getAcceptReport() {
return acceptReport;
}
#SuppressWarnings("unused")
public void setAcceptReport(AcceptReport acceptReport) {
this.acceptReport = acceptReport;
}
#Override
public String toString() {
return String.format("Report( %s )", this.acceptReport);
}
#XmlRootElement(name = "acceptreport")
public static class AcceptReport {
private Integer status;
private String statusMessage;
private String messageId;
private String originator;
private String recipient;
private String messageType;
private String messageData;
#XmlElement(name = "statuscode")
public Integer getStatus() {
return status;
}
#XmlElement(name = "statusmessage")
public String getStatusMessage() {
return statusMessage;
}
#XmlElement(name = "messageid")
public String getMessageId() {
return messageId;
}
#XmlElement(name = "originator")
public String getOriginator() {
return originator;
}
#XmlElement(name = "recipient")
public String getRecipient() {
return recipient;
}
#XmlElement(name = "messagetype")
public String getMessageType() {
return messageType;
}
#XmlElement(name = "messagedata")
public String getMessageData() {
return messageData;
}
/* set methods omited */
#Override
public String toString() {
return String.format("[ stats: %d, msgId: %s, msg: %s ]", this.status, this.messageId, this.messageData);
}
}
}
The xml 'data' section don't have anything to identifing 'what son of Data will be use in the ummarshaling operation?'
My test works fine in the marshalling operation but in the ummarshalling its broken because the jaxb don't identify the son of Data to use.
/* omite desnecessary imports */
public class Teste {
public static void main(String[] args) throws JAXBException {
JAXBContext ctx = JAXBContext.newInstance(
Data.class, GetAccountData.class,
GetInfoMessageData.class, Response.class, SendMessageData.class
);
Marshaller marshaller = ctx.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
Response r = new Response();
r.setAction("getcredits");
GetAccountData data = new GetAccountData();
Account account = new GetAccountData.Account();
account.setUsername("aptalaser");
account.setBalance(12523L);
data.setAccount(Arrays.asList(account));
r.setData(data);
//in there is equal to first xml
marshaller.marshal(r, System.out);
Unmarshaller unmarshaller = ctx.createUnmarshaller();
Response resp = (Response) unmarshaller.unmarshal(new File("./get-credits.xml"));
//but in there the resp retrieved is to the third xml(and the Data field is null)
System.out.println(resp);
}
}
The question is: I need implement my own customized converter or i can make that with simple configurations like annotations?
Thanks for help.
For the purposes of unmarshalling you need to have a unique identifier that can be used to identify the appropriate subclass to instantiate.
#XmlElementRef
Since you are using #XmlElementRef this unique identifier should be the #XmlRootElement of the subclass, but you currently have all subclasses with #XmlRootElement(name="data").
http://blog.bdoughan.com/2010/11/jaxb-and-inheritance-using-substitution.html
xsi:type Attribute
If you want to always leverage the data element then you need something else as the inheritance indicator. If you remove the #XmlElementRef annotation then JAXB will leverage the xsi:type attribute for this purpose.
http://blog.bdoughan.com/2010/11/jaxb-and-inheritance-using-xsitype.html
Element Content
If neither of the above will work for you then you can leverage an XmlAdapter.
http://blog.bdoughan.com/2012/01/jaxb-and-inhertiance-using-xmladapter.html
UPDATE
Thank you #Blaise this help me so much, one more thing: i need process
this model, so i need implement different response class with
appropriate subclass field?
If you know which type of response you are receiving this is a valid approach. Then when you do the unmarshal you can specify the class you are unmarshalling. You will need to do this because the response root element corresponds to all the response classes you will make.
GetAccountDataResponse = unmarshaller.unmarshal(xml, GetAccountDataResponse.class).getValue();
If you don't know what type of response you are receiving and need to decide based on the content of the message then an XmlAdapter approach for handling inheritance will work.
http://blog.bdoughan.com/2012/01/jaxb-and-inhertiance-using-xmladapter.html
Related
I have an OrderInfo class:
#ApiModel(description = "object needed to make an order")
public class OrderInfo implements Serializable {
private static final long serialVersionUID = 1L;
#JsonProperty("purchase")
private PurchaseInfo purchase = null;
#JsonProperty("paymentMode")
private PaymentMode paymentMode = null;
#JsonProperty("serialNumber")
private String serialNumber = null;
public OrderInfo purchase(PurchaseInfo purchase) {
this.purchase = purchase;
return this;
}
#ApiModelProperty(required = true, value = "coming from commercialization")
#NotNull
#Valid
public PurchaseInfo getPurchase() {
return purchase;
}
public void setPurchase(PurchaseInfo purchase) {
this.purchase = purchase;
}
public OrderInfo paymentMode(PaymentMode paymentMode) {
this.paymentMode = paymentMode;
return this;
}
#ApiModelProperty(required = true, value = "")
#NotNull
#Valid
public PaymentMode getPaymentMode() {
return paymentMode;
}
public void setPaymentMode(PaymentMode paymentMode) {
this.paymentMode = paymentMode;
}
public OrderInfo serialNumber(String serialNumber) {
this.serialNumber = serialNumber;
return this;
}
#ApiModelProperty(value = "The serial number of the registered device")
public String getSerialNumber() {
return serialNumber;
}
public void setSerialNumber(String serialNumber) {
this.serialNumber = serialNumber;
}
}
Having a PurchaseInfo child object:
#ApiModel(description = "Info necessary to order a video, an episode or a season")
public class PurchaseInfo implements Serializable {
private static final long serialVersionUID = 1L;
#JsonProperty("id")
private String id = null;
#JsonProperty("pricingId")
private String pricingId = null;
#JsonProperty("price")
private Double price = null;
#JsonProperty("type")
private ArticleType type = null;
public PurchaseInfo id(String id) {
this.id = id;
return this;
}
#ApiModelProperty(required = true, value = "id of the commercialization")
#NotNull
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public PurchaseInfo pricingId(String pricingId) {
this.pricingId = pricingId;
return this;
}
#ApiModelProperty(required = true, value = "additional pricing of the commercialization")
#NotNull
public String getPricingId() {
return pricingId;
}
public void setPricingId(String pricingId) {
this.pricingId = pricingId;
}
public PurchaseInfo price(Double price) {
this.price = price;
return this;
}
#ApiModelProperty(required = true, value = "price of the commercialization")
#NotNull
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public PurchaseInfo type(ArticleType type) {
this.type = type;
return this;
}
#ApiModelProperty(required = true, value = "")
#NotNull
#Valid
public ArticleType getType() {
return type;
}
public void setType(ArticleType type) {
this.type = type;
}
}
And the generated corresponding FieldDescriptor classes:
public class OrderInfoFieldDescriptor {
public static FieldDescriptor[] fdOrderInfo = new FieldDescriptor[] {
fieldWithPath("purchase").description("coming from commercialization").type(PurchaseInfo.class),
fieldWithPath("paymentMode").description("").type(PaymentMode.class),
fieldWithPath("serialNumber").description("The serial number of the registered device").type(java.lang.String.class).optional() };
public static FieldDescriptor[] fdOrderInfoList = new FieldDescriptor[] {
fieldWithPath("[].purchase").description("coming from commercialization").type(PurchaseInfo.class),
fieldWithPath("[].paymentMode").description("").type(PaymentMode.class),
fieldWithPath("[].serialNumber").description("The serial number of the registered device").type(java.lang.String.class).optional() };
}
And:
public class PurchaseInfoFieldDescriptor {
public static FieldDescriptor[] fdPurchaseInfo = new FieldDescriptor[] {
fieldWithPath("id").description("id of the commercialization").type(java.lang.String.class),
fieldWithPath("pricingId").description("additional pricing of the commercialization").type(java.lang.String.class),
fieldWithPath("price").description("price of the commercialization").type(java.lang.Double.class),
fieldWithPath("type").description("").type(ArticleType.class) };
public static FieldDescriptor[] fdPurchaseInfoList = new FieldDescriptor[] {
fieldWithPath("[].id").description("id of the commercialization").type(java.lang.String.class),
fieldWithPath("[].pricingId").description("additional pricing of the commercialization").type(java.lang.String.class),
fieldWithPath("[].price").description("price of the commercialization").type(java.lang.Double.class),
fieldWithPath("[].type").description("").type(ArticleType.class) };
}
When mockMvc.perform is called in order to make a HTTP POST with requestBody and responseBody of type OrderInfo.class:
mockMvc.perform(postRequest)
.andDo(document("orderInfoCreate", preprocessRequest(prettyPrint()), preprocessResponse(prettyPrint()),
pathParameters(parameterWithName("username").description("the name of the user")),
responseFields(OrderInfoFieldDescriptor.fdOrderInfo),
requestFields(OrderInfoFieldDescriptor.fdOrderInfo))).andExpect(status().isCreated())
.andExpect(content().json(orderInfoAsJson));
The validation works for paymentMode and serialNumber but fails for purchase with the following exception:
org.springframework.restdocs.snippet.SnippetException: The following parts of the payload were not documented:
{
"purchase" : {
"id" : "purchaseId",
"pricingId" : "pricingId",
"price" : 12.0,
"type" : "EPISODE"
},
"serialNumber" : "serialNumber"
}
Even though the request body and the response body look fine:
MockHttpServletRequest:
HTTP Method = POST
Request URI = /api/users/myUserName/orders
Parameters = {}
Headers = [Content-Type:"application/json;charset=UTF-8", Host:"host:posrt", Accept:"application/json;charset=UTF-8", Cookie:"identity=cookieForTest"]
Body = {"purchase":{"id":"purchaseId","pricingId":"pricingId","price":12.0,"type":"EPISODE"},"paymentMode":"POSTPAID","serialNumber":"serialNumber"}
Session Attrs = {}
And:
MockHttpServletResponse:
Status = 201
Error message = null
Headers = [Content-Type:"application/json;charset=UTF-8"]
Content type = application/json;charset=UTF-8
Body = {"purchase":{"id":"purchaseId","pricingId":"pricingId","price":12.0,"type":"EPISODE"},"paymentMode":"POSTPAID","serialNumber":"serialNumber"}
Forwarded URL = null
Redirected URL = null
Cookies = []
The issue appeared after migrating from Spring Boot 1x to 2x.
Do you have any idea what the issue could be?
Thanks :)
It looks like you are relying on purchase to document both purchase and everything it contains, i.e. purchase.id, purchase.pricingId, purchase.price, and purchase.type. This worked in REST Docs up to and including 1.1 but it created a problem where people would accidentally miss documenting a nested field as they had documented its parent. This was discussed in this question and also in this Spring REST Docs issue. The issue introduced a change in REST Docs 1.2 where documenting a field no longer documents all of its descendants by default. You are affected by this change as upgrading to Spring Boot 2 means that you have also upgraded to REST Docs 2.
If you want to keep the existing behaviour and just use purchase to document both it and all of its descendants, you should replace fieldWithPath with subsectionWithPath. If you want to document everything beneath purchase, you should add extra descriptors for purchase.id, purchase.pricingId, purchase.price, and purchase.type.
I'm receiving, for instance, this JSON from an external vendor (where payload can be variable):
{
"payload": {
"enrolledAt": "2018-11-05T00:00:00-05:00",
"userId": "99c7ff5c-2c4e-423f-abeb-2e5f3709a42a"
},
"requestId": "80517bb8-2a95-4f15-9a73-fcf3752a1147",
"eventType": "event.success",
"createdAt": "2018-11-05T16:55:13.762-05:00"
}
I'm trying to model these using this class:
public final class Notification<T extends AbstractModel> {
#JsonProperty("requestId")
private String requestId;
#JsonProperty("eventType")
private String eventType;
#JsonProperty("createdAt")
private ZonedDateTime createdAt;
private T payload;
#JsonCreator
public Notification(#JsonProperty("payload") T payload) {
requestId = UUID.randomUUID().toString();
eventType = payload.getType();
createdAt = ZonedDateTime.now();
this.payload = payload;
}
// getters
}
...and then having these possible (generic) types:
public abstract class AbstractModel {
private String userId;
private Type type;
#JsonCreator
AbstractModel(#JsonProperty("companyUserId") String userId, #JsonProperty("type") Type type) {
this.userId = userId;
this.type = type;
}
// getters
public enum Type {
CANCEL("event.cancel"),
SUCCESS("event.success");
private final String value;
Type(String value) {
this.value = value;
}
public String getValue() { return value; }
}
}
public final class Success extends AbstractModel {
private ZonedDateTime enrolledAt;
#JsonCreator
public Success(String userId, #JsonProperty("enrolledAt") ZonedDateTime enrolledAt) {
super(userId, Type.SUCCESS);
this.enrolledAt = enrolledAt;
}
// getters
}
public final class Cancel extends AbstractModel {
private ZonedDateTime cancelledAt;
private String reason;
#JsonCreator
public Cancel(String userId, #JsonProperty("cancelledAt") ZonedDateTime cancelledAt,
#JsonProperty("reason") String reason) {
super(userId, Type.CANCEL);
this.cancelledAt = cancelledAt;
this.reason = reason;
}
// getters
}
The application is based on Spring Boot, so I'm deserializing the JSON like:
#Component
public final class NotificationMapper {
private ObjectMapper mapper;
public NotificationMapper(final ObjectMapper mapper) {
this.mapper = mapper;
}
public Optional<Notification<? extends AbstractModel>> deserializeFrom(final String thiz) {
try {
return Optional.of(mapper.readValue(thiz, new NotificationTypeReference()));
} catch (final Exception e) { /* log errors here */ }
return Optional.empty();
}
private static final class NotificationTypeReference extends TypeReference<Notification<? extends AbstractModel>> { }
}
...but eventually since I'm posting this right here, Jackson doesn't like any of that so far. I've tried several things like: JsonTypeInfo and JsonSubTypes, but I can't change the JSON input.
Anyone? Any clue(s)?
We ended up adding another key/value pair in the JSON – the contract was indeed modified a little bit to accommodate that "private" key/value pair.
In any case, if somebody runs into the same issue, this is the solution for the approach:
...
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
#JsonIgnoreProperties("_type")
#JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "_type")
#JsonSubTypes({
#JsonSubTypes.Type(value = Cancel.class, name = "_type"),
#JsonSubTypes.Type(value = Fail.class, name = "_type"),
#JsonSubTypes.Type(value = Success.class, name = "_type"),
})
public abstract class AbstractModel {
private String userId;
private Type type;
AbstractModel() { }
AbstractModel(final String userId, final Type type) {
this.userId = userId;
this.type = type;
}
// getters, toString, etc.
public enum Type {
CANCEL("event.cancelled"),
FAIL("event.failed"),
SUCCESS("event.success");
private final String value;
Type(String value) {
this.value = value;
}
public String getValue() { return value; }
}
}
I am a novice in SAXParser. I don't know is it possible to parse complex object with SAXParser. I have a class which contain Item list. And my response xml is like that :
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><GetPaymentServiceInfoResponse xmlns="http://quetzalcoatlus/EpulPaymentService"><GetPaymentServiceInfoResult><ResultCodes>OK</ResultCodes><Description>User is temporary blocked</Description><Items><Item><Amount>122</Amount></Item><Item><Amount>23232</Amount></Item></Items></GetPaymentServiceInfoResult></GetPaymentServiceInfoResponse></s:Body></s:Envelope>
And my POJO class is like following:
#XmlRootElement(name = "PGResponse")
public class CheckAbonSuccessResponseModel {
private String message;
private String message_code;
private BigDecimal amount;
private String invoiceCode;
private String operationCode;
private List<Item> items;
#XmlElement(name = "message")
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
#XmlElement(name = "message_code")
public String getMessage_code() {
return message_code;
}
public void setMessage_code(String message_code) {
this.message_code = message_code;
}
#XmlElement(name = "amount")
public BigDecimal getAmount() {
return amount;
}
public void setAmount(BigDecimal amount) {
this.amount = amount;
}
#XmlElement(name = "invoice_code")
public String getInvoiceCode() {
return invoiceCode;
}
public void setInvoiceCode(String invoiceCode) {
this.invoiceCode = invoiceCode;
}
#XmlElement(name = "operation_code")
public String getOperationCode() {
return operationCode;
}
public void setOperationCode(String operationCode) {
this.operationCode = operationCode;
}
#XmlElement(name = "items")
public List<Item> getItems() {
return items;
}
public void setItems(List<Item> items) {
this.items = items;
}
#XmlRootElement(name = "item")
public static class Item {
private String label;
private String value;
#XmlElement(name = "label")
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
#XmlElement(name = "value")
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
}
How I can parse my xml string to CheckAbonSuccessResponseModel. Is it possible or not? I was trying but it shows just amount inside Result element.I need just know how I must write DefaultHandler class.
Thanks in advance.
I am trying to support the hypermedia constraint using link elements as per http://martinfowler.com/articles/richardsonMaturityModel.html#level3 in my response XML. I have a BankAccount object and I can return one link element on its own but I am having trouble when trying to return a list of link elements (even one on its own in a list is an issue). I would prefer not to have to encapsulate the link elements in a links parent element; I would prefer to list off the link elements.
My BankAccount class is as follows:
#XmlRootElement(name = "bankaccount")
#XmlType(propOrder={"branchCode","accountNo","custName", "custAddress", "custType", "custRating", "balance", "link"})
public class BankAccount {
private String branchCode, accountNo, custName, custAddress, custType, custRating;
private double balance;
// private Link link = new Link(); // works
private List<Link> links;// = new ArrayList<>();
public BankAccount(){
}
public BankAccount(String branchCode, String accountNo, String custName, String custAddress, String custType, String custRating, double balance) {
this.branchCode = branchCode;
this.accountNo = accountNo;
this.custName = custName;
this.custAddress = custAddress;
this.custType = custType;
this.custRating = custRating;
this.balance = balance;
}
#XmlElement
public void setBranchCode(String branchCode) {
this.branchCode = branchCode;
}
public String getBranchCode() {
return branchCode;
}
// other setters and getters...
// the link element on its own that works...
// #XmlElement(name = "link")
// public void setLink(Link aLink){
// this.link = aLink;
// }
// public Link getLink(){
// return link;
// }
#XmlElement(name = "link")
public List<Link> getLinks() {
return links;
}
public void setLinks(List<Link> links) {
this.links = links;
}
}
The Link class:
#XmlAccessorType(XmlAccessType.FIELD)
public class Link {
#XmlAttribute(name = "rel")
private String rel;
#XmlAttribute(name = "href")
private String href;
public String getRel() {
return rel;
}
public void setRel(String aRel){
this.rel = aRel;
}
public String getHref() {
return href;
}
public void setHref(String href){
this.href = href;
}
}
Lastly, my RESTful WS code:
// hypermedia constraint...
bankAccount.setLinks(new ArrayList<Link>());
Link linkSelf = new Link();
linkSelf.setRel("self");
linkSelf.setHref("/"+bankNSC+"/"+bankAccountNumber);
bankAccount.getLinks().add(linkSelf);
return Response.status(Response.Status.OK).entity(bankAccount).build();
I am getting a status 500 error :
The server encountered an internal error that prevented it from fulfilling this request
Any help very much appreciated...
Thanks,
Sean.
In the propOrder attribute in #XmlType you have link but the property is called links.
An easy way to find this type of error is to try your model with JAXB directly with a Java SE example outside of JAX-RS.
Try using annotation #XmlAccessorType(XmlAccessType.PROPERTY) for the BankAccount class, or try
...
#XmlAccessorType(XmlAccessType.FIELD)
public class BankAccount {
...
#XmlElement(name = "link")
private List<Link> links;
....
}
How to serialize this class using Jackson
package com.progressivebeef.service.response;
#XmlRootElement(name = "response")
#XmlSeeAlso({ User.class, Profile.class,MenuItem.class,Feedlot.class,Document.class,FeedlotDocument.class })
public final class PBResponse {
private Integer status = FAILURE;
private String code;
private String message;
private Integer totalRecords;
private List<Model> list = new ArrayList<Model>();
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
#XmlElementWrapper(name = "PBBeans")
#XmlAnyElement
public List<Model> getList() {
return list;
}
public void setList(List<Model> list) {
this.list = list;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Integer getTotalRecords() {
return totalRecords;
}
public void setTotalRecords(Integer totalRecords) {
this.totalRecords = totalRecords;
}
/**
* #author tiqbal
* Resets the response.
*/
public void reset(){
this.status = FAILURE;
this.list = new ArrayList<Model>();
this.code = null;
this.message = null;
this.totalRecords = null;
}
}
Jackson is not picking up #XmlElementWrapper #XmlSeeAlso annotations, also Jackson is not mapping #XmlRootElement annotation. I am using Jackson 1.9.0. Jackson is putting elements in the list but not mapping root element of POJO classes.
Here is sample method.
package com.progressivebeef.service.impl;
#Service("/ActivityServiceImpl/")
#Path("/activityservice/")
public class ActivityServiceImpl implements ActivityService {
#POST
#Produces(MediaType.APPLICATION_JSON)
#Override
public Response inputJson(User user ) {
System.out.println("user ");
user.setUser_name("check user name");
Profile profile = new Profile();
profile.setFirst_name("abc");
profile.setLast_name("khan");
user.setProfile( profile );
PBResponse response = new PBResponse();
response.getList().add(user);
return Response.ok(response).build();
}
}
The response it generating is '{"response":{"status":0,"PBBeans":[{"user_name":"check user name","password":"click123","user_role_key":2,"profile":{"first_name":"abc","last_name":"khan","tableName":"pb_profile","pk":"profile_id"},"tableName":"pb_user","pk":"user_id"}]}}'
not picking up the bean's root name inside PBBeans tag.
Hope this helps. Basically, you need to set the WRAP_ROOT_VALUE to true in your mapper.