An issue occurred when trying to retrieve JSON data using JAXB, but the data can be easily obtained in XML format

package com.marketplace.acres.dummyapp.test;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.xml.bind.annotation.XmlRootElement;


@Path("/fortest")
@XmlRootElement
public class ForTest {
    public int id;
    public String name;

    public ForTest( ){

    }


    public ForTest(int id, String name){
        this.id = id;
        this.name = name;

    }


    @GET
    @Produces(MediaType.APPLICATION_XML)
    public ForTest getMessages(){
        ForTest emp1 = new ForTest(22,"sachin");
        return emp1;

        }
    }

This particular piece of code generates the intended XML output:

<forTest>
<id>22</id>
<name>sachin</name>
</forTest>

However, upon attempting to retrieve the data in JSON format by switching:

@Produces(MediaType.APPLICATION_XML) to @Produces(MediaType.APPLICATION_JSON), an error is encountered:

SEVERE: MessageBodyWriter not found for media type=application/json, type=class com.marketplace.acres.dummyapp.test.ForTest, genericType=class com.marketplace.acres.dummyapp.test.ForTest.

Is there a method to obtain the data in JSON format?

Answer №1

Resolved by removing comments from the following sections in pom.xml

    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-moxy</artifactId>
    </dependency>

Initially, I was puzzled by the presence of both EffectivePOM.xml and pom.xml in my Eclipse project - what sets them apart?. The effective POM had this section already uncommented.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for handling function calls that result in RuntimeExceptions in Java?

I have to utilize tapestry-json as my JSON-API in Java: import org.apache.tapestry5.json.JSONObject; import org.apache.tapestry5.json.JSONArray; Unfortunately, there is no method called optJSONObject(int) to read JSONObjects from a JSONArray. Instead, th ...

Troubleshooting Problems with Ruby Arrays, JavaScript, and JSON

I am facing a challenge with rendering a highcharts plugin in my rails application. I suspect it could be related to the sql queries fetching data from the database and converting them into a ruby array that the javascript code fails to interpret correctly ...

Guide on how to efficiently insert JSON data into three different tables and retrieve the data from two tables in JSON format using knex and express.js

How to store JSON data in multiple tables and extract specific table data using knex and express.js The task at hand involves inserting data into three separate tables - login, user, and profile. Following this insertion, we need to retrieve the user and ...

Issues with decoding JSON data in PHP

Struggling to assign a JSON Object to a PHP Variable? When attempting to use var_dump, it's showing NULL. Here is the PHP code: <?php $json = utf8_encode(file_get_contents('http://socialclub.rockstargames.com/ajax/stat/1/profile_body/sta1/ ...

Decoding a JSON array containing multiple JSON objects into an ArrayList of org.json.JSONObject instances using Gson

I have a JSON string that looks like this: { "r": [ { "pic": "1.jpg", "name": "Name1" }, { "pic": "2.jpg", "name": "Name2" }, { "pic": "3.jpg", "name": "Name3" } ] } My goal is to convert it ...

Efficient Inter-service Communication Utilizing a Common Object Layer for Request and Response Handling

Currently, I am working on a distributed system that is built on Spring Boot with communication between services being handled by REST API calls. With more than 8 services in place, I am looking to create a common communication layer or library for all of ...

In the console, a JSON string is displayed, however the PHP variable outputs as null

Below is the snippet of javascript code I am working with: $('.showEditForm').click(function () { var webpagefileID = this.id; if($('#editForm').css('display', 'none')) { $('#editForm').css ...

Retrieve the stored information within JsonConverter.CanConvert()

Is there a way to create a custom JsonConverter that converts a string to a TimeSpan object only if the string follows the format of "hh:mm:ss"? I have explored creating a custom JsonConverter, but I encountered an issue with the CanConvert() method as it ...

Performing iteration over a list of elements using Java Selenium by targeting

I attempted to iterate through a list on a website. The separate line ID is as follows: //*[@id="lista-wiersz-74813704"] The number varies, so I believe I cannot use it. A different web element is: <li id="lista-wiersz-74779144" class = "linkDoKart ...

How can I obtain tabular output for Boto3 | Python data?

When using the AWS CLI, you have the option to set the output format as either json or table. While I am able to obtain JSON output using json.dumps, is there a way to achieve output in table format? I attempted to use pretty table but did not succeed. ...

The dropdown list in the edit form of jQgrid is displaying an additional quotation mark

In my MVC2 EditStatesController, I have the following code: public JsonResult GetStates() { string statesToReturn = GetStates(); // returns "1: Alabama; 2: Alaska; 3: Arizona; 4: Arkansas" return Json(statesToReturn); } ...

I am puzzled by this error in Typescript: "Why does the element have an 'any' type when the Object type lacks an index signature?"

Looking to extract an array of keys from an object with nested properties, my current code: public static getKeys(obj: Object) { let keys: string[] = []; for (let k in obj) { if (typeof obj[k] == "Object" && obj[k] !== null) { ...

How can I create a JSON response in ServiceStack that includes only the primary key?

After creating a new record in my table, I want to generate a JSON response that includes only the primary ID of the newly created record. For example: {"PrimaryID": 123} Currently, I am using the following custom function: // Adds a new row to the P ...

User account management in AngularJS web services

My latest project involves developing a web application on Azure. The backend is powered by a web API service while the frontend is supported by AngularJS, both hosted on Azure. I am now looking to implement a login page with individual user accounts, but ...

Just a quick inquiry regarding adding new line characters in JSON to be used in

After encountering an issue with a JSON file in my JavaScript application where it would not print new lines when viewed on the console, I am at a loss for a solution. The contents of my JSON file are as follows: [ { "id": "71046" ...

When I attempt to run a Selenium test using WebDriver, Internet Explorer fails to open

I am completely new to selenium, and I recently used the selenium firefox IDE to record a scenario. After saving the test case in java format, I attempted to run it using the selenium-ie driver and IEDriverServer.exe. Below is the content of my test case ...

The process of linking a JsonObject attribute to XML using MOXy

I am currently working with the following class structure: @XmlRootElement(name = "Root") class MyClass { @XmlElement(name = "Entries") JsonObject getProperty() { ... } } My goal is to generate the following XML output upon marshalling: <Roo ...

Getting a count value from the server and converting it into JSON format on an Android device

Seeking assistance with retrieving the number of rows from a server table using JSON parsing and PHP in an Android project. The PHP code shown below successfully fetches the value, but I am unsure about how to proceed with parsing the JSON data. Any guidan ...

Retrieving cascading values in a dropdownlist using Asp .NET MVC

In my current project, I am using Asp.Net MVC4. In a form, I have implemented cascading dropdown lists. When I select "Distrito" in one dropdown list, it correctly loads the corresponding values for "Sucursal". The insertion of records works fine. However, ...

Generating a list from JSON entities

I am tasked with creating a specific list format using values from a JSON object. <li> <span class="room">A20</span> <span class="dropin">3</span> <span class="appoint">1</span> <span class="del ...