Guide on sending JavaScript property Array to Jersey JAX-RS JSON-based RESTful web service

Is it possible to send an array of JS strings to the server side within a JS Object field? Here is an example:

JSON:

{prodName: "abc123", prodImages: ["a1", "a2", "a3"]}

Currently, I am utilizing Jersey JAX-RS to handle the JSON input. The server-side code snippet looks like this:

Web service method signature:

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Path("/delete")
public void deleteMethod(ProdListVO prodListDeleteVO) //...

JAX-B Object representation:

@XmlRootElement
public class ProdListVO {   

    private String prodName;
    private String[] prodImages;

// ... getters and setters

My issue is that while prodName is correctly set, prodImages remains null. Is there a proper JSON format that Jersey recognizes as a valid Array or List of Strings?

Answer №1

Consider implementing:

{ "productName": "XYZ789", "productImages": ["x1", "x2", "x3"]}

Don't forget to add the following dependency:

    <dependency>
        <groupId>com.sun.jersey.api</groupId>
        <artifactId>jersey-client</artifactId>
    </dependency>

Answer №2

Would you like to try this approach:

RestAPIClass:

@DELETE
@Consumes(MediaType.APPLICATION_XML)
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Path("/delete")
public Response deleteProductList(final JAXBElement<ProdListVO> prodListDeleteVO) {
    ProdListVO prodList =  prodListDeleteVO.getValue();
    // additional code here
}   

JAXBModelClass:

@XmlAccessorType(XmlAccessType.FIELD)//allows field customization
@XmlRootElement(name="algorithm")
public class Algorithm implements Serializable{
    private static final long serialVersionUID = -1L;
    @XmlElement(name="prodName")//field name
    private String prodName = null;
    @XmlElement(name="prodImages")//another field name
    private String [] prodImages = null;

    //getters and setters
}

Answer №3

Upgrading to EclipseLink JAXB Moxy version 2.6 successfully resolved the issue, referring to the solution provided in this question:

Moxy, JSON and Jersey 2.0 does not deserialize plain String array

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

The Evolution of Alternatives to contentEditable

Related: ContentEditable Alternative I am curious about the timeline of online WYSIWYG editors prior to the existence of contentEditable. I remember using GDocs and GMail with rich-text features that functioned similarly to contentEditable. I would appre ...

Ensure data accuracy by triggering the cache - implementing SWR hook in Next.js with TypeScript

I recently implemented the swr hook in my next.js app to take advantage of its caching and real-time updates, which has been incredibly beneficial for my project (a Facebook clone). However, I encountered a challenge. The issue arises when fetching public ...

Obtaining the NodeValue from an input of type <td>

I have a HTML code snippet that I am trying to parse in order to extract the nodeValue of all elements within the table columns. <table id="custinfo"> <tr> <td><label>First Name</label></td> <td& ...

Issue with parentNode.replaceChild not functioning properly in Internet Explorer 6

In my HTML file created with FCK editor, I attempted to replace an existing table element with a new one using the parentNode.replaceChild method. While this change was successful in Internet Explorer 8, it resulted in errors when viewed in IE6 and IE7. ...

Adding nested JSON in a Rails environment

I recently encountered an issue with nesting objects within each other using the model. Here is an example of what I did: Ingresso model -> def as_json(options=nil) super(:include => [:usuario, :tipo_de_ingresso]) Within the tipo_de_ingresso mod ...

Why doesn't Mongoose automatically generate an _id for my array elements when I push them in?

I am looking for a way to have mongoose automatically add an _id field to the objects I push into my array. Here is my mongoose schema: var playerModel = new Schema({ user: { type: mongoose.Schema.Types.ObjectId, ref: "Users", }, cl ...

AFrame: keeping an element's world position and rotation intact while reparenting

I am attempting to reassign a child element (entity) to another parent while preserving its position, rotation, and possibly size in the scene. Ideally, I would like to implement a component (let's call it "reparent") that can be added to an entity to ...

Tips for sending JSON data through the POST method on an Android device

I have a URL and parameter called "data": URL: http://www.xxxx.ru/mobile-api-v1/query/ data={«type":1,"body":{"sortType":0,"categoryId":0,"count":50,"authorId":0,"lastId":0}} What is the correct way to include the key "data="? I keep getting an error mes ...

IntelliJ coverage for backend JavaScript

Is it possible to analyze code coverage in IntelliJ without using a browser? http://www.jetbrains.com/webstorm/webhelp/monitoring-code-coverage-for-javascript.html Though there are tutorials by JetBrains on code coverage, they all seem to require a browse ...

Log out of Google+ API results in an error message stating: '$apply already in progress'

After successfully implementing the signIn functionality using Google+ API in my AngularJS web app, I encountered some issues with getting the signOut functionality to work properly. Within one of my .html files (the Nav-bar), I have a function being call ...

Include the model.obj file into the three.MESH framework

I am a beginner in three.js and I am developing an augmented reality application on the web to showcase plates of food. Currently, I have successfully displayed a cube. However, when I attempted to display a model.obj instead of using geometry and material ...

Maximizing values entered into form fields

Looking for a way to extract the highest number from a set of input fields in an HTML form using JavaScript? Take this example: <input id="temp_<strong>0</strong>__Amount" name="temp[<strong>0</strong>].Amount" type="text" valu ...

Using Flask for the frontend and integrating Tensorflow, however not utilizing Tensorflow serving

I currently have a straightforward 3-layer MLP model in Tensorflow designed to predict a single numerical output based on a 10-value input vector. My web application runs on the Flask framework, allowing clients to send a new 10-value input vector every m ...

Creating a function that counts the number of times a button is clicked

I want to have the "game" button appear after the user clicks the "next-btn" 2 times. Once the multiple choice test has been completed twice, I'd like the game button to show up and redirect the user to a separate HTML page called "agario.html." I&ap ...

Using jQuery to measure the height of a div consistently shows a value of 1

I'm attempting to retrieve the height of a specific div using the following code: var divHeight = $("#myDiv").height(); referencing information from the JQuery documentation <div style="width: 300px; height: 300px;"></div ...

Encountering a 400 error code stating "Invalid JSON payload" while attempting to add a G Suite licenseAssignment using the Google Enterprise License Manager API

Lately, I have been utilizing the Google Enterprise License Manager API within Google Apps Script to effectively assign G Suite licenses to users. However, starting from last Friday on 4/24/20, I've encountered a recurring error message stating ' ...

What is the best way to loop through the body of a POST request in Express.js?

Currently, I am developing a general route handler for handling POST requests in NodeJS. My challenge is to iteratively go through the req.params of each POST request without having prior knowledge of what the parameters will be. So far, my attempts have ...

Unravel the HTML entities using jQuery

Is there a way to decode HTML entities using jQuery similar to the PHP function htmlspecialchars_decode? Here is the code I am currently using: if (jQuery.trim(jQuery("#push_alert").val()) != "") { alert = jQuery("#push_alert").val(); } Whenever the ...

The main class cannot be initialized due to an error: java.lang.NoClassDefFoundError caused by the absence of com/mashape/unirest/http/exceptions/UnirestException in JAVA

ISSUE When I execute the Maven 'compile' and 'install' command in Eclipse to generate an executable JAR and try to run the JAR file, I encounter the following error: Error: Unable to initialize main class org.example.project.StockTrac ...

React-xarrows is a stunning display of multiple arrows overlapping each other in a mesmerizing fashion

I am currently using the react-xarrows library within my project to connect tables in a diagram. However, I have encountered an issue where multiple links between two tables cause the arrows to overlap, resulting in only one visible link instead of the int ...