Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8209605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T09:46:55+00:00 2026-06-07T09:46:55+00:00

I’m developing a web service using REST (Jersey 1.8). Currently I’m using XML to

  • 0

I’m developing a web service using REST (Jersey 1.8). Currently I’m using XML to communicate between the Java client and the server.

I need to change it to JSON: how can I do that? I have bunch of auto generated code from NetBeans, and have no idea what to do and how. When the testing the service it shows the JSON data. What I’m unable to do is deal with it within my main method.
enter image description here

these are the tutorial I followed

  • http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/RESTfulWebServices/RESTfulWebservices.htm
  • http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/RESTfulWebServices_Part2/RESTfulWebservicesPart2.htm
  • http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/RESTfulWebServices_Part3/RESTfulWebservicesPart3.htm

My Java client main method:

public class SOATestClient {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        PersonJerseyClient client = new PersonJerseyClient();
        ClientResponse response = client.findAll_XML(ClientResponse.class);


        GenericType<List<Person>> genericType = new GenericType<List<Person>>() {
        };
// Returns an ArrayList of Players from the web service
        List<Person> data = new ArrayList<Person>();
        data = (response.getEntity(genericType));
        System.out.println("Retreiving and Displaying Players Details");
        for (Person person : data) {
            System.out.println("FirstName: " + person.getName());
            System.out.println("ID : " + person.getId());
            System.out.println(" Age : " + person.getAge());
        }
        client.close();
    }
}

personjerseycilent

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jerseyclients;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.UniformInterfaceException;
import com.sun.jersey.api.client.WebResource;

/**
 * Jersey REST client generated for REST resource:PersonFacadeREST
 * [entity.person]<br>
 *  USAGE:
 * <pre>
 *        PersonJerseyClient client = new PersonJerseyClient();
 *        Object response = client.XXX(...);
 *        // do whatever with response
 *        client.close();
 * </pre>
 *
 * @author rj45
 */
public class PersonJerseyClient {
    private WebResource webResource;
    private Client client;
    private static final String BASE_URI = "http://localhost:8080/SOATestService/resources";

    public PersonJerseyClient() {
        com.sun.jersey.api.client.config.ClientConfig config = new com.sun.jersey.api.client.config.DefaultClientConfig();
        client = Client.create(config);
        webResource = client.resource(BASE_URI).path("entity.person");
    }

    public void remove(String id) throws UniformInterfaceException {
        webResource.path(java.text.MessageFormat.format("{0}", new Object[]{id})).delete();
    }

    public String countREST() throws UniformInterfaceException {
        WebResource resource = webResource;
        resource = resource.path("count");
        return resource.accept(javax.ws.rs.core.MediaType.TEXT_PLAIN).get(String.class);
    }

    public <T> T findAll_XML(Class<T> responseType) throws UniformInterfaceException {
        WebResource resource = webResource;
        return resource.accept(javax.ws.rs.core.MediaType.APPLICATION_XML).get(responseType);
    }

    public <T> T findAll_JSON(Class<T> responseType) throws UniformInterfaceException {
        WebResource resource = webResource;
        return resource.accept(javax.ws.rs.core.MediaType.APPLICATION_JSON).get(responseType);
    }

    public void edit_XML(Object requestEntity) throws UniformInterfaceException {
        webResource.type(javax.ws.rs.core.MediaType.APPLICATION_XML).put(requestEntity);
    }

    public void edit_JSON(Object requestEntity) throws UniformInterfaceException {
        webResource.type(javax.ws.rs.core.MediaType.APPLICATION_JSON).put(requestEntity);
    }

    public void create_XML(Object requestEntity) throws UniformInterfaceException {
        webResource.type(javax.ws.rs.core.MediaType.APPLICATION_XML).post(requestEntity);
    }

    public void create_JSON(Object requestEntity) throws UniformInterfaceException {
        webResource.type(javax.ws.rs.core.MediaType.APPLICATION_JSON).post(requestEntity);
    }

    public <T> T findRange_XML(Class<T> responseType, String from, String to) throws UniformInterfaceException {
        WebResource resource = webResource;
        resource = resource.path(java.text.MessageFormat.format("{0}/{1}", new Object[]{from, to}));
        return resource.accept(javax.ws.rs.core.MediaType.APPLICATION_XML).get(responseType);
    }

