CDATA element in WSDL client - java

I'm doing a WSDL client and want to know how I can set an XML element to be CDATA.
I'm using the wsimport to generate the source code, and the CDATA element is part of the request XML.
This is the XML class of the request:
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "", propOrder = { "dataRequest" })
#XmlRootElement(name = "ProcessTransaction")
public class ProcessTransaction {
protected String dataRequest;
public String getDataRequest() {
return dataRequest;
}
public void setDataRequest(String value) {
this.dataRequest = value;
}
}
I've already tried the #XmlAdapter, but it changes nothing on the output...
import javax.xml.bind.annotation.adapters.XmlAdapter;
public class AdaptorCDATA extends XmlAdapter<String, String> {
#Override
public String marshal(String arg0) throws Exception {
return "<![CDATA[" + arg0 + "]]>";
}
#Override
public String unmarshal(String arg0) throws Exception {
return arg0;
}
}
In the XML class:
#XmlJavaTypeAdapter(value=AdaptorCDATA.class)
protected String dataRequest;
I tried to debug, but it never steps on the AdaptorCDATA function.
The wsimport version is 2.2.9 and the jaxb-api version is 2.1.

So, as #user1516873 suggested, i moved the code to cxf, and with this, is working well. Now i'm using the "wsdl2java" to generate the code, and the jars from cxf on my project.
What is different in the code:
CdataInterceptor
import javax.xml.stream.XMLStreamWriter;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
public class CdataInterceptor extends AbstractPhaseInterceptor<Message> {
public CdataInterceptor() {
super(Phase.MARSHAL);
}
public void handleMessage(Message message) {
message.put("disable.outputstream.optimization", Boolean.TRUE);
XMLStreamWriter writer = (XMLStreamWriter) message.getContent(XMLStreamWriter.class);
if (writer != null && !(writer instanceof CDataContentWriter)) {
message.setContent(XMLStreamWriter.class, new CDataContentWriter(writer));
}
}
public void handleFault(Message messageParam) {
System.out.println(messageParam);
}
}
CDataContentWriter
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import org.apache.cxf.staxutils.DelegatingXMLStreamWriter;
public class CDataContentWriter extends DelegatingXMLStreamWriter {
public CDataContentWriter(XMLStreamWriter writer) {
super(writer);
}
public void writeCharacters(String text) throws XMLStreamException {
boolean useCData = text.contains("RequestGeneric");
if (useCData) {
super.writeCData(text);
} else {
super.writeCharacters(text);
}
}
// optional
public void writeStartElement(String prefix, String local, String uri) throws XMLStreamException {
super.writeStartElement(prefix, local, uri);
}
}
Using the Writer and the Interceptor:
MyService wcf = new MyService(url, qName);
IMyService a = wcf.getBasicHttpBinding();
Client cxfClient = ClientProxy.getClient(a);
CdataInterceptor myInterceptor = new CdataInterceptor();
cxfClient.getInInterceptors().add(myInterceptor);
cxfClient.getOutInterceptors().add(myInterceptor);
And it works perfect!

Related

Retrofit Get Data Object

