Looking at this code:
#Service
public class WebService {
public WebService() {
this.webClient = WebClient.builder()
.baseUrl(URL)
.build();
}
public Mono<Person> searchById(String id) {
return webClient.get().uri("/v3/"+ id).retrieve().bodyToMono(Person.class);
}
}
And this:
WebService ws = new WebService();
Mono<Person> version = ws.searchById("1");
System.out.println(version.toProcessor().block());
This code works well in getting one person object from a JSON, however I wanted a method to return more than one person from a JSON like so:
public Mono<List<Person>> or public Mono<Person[]>
And I can't seem to make it work. I've tried what I found here, but I don't understand what they are doing here:
return Arrays.stream(objects)
.map(object -> mapper.convertValue(object, Reader.class))
.map(Reader::getFavouriteBook)
.collect(Collectors.toList());
And in my case I want to have a list of objects Person on my main function, not just part of the object like in their case, where they use Reader::getFavouriteBook to apparently map a Book.
Converting this to an answer, for better readability.
WebClient can convert the response body into a mono of array. So, instead of performing bodyToMono(Person.class) we can do bodyToMono(Person[].class).
Now the resulting Mono is a Mono<Person[]>. You can then perform map, subscribe or flatMap as you wish (one or many times) to chain actions. Once all the actions are chained, you can then call block to wait till all the actions are executed and get back the result (or an error).
See this documentation to know more about all the supported methods on Mono.
Note - Using block in a reactive application is not recommended.
EDIT
Here is an example on how a Mono<Person> is converted to a Mono<PersonDTO>
import reactor.core.publisher.Mono;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
public class Test {
public static void main(String[] args) {
Person person = new Person();
Mono<Person> personMono = Mono.just(person);
Mono<PersonDTO> personDTOMono = personMono.map(p -> {
PersonDTO dto = new PersonDTO();
dto.setName(p.name);
dto.setVersion(p.version);
return dto;
}).delayElement(Duration.of(1000L, ChronoUnit.MILLIS));
System.out.println("Waiting for 1000 millis");
System.out.println("personDTOMono = " + personDTOMono.block());
}
}
class Person {
int version = 10;
String name = "test";
}
class PersonDTO {
private int version;
private String name;
#Override
public String toString() {
return "PersonDTO{" +
"version=" + version +
", name='" + name + '\'' +
'}';
}
public int getVersion() {
return version;
}
public void setVersion(int version) {
this.version = version;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Related
I have a predicate that I use to filter a list of the same Entity Object:
Predicate<DWHDeal> companyFilter = i -> i.getCompany().equals(company);
I also have to apply the same filter, with the exact same condition on the exact same field, on a list of DTOs where the DTOS is built based on the entity from before:
Predicate<DWHDealDTO> companyFilterDTO = i -> i.getCompany().equals(company);
Is it possible to achieve this without instancing two different predicates? If possible, I would like to achieve this by making only one Predicate.
Assuming getCompany() returns a String you could create Predicate<String>:
Predicate<String> predicate = s -> s.equals(company);
And then using it like:
list.stream()
.filter(dto -> predicate.test(dto.getCompany()))
...
But there is not much benefit since it requires almost the same code.
If equality is only check then you can use static Predicate isEqual(Object targetRef). see java doc https://docs.oracle.com/javase/8/docs/api/java/util/function/Predicate.html#isEqual-java.lang.Object-
class StudentView{
String name;
public StudentView(String name) {
this.name = name;
}
}
class StudentDTO{
String name;
public StudentDTO(String name) {
this.name = name;
}
}
public void testPredicate(){
StudentView studentView= new StudentView("John");
StudentDTO studentDTO = new StudentDTO("Sam");
Predicate p = Predicate.isEqual("John");
System.out.println("Test for Student View "+ p.test(studentView.name));
System.out.println("Test for Student DTO "+ p.test(studentDTO.name));
}
I think you will need a Function<T,R> before using Predicate :
There are two concepts to Function. First is a java.util.function.Function which accepts one argument and produces a result. The second is stream intermediate operation map which converts each element in a stream into another object via the supplied function.
In your case the Function should look like :
Function<DWHDeal, DWHDealDTO> myFunction = new Function<DWHDeal, DWHDealDTO>() {
public DWHDealDTO apply(DWHDeal t) {
return ... ;
}
};
I tried the basic Program as below with success:
static class DWHDeal{
String name;
public DWHDeal(String name) {
this.name = name;
}
}
static class DWHDealDTO{
String name;
public DWHDealDTO(String name) {
this.name = name;
}
}
static Predicate<DWHDealDTO> companyFilter = i -> i.name.equalsIgnoreCase("com");
public static void main(String[] args) {
Function<DWHDeal, DWHDealDTO> myFunction = new Function<DWHDeal, DWHDealDTO>() {
public DWHDealDTO apply(DWHDeal t) {
return new DWHDealDTO("com");
}
};
DWHDeal newDWHDealDTOObj = new DWHDeal("com");
System.out.println(companyFilter.test(myFunction.apply(newDWHDealDTOObj))); //Works
}
As suggested in the comments, the common interface would be the preferred solution.
I guess you could do something like this, but to be fair, it is ugly.
private String getCompany(Object o) {
if(o instanceof DWHDeal)
return ((DWHDeal) o).getCompany();
else
return ((DWHDealDTO) o).getCompany();
}
Predicate<Object> companyFilter = i -> getCompany(i).equals(company);
I'm trying to create my own lazy load implementation using CGLib, but i've faced some strange behavior that I cannot explain.
Here is what i'm doing.
Proxy instance is being created like follows:
public static <T> T newInstance(Long sourceId, SourceMapper<T> sourceMapper) {
Class<?> proxyTargetType = sourceMapper.getType();
//mapper will use provided sourceId in order to load real object from the DB
Function<Long, T> mapper = sourceMapper.getMapper();
return (T) Enhancer.create(proxyTargetType,
new DynamicProxy<>(sourceId, mapper));
}
Here is the usage of the code above:
Order order = new Order();
try {
//heavy object is being proxied
long customerTariffId = rs.getLong("customer_tariff_id");
order.setCustomerTariff(DynamicProxy
.newInstance(customerTariffId, CUSTOMER_TARIFF_MAPPER));
}
Heavy object should be loaded only if any of its methods gets invoked:
public Object intercept(Object obj, Method method, Object[] args,
MethodProxy methodProxy) throws Throwable {
T source = this.getSource(); // loads real object using sourceId and mapper
if(source == null) return null;
return method.invoke(source, args);
}
It works perfectly if this.getSource() loads some object.
But here what i'm getting if we assume, that order.getCustomerTariff() should return null (this.getSource() will return null)
LOG.debug("{}", order.getCustomerTariff()); //null (1)
LOG.debug("{}", order.getCustomerTariff() != null); //true (2)
I assume, that for some reason toString() gets invoked at line (2), so i'm getting String null instead of literal null. That's why it is not equal to a literal null in the comparison clause.How do you think, is there any way to return a regular null at line (2) and receive a correct value of false during that check?
EDIT
Class being proxied looks similar to this:
public class CustomerTariff extends DomainEntity {
private Customer customer;
//some other similar fields
private Tariff tariff;
public CustomerTariff() {
}
public CustomerTariff(Customer customer
Tariff tariff) {
this.customer = customer;
this.tariff = tariff;
}
public CustomerTariff(Long id, Customer customer,
Tariff tariff) {
super(id);
this.customer = customer;
this.tariff = tariff;
}
//getters and setters
#Override
public String toString() {
return "CustomerTariff{" +
"customer=" + customer +
", tariff=" + tariff +
"} " + super.toString();
}
}
public abstract class DomainEntity {
private Long id;
public DomainEntity() {}
public DomainEntity(Long id) {
this.id = id;
}
#Override
public String toString() {
return "DomainEntity{" +
"id=" + id +
'}';
}
}
I assume that you are intercepting your toString method from your interceptor and you do not get the interception chain you expect. Specify a method filter that only hits the methods you want to intercept and you should get the expected result.
i have a domain class(DB):
public class PersonDoamin {
private String name;
private String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
i also have model class:
public class PersonBean extends PersonDoamin {
}
so when i go to DAOImpl class and query for List and transfer this list to List and return to users as i have interface method for List getAllPerson(). so my questions is here when i transfer all data from List. Here i have some utility method that copies from one bean to another like this:
List<PersonDoamin> list = PersonDAO.getAllPersons();
List<PersonBean> pbList = new ArrayList<PersonBean>();
/* this below logic is pretty much in the all DAO impl*/
for(PersonDoamin p : list){
PersonBean pb = new PersonBean();
CopyHelper.copyBean(p, pb);
pbList.add(pb);
}
return pbList;
can we replace the looping and copying and adding to another list and returning part with somekind of generic method which will take any object two list and loop thorugh one and add it to another passed List parameter and return it. something like below which is not perfect right now:
public static <T> List<T> listToArray(List<T> list,List<T> list2) {
for(T element : list){
list2.add(element);
}
return list2;
}
public static void main(String[] args) {
List<PersonDoamin> personList = new ArrayList<PersonDoamin>();
PersonDoamin p = new PersonDoamin();
p.setName("aj");
p.setAge("25");
personList.add(p);
List<PersonBean> personBeansToReturn = new ArrayList<PersonBean>();
Test.listToArray(personList , personBeansToReturn );
}
A bit off topic, your design seems a bit weird that you have "Domain" class and "Bean" class and have "Bean" extends "Domain"...
Anyway, come back to your question, what you are trying to do is:
You have a List<Domain>
You want to transform each Domain in the List into a Bean (by use of some util method)
Put the resulting Beans into a list and return
Let's go through it step by step.
(by the way, the listToArray method you wrote does not align with your original loop as it does not do the transformation (point 2). I guess it is typo?)
(all psuedo code as I don't have environment on hand to make it compile. Concept should be correct I guess)
Step 1: Util method for Person
One biggest problem of your original util method is that, it is illegal to put a Parent object instance to a List of Child (it should be easy to figure why by yourself).
The util method should look like this:
List<PersonBean> toBeans(List<PersonDomain> domains) {
List<PersonBean> beans = new ArrayList<>(domains.size());
for (PersonDomain domain: domains) {
PersonBean bean = new PersonBean();
CopyHelper.copyBean(domain, bean);
beans.add(bean);
}
return beans;
}
Step 2: Make it generic
The problem above is that it only works for Person. If you want to make it generic, you will also need to provide the function to transform Domain to Bean:
(Assume you are using Java8, should be trivial to make your own interface if you are using older version)
<D,B> List<B> toBeans(List<D> domains, Function<B,D> mapper) {
List<PersonBean> beans = new ArrayList<>(domains.size());
for (PersonDomain domain: domains) {
beans.add(mapper.apply(domain));
}
return beans;
}
so that you can use it by:
return toBeans(personDomains, (domain) -> {
PersonBean bean = new PersonBean();
CopyHelper.copyBean(domain, bean);
return bean;
});
(You may consider wrap the function if in most case you are going to use the CopyHelper way)
<D,B> List<B> toBeansByBeanCopy(List<D> domains, Class<B> beanClass) {
return toBeans(domains, (domain)-> {
B bean = beanClass.newInstance();
CopyHelper.copyBean(domain, bean);
return bean;
});
}
so that you can use it as
return toBeansByBeanCopy(personDomains, PersonBean.class);
Step 3: Java has done it for you
Actually what you are trying to do above, it is already provided by Java in Java 8. You can simply do:
return personDomains.stream()
.map(d -> {
PersonBean bean = new PersonBean();
CopyHelper.copyBean(domain, bean);
return bean;
})
.collect(Collectors.toList());
You may write a little method to use in the lambda expression if it is the standard way.
return personDomains.stream()
.map(BeanMapper.mapper(PersonBean.class))
.collect(Collectors.toList());
(Leave the implementation as your exercise)
If you're looking for a way to call new on a generic type, you can, sort of. You have to use reflection and call newInstance on the Class object. I don't know if this is going to be feasible for you.
Also, I don't see anyway of realistically implementing your bean copy method without using some heavy reflection as well. In the example below I faked by just casting to the required classes.
public class GenericCopyTest
{
public static void main( String[] args ) throws Exception
{
List<PersonDoamin> personList = new ArrayList<PersonDoamin>();
PersonDoamin p = new PersonDoamin();
p.setName( "aj" );
p.setAge( "25" );
personList.add( p );
List<PersonBean> personBeansToReturn = new ArrayList<PersonBean>();
copyAndDowncast( personList, personBeansToReturn, PersonBean.class );
System.out.println( personBeansToReturn );
}
public static <T,U extends T> List<U> copyAndDowncast( List<T> from,
List<U> to, Class<U> type )
throws InstantiationException, IllegalAccessException
{
for( T element : from ) {
U nu = type.newInstance();
copyBean( element, nu );
to.add( nu );
}
return to;
}
private static <X,Y extends X> void copyBean( X from, Y nu ) {
((PersonBean)nu).setName( ((PersonDoamin)from).getName() );
((PersonBean)nu).setAge( ((PersonDoamin)from).getAge() );
}
}
class PersonDoamin {
private String name;
private String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
#Override
public String toString()
{
return "PersonDoamin{" + "name=" + name + ", age=" + age + '}';
}
}
class PersonBean extends PersonDoamin {
#Override
public String toString()
{
return "PersonBean{" + getName() + ',' + getAge()+ '}';
}
}
Output:
run:
[PersonBean{aj,25}]
BUILD SUCCESSFUL (total time: 0 seconds)
Why not just use addAll() for this? It does what you're trying to do, and it's already part of the system library.
Remember you can add a PersonBean to a PersonDomain list, but not the other way around.
public class GenericCopyTest
{
public static void main( String[] args ) {
List<PersonDoamin> personList = new ArrayList<PersonDoamin>();
List<PersonBean> personBeansToReturn = new ArrayList<PersonBean>();
personList.addAll( personBeansToReturn );
personBeansToReturn.addAll( personList ); // <-- FAILS
// No suitable method found
}
}
class PersonDoamin {}
class PersonBean extends PersonDoamin {}
If you want to put more than one bean class in the same list,
how about creating the list with parent class PersonDoamin , and then, you can store both PersonDoamin and PersonBean classes.
public static void main(String[] args) {
List<PersonDoamin> personList = new ArrayList<PersonDoamin>();
PersonDoamin p = new PersonDoamin();
p.setName("aj");
p.setAge("25");
personList.add(p);
// Changed here. PersonBean => PersonDoamin
List<PersonDoamin> personBeansToReturn = new ArrayList<PersonDoamin>();
Test.listToArray(personList, personBeansToReturn);
// also you can insert PersonBean into the list
personBeansToReturn.add(new PersonBean());
}
{
"TestSuite":{
"TestSuiteInfo":{
"-description":"parse"
},
"TestCase":[
{
"TestCaseData":{
"-sequence":"sequential",
"-testNumber":"2",
"-testCaseFile":"testcase\\Web\\Ab.xml"
}
},
{
"TestCaseData":{
"-sequence":"sequential",
"-testNumber":"3",
"-testCaseFile":"testcase\\Web\\BC.xml"
}
}
]
}
}
My Pojos are:
public class TestSuite {
private TestSuiteInfo testSuiteInfo;
private TestCase listOfTestCases;
public TestSuiteInfo getTestSuiteInfo() {
return testSuiteInfo;
}
public void setTestSuiteInfo(TestSuiteInfo testSuiteInfo) {
this.testSuiteInfo = testSuiteInfo;
}
public TestCase getListOfTestCases() {
return listOfTestCases;
}
public void setListOfTestCases(TestCase listOfTestCases) {
this.listOfTestCases = listOfTestCases;
}
}
public class TestSuiteInfo {
private String description;
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
import java.util.Iterator;
import java.util.List;
public class TestCase {
private List<TestCaseData> testCaseData;
public List<TestCaseData> getTestCaseData() {
return testCaseData;
}
public void setTestCaseData(List<TestCaseData> testCaseData) {
this.testCaseData = testCaseData;
}
}
public class TestCaseData {
private String sequence;
private int testNumber;
private String testCaseFile;
public String getSequence() {
return sequence;
}
public void setSequence(String sequence) {
this.sequence = sequence;
}
public int getTestNumber() {
return testNumber;
}
public void setTestNumber(int testNumber) {
this.testNumber = testNumber;
}
public String getTestCaseFile() {
return testCaseFile;
}
public void setTestCaseFile(String testCaseFile) {
this.testCaseFile = testCaseFile;
}
}
I haven't use Jackson before, will really appreciate if anyone could help me in parsing the file and getting the objects.
I am trying to parse this from past two days, but didnt got any success
Usually to parse JSON with the Jackson library, you would use the ObjectMapper class like this:
public static void main(final String[] args) {
final String json = "some JSON string";
final ObjectMapper mapper = new ObjectMapper();
final TestSuite readValue = mapper.readValue(json, TestSuite.class);
//Then some code that uses the readValue.
//Keep in mind that the mapper.readValue() method does throw some exceptions
//So you'll need to handle those too.
}
However, I wrote a quick test class to check out the parsing of your JSON and came across some issues.
Basically, the design of the JSON and the design of the domain don't match up. So you can either alter the JSON, or you can alter the domain objects.
Altering the JSON to fit the domain
The property names that have "-" in them wont parse nicely in jackson, so they will need to be removed.
Having the class name before eachof the objects isn't going to help. Jackson will expect these to be properties, so the Class names will need removing or replacing with property names.
Property names must be provided as they are in the domain objects in order for jackson to parse them. You can't just say here's an object and then start a list, the list must have a property name/
After I'd adjusted a these things in the JSON, I got it to parse with the provided domain objects. The JSON I ended up with looked like this:
{
"testSuiteInfo":{
"description":"parse"
},
"listOfTestCases":{
"testCaseData":[
{
"sequence":"sequential",
"testNumber":"2",
"testCaseFile":"testcase\\Web\\Ab.xml"
},
{
"sequence":"sequential",
"testNumber":"3",
"testCaseFile":"testcase\\Web\\BC.xml"
}
]
}
}
Here's my test method that does parse the doctored JSON above (please ignore all the escape characters)
public static void main(final String[] args) {
final String json = "{\"testSuiteInfo\":{\"description\":\"parse\"}," +
"\"listOfTestCases\":{" +
"\"testCaseData\":[" +
"{\"sequence\":\"sequential\",\"testNumber\":\"2\",\"testCaseFile\":\"testcase\\\\Web\\\\Ab.xml\"}," +
"{\"sequence\":\"sequential\",\"testNumber\":\"3\",\"testCaseFile\":\"testcase\\\\Web\\\\BC.xml\"}" +
"]" +
"}" +
"}";
final ObjectMapper mapper = new ObjectMapper();
try {
final TestSuite readValue = mapper.readValue(json, TestSuite.class);
System.out.println(readValue.getListOfTestCases()); //just a test to see if the object is built
}
catch (final Exception e) {
e.printStackTrace();
}
}
Altering the domain to fit the JSON
Firstly, the main issues is having the Class names as the property identifiers. That makes it quite difficult to work with this JSON in the usual manner. I've had to add a couple of wrapper classes to get around the class names being in the JSON.
I've added an OverallWrapper class that has a TestSuite property to cater for the TestSuite class name in the JSON.
I've also added a TestCaseDataWrapper class to cater for the TestCaseData class names in the list in the JSON.
I removed the TestCase class all together as that just became a property on one of the other classes.
Then to make the property names match up with the objects, I've used the #JsonProperty annotation.
Here are the classes after the modifications, and the ultimate parser test method that works and parses the JSON. (again, excuse all the escape characters in the JSON string)
import org.codehaus.jackson.annotate.JsonProperty;
public class OverallWrapper {
private TestSuite testSuite;
#JsonProperty("TestSuite")
public TestSuite getTestSuite() {
return this.testSuite;
}
public void setTestSuite(final TestSuite testSuite) {
this.testSuite = testSuite;
}
}
import java.util.List;
import org.codehaus.jackson.annotate.JsonProperty;
public class TestSuite {
private TestSuiteInfo testSuiteInfo;
private List<TestCaseDataWrapper> testCaseData;
#JsonProperty("TestCase")
public List<TestCaseDataWrapper> getTestCaseData() {
return this.testCaseData;
}
public void setTestCaseData(final List<TestCaseDataWrapper> testCaseData) {
this.testCaseData = testCaseData;
}
#JsonProperty("TestSuiteInfo")
public TestSuiteInfo getTestSuiteInfo() {
return this.testSuiteInfo;
}
public void setTestSuiteInfo(final TestSuiteInfo testSuiteInfo) {
this.testSuiteInfo = testSuiteInfo;
}
}
import org.codehaus.jackson.annotate.JsonProperty;
public class TestSuiteInfo {
private String description;
#JsonProperty("-description")
public String getDescription() {
return this.description;
}
public void setDescription(final String description) {
this.description = description;
}
}
import org.codehaus.jackson.annotate.JsonProperty;
public class TestCaseDataWrapper {
#JsonProperty("TestCaseData")
private TestCaseData testcaseData;
public TestCaseData getTestcaseData() {
return this.testcaseData;
}
public void setTestcaseData(final TestCaseData testcaseData) {
this.testcaseData = testcaseData;
}
}
import org.codehaus.jackson.annotate.JsonProperty;
public class TestCaseData {
private String sequence;
private int testNumber;
private String testCaseFile;
#JsonProperty("-sequence")
public String getSequence() {
return this.sequence;
}
public void setSequence(final String sequence) {
this.sequence = sequence;
}
#JsonProperty("-testNumber")
public int getTestNumber() {
return this.testNumber;
}
public void setTestNumber(final int testNumber) {
this.testNumber = testNumber;
}
#JsonProperty("-testCaseFile")
public String getTestCaseFile() {
return this.testCaseFile;
}
public void setTestCaseFile(final String testCaseFile) {
this.testCaseFile = testCaseFile;
}
}
public static void main(final String[] args) {
final String json = "{\"TestSuite\":{\"TestSuiteInfo\":{\"-description\":\"parse\"},\"TestCase\":[" +
"{\"TestCaseData\":{\"-sequence\":\"sequential\",\"-testNumber\":\"2\",\"-testCaseFile\":\"testcase\\\\Web\\\\Ab.xml\"}}," +
"{\"TestCaseData\":{\"-sequence\":\"sequential\",\"-testNumber\":\"3\",\"-testCaseFile\":\"testcase\\\\Web\\\\BC.xml\"}}" +
"]}}";
final ObjectMapper mapper = new ObjectMapper();
try {
final OverallWrapper readValue = mapper.readValue(json, OverallWrapper.class);
System.out.println(readValue.getTestSuite());
}
catch (final Exception e) {
e.printStackTrace();
}
}
Summing up
The ultimate issue is that the domain doesn't marry up with the JSON.
Personally I prefer to change the JSON to marry up to the domain, as the domain seems to make sense in it's design and requires less customization and forcing.
However, I do accept that you may not have that choice, hence the redesign of the domain.
In this blog you can find a simple way to parse a large json file without directly using Jackson's ObjectMapper
https://www.ngdata.com/parsing-a-large-json-file-efficiently-and-easily/
With jp.skipChildren() and nested loops you can reach to your section of interest and once you are there simply break the nested loops using a label:
outerloop: while (jp.nextToken() != JsonToken.END_OBJECT) {
//...nested loops here
break outerloop;
//...closing loops
}
I copied the code for reference:
import org.codehaus.jackson.map.*;
import org.codehaus.jackson.*;
import java.io.File;
public class ParseJsonSample {
public static void main(String[] args) throws Exception {
JsonFactory f = new MappingJsonFactory();
JsonParser jp = f.createJsonParser(new File(args[0]));
JsonToken current;
current = jp.nextToken();
if (current != JsonToken.START_OBJECT) {
System.out.println("Error: root should be object: quiting.");
return;
}
while (jp.nextToken() != JsonToken.END_OBJECT) {
String fieldName = jp.getCurrentName();
// move from field name to field value
current = jp.nextToken();
if (fieldName.equals("records")) {
if (current == JsonToken.START_ARRAY) {
// For each of the records in the array
while (jp.nextToken() != JsonToken.END_ARRAY) {
// read the record into a tree model,
// this moves the parsing position to the end of it
JsonNode node = jp.readValueAsTree();
// And now we have random access to everything in the object
System.out.println("field1: " + node.get("field1").getValueAsText());
System.out.println("field2: " + node.get("field2").getValueAsText());
}
} else {
System.out.println("Error: records should be an array: skipping.");
jp.skipChildren();
}
} else {
System.out.println("Unprocessed property: " + fieldName);
jp.skipChildren();
}
}
}
}
From the blog:
The nextToken() call each time gives the next parsing event: start object, start field, start array, start object, …, end object, …, end array, …
The jp.skipChildren() is convenient: it allows to skip over a complete object tree or an array without having to run yourself over all the events contained in it.
All the credits go to the blog's author: Molly Galetto
I'm trying to #POST a user-created object and get a Response with a different user-created payload as the entity. Although the object returned exists and is populated, on the client end it is empty.
Client sent / server received object:
#XmlRootElement
public class TweetQuery {
String query;
List<TweetQueryTweet> tweets = new ArrayList<>();
// setters and getters
}
public class TweetQueryTweet {
private String id;
private String text;
// setters and getters
}
Server received / client sent object:
#XmlRootElement
public class TweetClusters {
List<TweetCluster> tweetClusters = new ArrayList<>();
// setters and getters
}
public class TweetCluster {
List<String> labels = new ArrayList<>();
List<String> docs = new ArrayList<>();
// setters and getters
}
Client (Arquillian) Test:
#Test
#RunAsClient
public void test01SeeSomething(#ArquillianResource URL deploymentUrl) throws ... {
final URI targetURI = ...;
System.out.println(" test target:" + targetURI.toASCIIString());
Entity<TweetQuery> tweetQuery = Entity.entity(getTestTweetQuery(), MediaType.APPLICATION_JSON);
Client client = ClientBuilder.newBuilder().build();
WebTarget target = client.target(targetURI.toASCIIString());
Response response = target.request(MediaType.APPLICATION_JSON).post(tweetQuery);
TweetClusters x = response.readEntity(TweetClusters.class);
System.out.println("Entity:" + x);
System.out.println("Response: " + response.getStatus());
assertEquals(Status.OK.getStatusCode(), response.getStatus());
assertNotNull(x);
assertThat(x.getTweetClusters().size()).isGreaterThan(0);
}
Jersey Post method:
#POST
#Consumes(MediaType.APPLICATION_JSON)
#Produces(MediaType.APPLICATION_JSON)
public Response clusterPost(TweetQuery tweetQuery) {
TweetClusters tweetClusters = clusterService.getTweetClusters(tweetQuery);
System.out.println("clusterPost - in - tweetQuery: " + tweetQuery);
System.out.println(" - out tweetClusters: " + tweetClusters);
return Response.status(Status.OK).type(MediaType.APPLICATION_JSON).entity(tweetClusters).build();
}
Debug:
test target:http://localhost:18080/test//cluster
clusterPost - in - tweetQuery: [TweetQuery - query:TweetQuery query, tweets:[[TweetQueryTweet - id:1, text:text 1], [TweetQueryTweet - id:2, text:text 2], [TweetQueryTweet - id:3, text:text 3]]]
- out tweetClusters: [TweetClusters:[[TweetCluster - labels: [Other Topics], docs:[3, 2, 1]]]]
Entity:[TweetClusters:[]]
Response: 200
Line 2 - clusterPost - in -- shows TweetQuery is being marshalled properly.
Line 3 - clusterPost - out -- shows the tweetClusters to be sent as the Response entity exists
Line 4 - tweetClusters is not coming out of the request
Edit
I changed the REST method to return the tweetQuery that it receives as input and it is returned correctly. So its something about TweetClusters. Maybe I need a MessageBodyReader & Writer. Or a Moxy #XmlJavaTypeAdapter. But for what as Lists obviously work out of the box as TweetQuery works.
https://stackoverflow.com/users/383861/blaise-doughan are you out there? :)
Am I missing something simple?
oK I worked it out. I unintentionally told a porkie-pie. When I said I had setters and getters I was mising the setter for TweetClusters. Then once fixed i had a constructor with an argument but no no-arg constructor. Once the no-arg constructor added it was all good.
In summary you need to have in objects to be (un)marshalled:
A no-arg constructor if you have an arg constructor
Setters and getters for all the elements
And if you have more complex types including Date and Calendar you need to have a Adapter #XmlJavaTypeAdapter (Moxy) or #JsonSerialize.using in Jackson (or ??? in RESTeasy ...).
Interestingly I didn't need to have #XmlRootElement (Moxy) though kept it there for good measure.
The complete answer is:
The Client (Arquillian) Test is same as above
The Jersey Post method is same as above
The object classes that (un)marshall are:
#XmlRootElement
public class TweetClusters {
List tweetClusters = new ArrayList<>();
public void addCluster(Cluster c) {
TweetCluster tweetCluster = new TweetCluster(c);
tweetClusters.add(tweetCluster);
}
public List<TweetCluster> getTweetClusters() {
return tweetClusters;
}
public void setTweetClusters(List<TweetCluster> tweetClusters) {
this.tweetClusters = tweetClusters;
}
#Override
public String toString() {
return String.format("[TweetClusters:%s]", tweetClusters);
}
}
public class TweetCluster {
List labels = new ArrayList<>();
List docs = new ArrayList<>();
public TweetCluster() {
}
public TweetCluster(Cluster c) {
labels.add(c.getLabel());
for (Document doc : c.getDocuments()) {
docs.add(doc.getTitle());
}
}
public List<String> getLabels() {
return labels;
}
public void setLabels(List<String> labels) {
this.labels = labels;
}
public List<String> getDocs() {
return docs;
}
public void setDocs(List<String> docs) {
this.docs = docs;
}
#Override
public String toString() {
return String.format("[TweetCluster - labels: %s, docs:%s]", labels, docs);
}
}
public class TweetQuery {
String query;
List<TweetQueryTweet> tweets = new ArrayList<>();
public String getQuery() {
return query;
}
public void setQuery(String query) {
this.query = query;
}
public List<TweetQueryTweet> getTweets() {
return tweets;
}
public void setTweets(List<TweetQueryTweet> tweets) {
this.tweets = tweets;
}
public void addTweets(TweetQueryTweet... queryTweets) {
for (TweetQueryTweet tweet : queryTweets) {
this.tweets.add(tweet);
}
}
#Override
public String toString() {
return String.format("[TweetQuery - query:%s, tweets:%s]",query, tweets);
}
}
(Arghhh, sorry about the formatting; I can't fix it in SO)
For debugging purposes it is often good to get back the string representation returned from the response (ie. XML or JSON) and you simply specify the entity type as String.class:
String x = response.readEntity(String.class);
Try to return response like this : Response.ok(tweetClusters).build();
I'm not sure why you have the models Annotated with #XmlRootElements. So I would think you could remove that and make sure you are using Jackson to Serialize and Deserialize your request and response body. Which I assume you are or gson.