Retrieving Parameter Values in JAX-RS Request Bodies

A task in my javascript involves making an ajax post request to one of my JAX-RS functions. Here is a snippet of the code:

 var postPackingListRequest = $http({
              method: "post",
              url: "/rest/v1/submit",
              data: $scope.items
          });

In the JAX-RS method, I am trying to retrieve the variable $scope.items that was passed. I am aware of how to get path params from the URL like this:

public Response getPathParams(@QueryParam("id") int id) {

However, I am unsure about how to access the data sent in the body of the request.

Thank you for any assistance.

UPDATE

@POST
@Path("submit")
@Consumes(MediaType.APPLICATION_JSON)
@ApiResponses({ @ApiResponse(code = 201, response = Response.class) })
@Produces("application/json")
public Response submitPackingList(TestUser testUser) {

}

public class TestUser {
    public String id;
    public String name;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

When attempting to send a request with TestUser data, I encounter an HTTP 400 error. Here is the format of the request being sent:

var postPackingListRequest = $http({
              method: "post",
              url: "/rest/v1/submit",
              data: "{'user':'1', 'name':james}"
          });

Answer №1

Suppose you have a method designed to handle GET requests like the example below:

    @GET
    public Response getPathParams(@QueryParam("id") int id)

In this method, every argument must be annotated with something, such as QueryParam or PathParam. However, in JAX-RS, only one argument is allowed without annotation. Let's say you have a User object structured like this:

    public class User{
    String userName;
    int userId;
    //getters and setters
    }

If you want to accept user details coming via the body of a request, your method signature would look like this:

    @GET
    public Response getPathParams(@QueryParam("id") int id, User user)

Depending on whether the above method consumes JSON or XML, the request body will be converted into a User object and bound to your argument. If using JSON, an explicit MessageBodyReader will be required.

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

Testing the selection of submenus in a main menu using a Selenium test case

Need help creating a selenium test case to choose the mean from the menu bar and then selecting a submenu. Here is my menu structure: https://i.sstatic.net/4s5ms.png Below is the code snippet where I am able to select the Admin menu but unsure how to pr ...

Continuously search for a node and adjust its permissions in a recursive manner

Looking at the tree data structure I have: type DataType = { id: string; access: 'view' | 'none'; isDisabled: boolean; children: DataType[]; }; export const Data: DataType = { id: '1', access: 'view', ...

Combining Array Attributes to Create a New Property as a 'JSON String'

I'm attempting to combine the attributes of an array into a JSON-like string as a new attribute. For example: [{ { "orderNo":"1", "description":"Item 1", "note": "Note 1" }, { "orderNo":"2", "description":"Item 2", ...

Nextjs unexpectedly displays blank page upon fetching data from Firebase Firestore without any error messages

I am currently facing an issue when trying to retrieve page details from Firebase Firestore using getStaticPaths and getStaticProps in my Next.js project. Despite following the code structure correctly, I am encountering a scenario where the page appears e ...

How can I increase the element by $100 using a dropdown selection in JavaScript?

Hey there! Looking to create a dropdown list with two shipping options: Special Shipping Normal Shipping <select> <option>Special Shipping</option> <option>Normal Shipping</option> </select> If the user selects Speci ...

Attempting to utilize solution for the "ajax-tutorial-for-post-and-get" tutorial

I've been exploring the implementation of the second answer provided in a post about Ajax tutorials on a popular coding forum. Despite following the instructions, I encountered an issue where the $.ajax script, triggered by an "onclick" function, only ...

using javascript to trigger android function

Looking to create a button in HTML that triggers a call from an Android device via JavaScript. Here is the code snippet I have tried, but it's not functioning as expected. Please note, I am new to Android development: public class MainActivity extend ...

Automatically modify browser configurations to disable document caching

Is it possible to prevent browsers from caching pages using JavaScript? I've noticed that even though PHP has a redirection implemented once the user logs in, when they press the browser's history button, it goes back to the login form. This is b ...

setting up PHP on Tomcat 7 within a windows environment

I attempted to install PHP5.2.16 on Tomcat 7 using PECL5.2.5, I carefully followed the instructions provided here: Run a php app using tomcat?, written by Saunik Singh. Unfortunately, I encountered an error that is preventing me from proceeding, and I am u ...

Is the Packery image grid only functional when the background image is specified in CSS and not in JavaScript? Perhaps we need to look into using Await/Sem

I've successfully implemented a packery image grid that is responsive and functional when the background image in the .item-content section is defined in the CSS file: http://codepen.io/anon/pen/eJdapq .item-content { width: 100%; height: 100%; ...

When using AngularJS to poll a $resource at regular intervals, the promise that is returned from the $resource call does not automatically get resolved

Let's get to the important details: When I link a $scope variable in my controller directly to a $resource, Angular takes care of promise resolution automatically and updates my view as soon as data is received from the service. However, I need to per ...

Unable to retrieve image: status code 402 - payment required

When trying to fetch my Facebook page's posts using the Facebook graph API and nextjs with vercel, I encountered an error: GET imageurl 402 payment required. Oddly enough, this works perfectly fine in localhost: https://i.sstatic.net/JGy2g.png I sus ...

Enhancing Material UI KeyboardDatePicker with personalized CSS design

Material UI KeyboardDatePicker is the component I'm currently using. https://i.sstatic.net/It50L.png In order to remove the black line visible in the datepicker (as shown in the screenshot), what steps should I take? Displayed below is the code ...

Tips for locating the highest value within an array, encompassing numerical values

I am looking for a way to identify the maximum negative value in an array of length 5, regardless of whether the integers in the array are positive or negative. I understand this should be straightforward, but any guidance you can offer would be greatly ...

Is there a way to delay the start of this until a legitimate answer is provided in a pop-up window?

Is it possible to delay the loading of this content until a prompt box is answered with a valid response, and have it only appear once a month? Do I need anything beyond JS and HTML for this functionality? <script language="javascript"> function ...

Facing issues with React JS and Material UI? If you're encountering problems where 'this.props' is

After running the code provided below, I anticipated that the styles would be injected within props. However, I consistently encounter undefined props. No props are being supplied to this component. const customStyles = theme => ({ root: { & ...

Omitting a Term in Regular Expressions with Java | Mobile Visual Recognition API | Extracting Text Block

I created a license plate scanning program that only recognizes the word "SINDH" and not the numbers above it, like AFR-2020. It skips over AFR-2020 and goes straight to Textblock. Below is the regular expression. How can I exclude "SINDH"? String REGEX = ...

Extract information from a JSON file to populate a jQuery Datatable

I am attempting to include data from a JSON file that was created using a Django script. Here is the structure of the JSON file: [ { "6": "yo1", "1": "2019-04-04", "4": "yo1", "3": "yo1", "2": "yo1", "5": "yo1" }, { "6": "yo2" ...

Vue js for filtering and replacing prohibited words

For this scenario, our objective is to screen the words in our input: <input type="text" class="form-control" placeholder="Write something..." v-model="todoInput""> Below are the restricted words that we aim to substitute in the input "restrict ...

Trouble getting CSS and JavaScript to function properly with HTTPS?

It seems that the css and js files are functioning properly on , but not on . As a novice in web design and javascript, I am struggling to find a solution to this issue. Trying to link the CSS file with "//isdhh.org/css/style.css" has not resolved the pr ...