I have a data like this, and i want get report and criteria data.
{
"response_code": 200,
"message": "Your report data has been loaded.",
"data": {
"report": [
{
"id_report": 1,
"report_name": "report name A"
},
{
"id_report": 2,
"report_name": "report name B"
}
],
"criteria": [
{
"id_criteria": 1,
"criteria_name": "criteria name A"
},
{
"id_criteria": 2,
"criteria_name": "criteria name B"
}
]
}
}
And i get data in java using retrofit. And this is my java class.
GetReport.java
#SerializedName("response_code")
private int response_code;
#SerializedName("status")
private boolean status;
#SerializedName("message")
private String message;
#SerializedName("data")
Call<Data> listData;
Data.java
#SerializedName("report")
private List<Report> reportList;
#SerializedName("criteria")
private List<Criteria> criteriaList;
And this how i call the data.
public void populateData() {
Call<GetReport> getReportCall = apiInterface.getReportCall();
getReportCall.enqueue(new Callback<GetReport>() {
#Override
public void onResponse(Call<GetReport> call, Response<GetReport> response) {
response.body().getListData().enqueue(new Callback<Data>() {
#Override
public void onResponse(Call<Data> call, Response<Data> response) {
List<Report> reportList = response.body().getReportList();
Log.d("TAGGGGGGGGGG", String.valueOf(reportList.size()));
}
#Override
public void onFailure(Call<Data> call, Throwable t) {
t.printStackTrace();
}
});
}
#Override
public void onFailure(Call<GetReport> call, Throwable t) {
t.printStackTrace();
}
});
}
When I run the program, my activity closes immediately. When I look at logcat, there is too much running log data so I can't see where the error is.
I have managed to attempt and solve your problem with the following code. I copied and pasted the JSON you provided above at JSONbin.io so that I can be able to call it using an API call. I did not modify the structure of the JSON at all.
App build.gradle
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
GetReport.java
package com.example.retrofitapp;
import com.google.gson.annotations.SerializedName;
public class GetReport {
#SerializedName("response_code")
int response_code;
#SerializedName("message")
String message;
#SerializedName("data")
Data data;
public int getResponse_code() {
return response_code;
}
public void setResponse_code(int response_code) {
this.response_code = response_code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Data getData() {
return data;
}
public void setData(Data data) {
this.data = data;
}}
Data.java
package com.example.retrofitapp;
import com.google.gson.annotations.SerializedName;
import java.util.List;
public class Data {
#SerializedName("report")
List<Report> reportList;
#SerializedName("criteria")
List<Criteria> criteriaList;
public List<Report> getReportList() {
return reportList;
}
public void setReportList(List<Report> reportList) {
this.reportList = reportList;
}
public List<Criteria> getCriteriaList() {
return criteriaList;
}
public void setCriteriaList(List<Criteria> criteriaList) {
this.criteriaList = criteriaList;
}}
Criteria.java
package com.example.retrofitapp;
import com.google.gson.annotations.SerializedName;
public class Criteria {
#SerializedName("id_criteria")
int id_criteria;
#SerializedName("criteria_name")
String criteria_name;
public Criteria(int id_criteria, String criteria_name) {
this.id_criteria = id_criteria;
this.criteria_name = criteria_name;
}
public int getId_criteria() {
return id_criteria;
}
public void setId_criteria(int id_criteria) {
this.id_criteria = id_criteria;
}
public String getCriteria_name() {
return criteria_name;
}
public void setCriteria_name(String criteria_name) {
this.criteria_name = criteria_name;
}}
Report.java
package com.example.retrofitapp;
import com.google.gson.annotations.SerializedName;
public class Report {
#SerializedName("id_report")
int id_report;
#SerializedName("report_name")
String report_name;
public Report(int id_report, String report_name) {
this.id_report = id_report;
this.report_name = report_name;
}
public int getId_report() {
return id_report;
}
public void setId_report(int id_report) {
this.id_report = id_report;
}
public String getReport_name() {
return report_name;
}
public void setReport_name(String report_name) {
this.report_name = report_name;
}}
RetrofitClient.java
package com.example.retrofitapp.api;
import com.google.gson.*;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class RetrofitClient {
public static Retrofit retrofit;
public static Retrofit getRetrofitClient(String baseUrl){
if(retrofit==null){
Gson gson = new GsonBuilder().setLenient().create();
retrofit = new Retrofit.Builder().baseUrl(baseUrl).addConverterFactory(GsonConverterFactory.create(gson)).build();
}
return retrofit;
}}
Constants.java
package com.example.retrofitapp;
public class Constants {
public static String base_url = "https://api.jsonbin.io/";
}
Api.java
package com.example.retrofitapp.api;
import com.example.retrofitapp.GetReport;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Headers;
public interface Api {
#Headers("Secret-key:$2a$10$WxkkTylkdR7NwGSoPwrfy.Odxtj7MR2vDtYZBp9cOd0SaYGVRmhOm")
#GET("/b/5ff8172e63e86571a2e35639")
Call<GetReport> getReport();
}
MainActivity.java
package com.example.retrofitapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.example.retrofitapp.api.Api;
import com.example.retrofitapp.api.RetrofitClient;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//call method here
populateData();
}
private void populateData() {
Retrofit retrofit = RetrofitClient.getRetrofitClient(Constants.base_url);
Api api = retrofit.create(Api.class);
Call<GetReport> getReportCall = api.getReport();
//make asynchronous request
getReportCall.enqueue(new Callback<GetReport>() {
#Override
public void onResponse(Call<GetReport> call, Response<GetReport> response) {
if(response.code() == 200){
GetReport getReport = (GetReport) response.body();
//get response code
int responseCode = getReport.getResponse_code();
//get message
String message = getReport.getMessage();
//get data
Data data = getReport.getData();
//get reports(loop)
for(Report report : data.getReportList()){
//your report here
}
//get criteria(loop)
for(Criteria criteria : data.getCriteriaList()){
//your criteria here
}
}
}
#Override
public void onFailure(Call<GetReport> call, Throwable t) {
//do something if the request failed
}
});
}}
That is how I solved it.

