Rest client using jersey returns empty object

im trying to create a simple RESTful web service client. Using java and javax jersey + dependencies. My problem is trying to convert from XML, which the object is in, and return an empty object.

The test is a simple test. I want to get a language with a specific identifier, from my test I can see that getting the XML data is the same as in the browser.

The code for the classes is as follows:

Tongue

package data; import javax.xml.bind.annotation.*; @XmlRootElement(name = "prestashop") @XmlType public class Language { private int id; private String name; private String iso_code; private String language_code; private boolean active; private boolean is_rtl; private String date_format_lite; private String date_format_full; public Language() { } public Language(String name, String iso_code, String date_format_lite, String date_format_full) { this.name = name; this.iso_code = iso_code; this.date_format_lite = date_format_lite; this.date_format_full = date_format_full; } public Language(int id, String name, String iso_code, String language_code, boolean active, boolean is_rtl, String date_format_lite, String date_format_full) { this.id = id; this.name = name; this.iso_code = iso_code; this.language_code = language_code; this.active = active; this.is_rtl = is_rtl; this.date_format_lite = date_format_lite; this.date_format_full = date_format_full; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getIso_code() { return iso_code; } public void setIso_code(String iso_code) { this.iso_code = iso_code; } public String getLanguage_code() { return language_code; } public void setLanguage_code(String language_code) { this.language_code = language_code; } public boolean isActive() { return active; } public void setActive(boolean active) { this.active = active; } public boolean isIs_rtl() { return is_rtl; } public void setIs_rtl(boolean is_rtl) { this.is_rtl = is_rtl; } public String getDate_format_lite() { return date_format_lite; } public void setDate_format_lite(String date_format_lite) { this.date_format_lite = date_format_lite; } public String getDate_format_full() { return this.date_format_full; } public void setDate_format_full(String date_format_full) { this.date_format_full = date_format_full; } @Override public String toString(){ return "Language [" + "id=" + id + ", " + "name=" + name + ", " + "iso_code=" + iso_code + ", " + "language_code=" + language_code + ", " + "active=" + active + ", " + "is_rtl=" + is_rtl + ", " + "date_format_lite=" + date_format_lite + ", " + "date_format_full=" + date_format_full+ "," + " ]"; } } 

home

 package webservicetest2; import com.sun.jersey.api.client.Client; import com.sun.jersey.api.client.WebResource; import com.sun.jersey.api.client.config.ClientConfig; import com.sun.jersey.api.client.config.DefaultClientConfig; import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter; import data.Language; import java.net.URI; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.UriBuilder; import javax.xml.bind.JAXBException; public class WebServiceTest2 { /** * @param args the command line arguments */ public static void main(String[] args) throws JAXBException { try { ClientConfig config = new DefaultClientConfig(); Client client = Client.create(config); client.addFilter(new HTTPBasicAuthFilter("KEY", "")); WebResource service = client.resource(getBaseURI()); System.out.println(service.path("languages") .path("7") .accept(MediaType.TEXT_XML) .get(String.class)); Language language; language = (Language) service.path("languages") .path("7") .accept(MediaType.TEXT_XML) .get(Language.class); System.out.println(language.toString()); } catch (Exception e) { System.out.println(e.getMessage()); } } private static URI getBaseURI() { return UriBuilder.fromUri("http://localhost:8080/api").build(); } } 

Output

 run: <?xml version="1.0" encoding="UTF-8"?> <prestashop xmlns:xlink="http://www.w3.org/1999/xlink"> <language> <id><![CDATA[7]]></id> <name><![CDATA[Danish]]></name> <iso_code><![CDATA[da]]></iso_code> <language_code><![CDATA[da]]></language_code> <active><![CDATA[1]]></active> <is_rtl><![CDATA[0]]></is_rtl> <date_format_lite><![CDATA[Ymd]]></date_format_lite> <date_format_full><![CDATA[Ymd H:i:s]]></date_format_full> </language> </prestashop> Language [id=0, name=null, iso_code=null, language_code=null, active=false, is_rtl=false, date_format_lite=null, date_format_full=null, ] BUILD SUCCESSFUL (total time: 0 seconds) 

As you can see, XML im get back has an object, but printing an object is an empty object. So something is wrong.

Sorry if a question was asked about the simulator, but havent been able to find it.

+4
source share

Source: https://habr.com/ru/post/1487925/


All Articles