    public <T> T findRange_JSON(Class<T> responseType, String from, String to) throws UniformInterfaceException {
        WebResource resource = webResource;
        resource = resource.path(java.text.MessageFormat.format("{0}/{1}", new Object[]{from, to}));
        return resource.accept(javax.ws.rs.core.MediaType.APPLICATION_JSON).get(responseType);
    }

    public <T> T find_XML(Class<T> responseType, String id) throws UniformInterfaceException {
        WebResource resource = webResource;
        resource = resource.path(java.text.MessageFormat.format("{0}", new Object[]{id}));
        return resource.accept(javax.ws.rs.core.MediaType.APPLICATION_XML).get(responseType);
    }

    public <T> T find_JSON(Class<T> responseType, String id) throws UniformInterfaceException {
        WebResource resource = webResource;
        resource = resource.path(java.text.MessageFormat.format("{0}", new Object[]{id}));
        return resource.accept(javax.ws.rs.core.MediaType.APPLICATION_JSON).get(responseType);
    }

    public void close() {
        client.destroy();
    }

}

I try to access it with the following, and deal it with same way as XML,

ClientResponse response = client.findAll_JSON(ClientResponse.class);

but it gives me

Exception in thread "main" javax.ws.rs.WebApplicationException: javax.xml.bind.UnmarshalException
 - with linked exception:
[com.sun.istack.internal.SAXParseException2; lineNumber: 0; columnNumber: 0; unexpected element (uri:"", local:"id"). Expected elements are <{}person>]
    at com.sun.jersey.core.provider.jaxb.AbstractListElementProvider.readFrom(AbstractListElementProvider.java:251)
    at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:553)
    at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:523)
    at soatestclient.SOATestClient.main(SOATestClient.java:33)
Caused by: javax.xml.bind.UnmarshalException

I would be grateful to if you could help me on this matter. Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T09:46:58+00:00Added an answer on June 7, 2026 at 9:46 am

    1) Whoever is generating this error, is clearly expecting XML input. Not JSON. You need to change that ASAP:

     javax.ws.rs.WebApplicationException: javax.xml.bind.UnmarshalException
     com.sun.istack.internal.SAXParseException2;
     <= javax.xml.bind and SAXParse are both XML-only: JSON not invited
    

    2) The stuff in your screen shot (presumably Jersey?) is definitely OK.

    3) I haven’t followed the whole tutorial, and you haven’t given enough information to tell where you went astray.

    SUGGESTION:

    Just retrace your steps in the tutorial, and make sure you’re selecting “JSON” (not XML, and not SOAP) every step of the way.

    =========== ADDENDUM ===========

    OK – Thanx for the update. Here’s where we’re at:

    1) This is the problem:

    Exception in thread "main" javax.ws.rs.WebApplicationException: javax.xml.bind.UnmarshalException
     - with linked exception:
    [com.sun.istack.internal.SAXParseException2; lineNumber: 0; columnNumber: 0; unexpected element (uri:"", local:"id"). Expected elements are <{}person>]
        at com.sun.jersey.core.provider.jaxb.AbstractListElementProvider.readFrom(AbstractListElementProvider.java:251)
        at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:553)
        at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:523)
        at soatestclient.SOATestClient.main(SOATestClient.java:33)
    Caused by: javax.xml.bind.UnmarshalException
    

    2) You said this stack traceback is coming from the client.

    So your server is 100% OK – the ONLY thing you need to do is fix your client. Cool 🙂

    3) The traceback shows the client is expecting XML … but getting JSON instead.

    So the ONLY thing you should need to fix is to tell your client “Hey: read JSON, not XML”. Again – cool 🙂

    4) How do you do that?

    Well, for starters, you need to get rid of this line (if you haven’t already):

    // Bad, bad bad.  Don't do this!|
    ClientResponse response = client.findAll_XML(ClientResponse.class);
    

    5) You might want to change other parts of your client code – I don’t know.

    You might also want to change your client’s configuration – I don’t know that, either.

    6) Suggestion: look at this other tutorial – it might point you in the right direction:

    • http://www.vogella.com/articles/REST/article.html

    NOTE:

    WHATEVER you need to do – it should be REALLY simple! Please review the link, review your code and your test client configuration … and post back what you find!

    Thank you in advance…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have thousands of HTML files to process using Groovy/Java and I need to
That's pretty much it. I'm using Nokogiri to scrape a web page what has
In my XML file chapters tag has more chapter tag.i need to display chapters
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.