Unable to set Pojo Object variable with an external value during JaxB MoXY Unmarshalling

I am trying the above approach that you have mentioned for NodeAdaptar, but I am getting null for my element.
Scenario:
I have to set the value of variable defined in my Java Model(POJO) Class with a value, coming externally(not part of the input XML). This value will be used in some calculation post unmarshal.
input XML: its a huge one. I am able to unmarshall it correctly.
POJO Class:
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlValue;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import org.eclipse.persistence.oxm.annotations.XmlPath;
#XmlRootElement(name="root")
#XmlAccessorType(XmlAccessType.FIELD)
public class MainModelClass {
#XmlPath("abc/def/Result/text()")
#XmlElement(name="Result")
private String result;
#XmlPath("/abc/def/Score/text()")
#XmlElement(name="Score")
private String score;
///This is where I want to populate the external value ///
#XmlElement
#XmlJavaTypeAdapter(PCNDateAdaptar.class)
private PCNDate pcnDateObject;
public PCNDate getPcnDateObject() {
return pcnDateObject;
}
public void setPcnDateObject(PCNDate pcnDateObject) {
this.pcnDateObject = pcnDateObject;
}
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
public String getScore() {
return score;
}
public void setScore(String score) {
this.score = score;
}
// Block for overridden toString() //
}
Adaptar Class:
import javax.xml.bind.annotation.adapters.XmlAdapter;
public class PCNDateAdaptar extends XmlAdapter<Integer, PCNDate> {
private PCNDate pcnDateObject;
public void setPcnDateObject(PCNDate pcnDateob) {
this.pcnDateObject = pcnDateob;
}
#Override
public PCNDate unmarshal(Integer v) throws Exception {
if(v == null)
return this.pcnDateObject;
else
return this.pcnDateObject;
}
#Override
public Integer marshal(PCNDate v) throws Exception {
if(v == null)
return null;
else
return null;
}
}
PCNDate.class:
public class PCNDate {
private Integer processControlDate;
public Integer getProcessControlDate() {
return processControlDate;
}
public void setProcessControlDate(Integer pcn) {
this.processControlDate = pcn;
}
}
Unmarshaller Method:
public static <T> T getXMLSnippetObject(String xmlString, Class<T> modelClass, XmlAdapter<?, ?> xmlAdapterObject) throws XMLStreamException, JAXBException {
JAXBContext context = JAXBContext.newInstance(modelClass);
Unmarshaller unmarshaller = context.createUnmarshaller();
unmarshaller.setAdapter(xmlAdapterObject);
InputStream xmlInputStream = IOUtils.toInputStream(xmlString);
XMLInputFactory2 xmlInputFactory = (XMLInputFactory2)XMLInputFactory.newInstance();
xmlInputFactory.setProperty("javax.xml.stream.isCoalescing", true);
xmlInputFactory.setProperty("javax.xml.stream.isNamespaceAware", true);
xmlInputFactory.setProperty("javax.xml.stream.isReplacingEntityReferences", true);
xmlInputFactory.setProperty(XMLInputFactory2.P_AUTO_CLOSE_INPUT, true);
//xmlInputFactory.setProperty(XMLInputFactory2.P_DTD_OVERRIDE, false);
xmlInputFactory.setProperty(XMLInputFactory2.P_REPORT_PROLOG_WHITESPACE, false);
xmlInputFactory.setProperty(XMLInputFactory2.P_PRESERVE_LOCATION,false);
xmlInputFactory.setProperty(XMLInputFactory2.P_INTERN_NS_URIS, true);
XMLStreamReader2 xmlStreamReader = (XMLStreamReader2) xmlInputFactory.createXMLStreamReader(xmlInputStream);
T objectInstance = (T) JAXBIntrospector.getValue(unmarshaller.unmarshal(xmlStreamReader, modelClass));
return objectInstance;
}
Main Calling Class:
public class XMLparsing {
public static void main(String[] args) throws XMLStreamException, JAXBException, IOException {
String xmlString = // Xml String //
MainModelClass modelObj = null;
Integer pcnDate = 20171010;
// PCNDate & PCNDateAdapter are both no-arg-constructor classes for JAXB purpose
PCNDate pcnObj = new PCNDate();
pcnDateObj.setProcessControlDate(pcnDate);
PCNDateAdaptar pcndateAdaptar = new PCNDateAdaptar();
pcndateAdaptar.setPcnDateObject(pcnObj);
modelObj = XmlUtilsStAX.getXMLSnippetObject(xmlString, MainModelClass.class, pcndateAdaptar);
}
Result:
The whole Xml String is getting correctly parsed. Only MainModelClass's pcnDateObject is null, result & score have values. I want pcnDateObject to have 20171010.
I don't know, what I am missing, please help.

Jackson #JsonUnwrapped behaviour with custom JsonSerializer

I have two classes like this:
public class A {
String aProp = "aProp";
public String getAProp() {
return aProp;
}
}
public class B {
String bProp = "bProp";
A a = new A();
#JsonProperty("bProp")
public String getBProp() {
return bProp;
}
#JsonSerialize(using = CustomSerializer.class)
public A getA() {
return a;
}
}
I'm expecting to get JSON like this:
{
"bProp": "bProp", // just serizlised bProp
"sProp1": "sProp1_aProp", // computed using aProp
"sProp2": "sProp2_aProp" // computed another way
}
So I wrote custom JsonSerializer like this:
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
public class CustomSerializer extends JsonSerializer<A> {
#Override
public void serialize(A a, JsonGenerator json, SerializerProvider provider) throws IOException {
json.writeStringField("sProp1", "sProp1_" + a.getAProp());
json.writeStringField("sProp2", "sProp2_" + a.getAProp());
}
}
But I keep getting an error:
com.fasterxml.jackson.core.JsonGenerationException: Can not write a field name, expecting a value
Unless I put json.writeStartObject(); and json.writeEndObject(); in serialize method (so it produces wrong JSON).
So I'm looking for a solution like #JsonUnwrapped to use with custom JsonSerializer.
I understand your problem and the thing that you need is UnwrappingBeanSerializer. You can see another related SO post:
Different JSON output when using custom json serializer in Spring Data Rest
The problem is that you cannot have both annotations #JacksonUnwrapped and #JsonSerialize in one field because when you have #JsonSerializer Jackson will always write field name.
Here is the complete solution:
public class CustomSerializer extends UnwrappingBeanSerializer {
public CustomSerializer(BeanSerializerBase src, NameTransformer transformer) {
super(src, transformer);
}
#Override
public JsonSerializer<Object> unwrappingSerializer(NameTransformer transformer) {
return new CustomSerializer(this, transformer);
}
#Override
protected void serializeFields(Object bean, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonGenerationException {
A a = (A) bean;
jgen.writeStringField("custom", a.getAProp());
jgen.writeStringField("custom3", a.getAProp());
}
#Override
public boolean isUnwrappingSerializer() {
return true;
}
}
Test case, you should redefine your object mapper with custom configuration or research for other method .
#RunWith(SpringJUnit4ClassRunner.class)
#WebAppConfiguration
#SpringApplicationConfiguration(classes = Application.class)
public class ColorsTest {
ObjectMapper mapper = new ObjectMapper();
#Before
public void setUp(){
mapper.registerModule(new Module() {
#Override
public String getModuleName() {
return "my.module";
}
#Override
public Version version() {
return Version.unknownVersion();
}
#Override
public void setupModule(SetupContext context) {
context.addBeanSerializerModifier(new BeanSerializerModifier() {
#Override
public JsonSerializer<?> modifySerializer(SerializationConfig config, BeanDescription beanDesc, JsonSerializer<?> serializer) {
if(beanDesc.getBeanClass().equals(A.class)) {
return new CustomSerializer((BeanSerializerBase) serializer, NameTransformer.NOP);
}
return serializer;
}
});
}
});
}
#Test
public void testSerializer() throws JsonProcessingException {
System.out.println(mapper.writeValueAsString(new B()));
}
}
Class B:
public class B {
#JsonProperty("bProp")
public String getBProp() {
return "bProp";
}
#JsonUnwrapped
public A getA() {
return new A();
}
}
I like to add this post and solution to the question asked here: Using custom Serializers with JsonUnwrapperd as the original poster is using JsonSerializer as I am. The suggest approach with the UnwrappingBeanSerializer won't work in this case. My post has a slightly different goal, but the idea from the post should be applicable to your use case easily, as it is just overwriting one more method and not having to add bunch of stuff apart from JsonUnwrapped on the property.
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonUnwrapped;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import java.io.IOException;
public class Test {
static class A {
String aProp = "aProp";
public String getAProp() {
return aProp;
}
}
static class B {
String bProp = "bProp";
A a = new A();
#JsonProperty("bProp")
public String getBProp() {
return bProp;
}
#JsonSerialize(using = CustomSerializer.class)
#JsonUnwrapped
public A getA() {
return a;
}
}
static class CustomSerializer extends JsonSerializer<A> {
#Override
public boolean isUnwrappingSerializer() {
return true;
}
#Override
public void serialize(A a, JsonGenerator json, SerializerProvider provider) throws IOException {
json.writeStringField("sProp1", "sProp1_" + a.getAProp());
json.writeStringField("sProp2", "sProp2_" + a.getAProp());
}
}
public static void main(String... a) throws Exception {
final ObjectMapper o = new ObjectMapper();
o.enable(SerializationFeature.INDENT_OUTPUT);
System.out.println(o.writeValueAsString(new B()));
}
}

JAXB CDATA without eclipselink

Is there a way to use CDATA in JAXB without using EclipseLink API ?
JAXB Class :
#XmlRootElement(name = "Documentation")
#XmlAccessorType(XmlAccessType.FIELD)
public class Documentation {
#XmlJavaTypeAdapter(AdaptorCDATA.class)
#XmlValue
protected String value; //setter & getter implemeted
}
Marshaller Property : required eclipselink api org.eclipse.persistence.oxm.CharacterEscapeHandler
marshaller.setProperty(CharacterEscapeHandler.class.getName(), new org.eclipse.persistence.oxm.CharacterEscapeHandler() { // property required for CDATA
#Override
public void escape(char[] ac, int i, int j, boolean flag,
Writer writer) throws IOException {
writer.write( ac, i, j ); }
});
Adaptor Class :
import javax.xml.bind.annotation.adapters.XmlAdapter;
public class AdaptorCDATA extends XmlAdapter<String, String> {
#Override
public String marshal(String arg0) throws Exception {
return "<![CDATA[" + arg0 + "]]>";
}
#Override
public String unmarshal(String arg0) throws Exception {
return arg0;
}
}

Why doesn't this simple example of JSON marshalling in Apache Camel work?

I've spent quite a while trying to figure this out. I am working on writing a service which receives a user name and password. It then uses a processor to generate an authentication token which is returned in the Out part of the message. I want to accept JSON formatted parameters, and am trying to get type conversion to work correctly. I've reduced the problem to a self contained unit test which is below:
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.model.dataformat.JsonDataFormat;
import org.apache.camel.model.dataformat.JsonLibrary;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;
import com.thoughtworks.xstream.annotations.XStreamAlias;
public class BasicJsonMarshallingTest extends CamelTestSupport {
#Override
protected RouteBuilder createRouteBuilder() throws Exception {
final Processor simpleProcessor = new Processor() {
#Override public void process(Exchange exchange) throws Exception {
SimpleBean bean = exchange.getIn().getBody(SimpleBean.class);
if(bean == null){
return;
}
exchange.getOut().setBody("a=" + bean.getA() + " b=" + bean.getB());
}
};
return new RouteBuilder() {
#Override public void configure() throws Exception {
JsonDataFormat jsonFormat = new JsonDataFormat(JsonLibrary.XStream);
jsonFormat.setUnmarshalType(SimpleBean.class);
from("direct:service").unmarshal(jsonFormat).process(simpleProcessor);
}
};
}
#Test
public void testSuccessfulAuthentication(){
Exchange lAuthRequest = createExchangeWithBody("{\"simple\":{\"a\":\"v1\",\"b\":\"v2\"}}");
template.send("direct:service", lAuthRequest);
assertEquals("a=v1 b=v2", lAuthRequest.getOut().getBody());
}
#XStreamAlias("simple")
public static final class SimpleBean {
private String a;
private String b;
public void setA(String a) {
this.a = a;
}
public String getA() {
return a;
}
public void setB(String b) {
this.b = b;
}
public String getB() {
return b;
}
}
}
When I run this test, I get this exception in the console:
com.thoughtworks.xstream.mapper.CannotResolveClassException: simple
at com.thoughtworks.xstream.mapper.DefaultMapper.realClass(DefaultMapper.java:56)[xstream-1.4.1.jar:]
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)[xstream-1.4.1.jar:]
<snip>
Am I approaching this wrong somehow? Please Help!
I found one way to make this work. I switched to Jackson as my JSON parser and it worked. To do this, all I had to do was change the RouteBuilder to this:
return new RouteBuilder() {
#Override public void configure() throws Exception {
from("direct:service").unmarshal().json(JsonLibrary.Jackson, SimpleBean.class).process(simpleProcessor);
}
};
I also had to change the format of the JSON being sent over the wire from this:
{"simple":{"a":"v1","b":"v2"}}
to this (which I like more anyway):
{"a":"v1", "b":"v2"}

Categories

Resources