Check the validity of the JSON input object using Jackson library

My REST service includes a method that uses POST and consumes application/json. The argument of the method is another class that describes the fields required in the JSON input. Is there a way to configure Jackson to validate the input JSON before it enters the method?

@PUT
@Consumes("application/json")
public UserDataResourceObject login(UserResourceObject userResourceObject)
{
    Query query = getEm().createNamedQuery("User.authorize");
    if (userResourceObject == null)
    {
        throw new WebApplicationException(Response.Status.BAD_REQUEST);
    }
    query.setParameter("username", userResourceObject.getUsername());
    query.setParameter("password", Utility.getMD5Value(userResourceObject.getPassword()));
    Exception e = null;
    try
.................

The UserResourceObject defines this structure:

{
  "username" : "admin",
  "password" : "123456"
}

If I modify the structure of the above JSON, I want Jackson to throw an exception, like 400 (Bad Request). How can I achieve this?

Answer №1

To ensure the accuracy of the data you receive, consider implementing Bean Validation (JSR 303). Two popular implementations to explore are available at and .

You can integrate validation into your code like this:

@PUT
@Consumes("application/json")
public UserDataResourceObject login(@Valid UserResourceObject userResourceObject) {

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

Seeking information from JSON data using an internet address

Currently, I am utilizing the following URL to access my JSON data: myapp/laravelRoute/jsondata By entering this in the address bar, the JSON array below will be shown: [{"A":"B"},{"B":1},{"C","http"}] However, when I input the URL as follows: myapp/l ...

What is the best way to increase incrementally with Selenium POM?

Can someone help me figure out how to select two quantities of the same item in Selenium POM using a for loop? My current solution is not working, and I need to know how to increment twice in POM. Below is the file where my page objects are stored: packa ...

Using Ext JS to send an AJAX request with a JSON object to an ASP.NET MVC web application

Currently, I am attempting to transmit a json object (extjs client) to an asp.net server-side application: Here is the sample request with logic intact: var orderData = []; for (i = 0; i < checkedList.length; i++) { orderData.push ...

jQuery GetJson fails to execute success function

I'm trying to use this code for an Ajax request to a JSON file: let formData = $("#myform").serialize(); $.getJSON("/files/data.json?" + formData, function(response){ alert(response); } ); However, there are no errors and the alert is n ...

Switch Your PHP Script to Use PDO

Hello, I am new to PHP and seeking some guidance. My goal is to convert the following code to PDO in order to generate a JSON output for an Android app that I am currently working on. I have tried several solutions but encountered issues with the JSON resp ...

Encountering a Selenium Webdriver issue of "Element not click-able at specified point" in regards to PrimeFaces SelectOneMenu

I've browsed through various resources to find a solution to my current issue, but none of the suggested fixes seem to work for me. The problem I'm facing is that my tests on SelectOneMenu elements are successful in Firefox and Internet Explorer, ...

Is it possible to create a standard response template that includes fields for value, error, and warning messages?

The official documentation sample states that responses from OneNote may have the following structure: { value:{the content we requested}, error:{error if exists with warnings inside if exist}, @api.diagnostics:{warnings if exist} } However, if the ...

What is the best approach for filtering multiple fields with identical names in logstash?

For the purpose of filtering, visualizing, and comparing results using Kibana, I am integrating tsung logs into ElasticSearch (ES). To accomplish this task, I am utilizing logstash and its JSON parsing filter to push tsung logs in JSON format into ES. Ts ...

Issue with JSON handling on iOS devices

I have created a JSON app, but I seem to be facing an issue with my JSON code. Despite validating it on various validators that all confirm its validity, I am encountering the following error: Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value aro ...

Increasing Nested Values in MongoDB Using NodeJS

Looking at this JSON structure: JSON = { "change_number": "CRQ91320s23", "change_description": "Nexusg work", "date": "2/10/2020", "changeowner" : "Jeff ...

Tips on transferring two JSON lists from one response to another feature file

I'm having trouble passing 2 lists to a read function. I've tried several approaches but haven't been able to find the solution. Here's what I've attempted: #step 1 * url callPathProvider = anything Given url callPath ...

python Using urllib2 to retrieve JSON data

I need help retrieving a JSON object from a specific URL. I've provided the correct API key, but when I attempt to do the following: data=json.loads(a.read()) print data I encounter this error: Traceback (most recent call last): File "C:\Pyt ...

"Troubleshooting: Node.js encountering a path error while loading a JSON file with

I am organizing a collection of folders and files structured like this: viz |_ app.js // node application |_ public |_ css |_ bubblemap.css |_ images |_ nuts |_ nuts0.json |_ script ...

Tips for incorporating HTML elements into XML emails

I'm facing an issue with my email template where the body of the email is being pulled as a String, resulting in raw html appearing in the email. Does anyone know how to insert an html tag into the body of an XML email using a String? <body> ...

Decoding JSON data from a URL on the Arduino IDE using ESP8266

My process involves programming the esp8266 using the Arduino IDE and connecting to a local network via WIFI. The goal is to download JSON content generated on the local server, with the following code being used: #include <ESP8266WiFi.h> #incl ...

passing JSON data using JavaScript or jQuery

I have a JSON snippet that I need to parse using JavaScript or jQuery and convert into variables: name and meetup. Can you help me with this? Below is the JSON code: { "MYID": 1, "module": [ { "name": "Manchester", ...

The use of JSON format is not supported by the REST API method in [Java]

I am currently running tests on a REST API method in postman. The interesting thing is that when I perform the test using x-wwww-form-urlencoded, I receive the correct result, but when attempting it with the Json format, I encounter a status code of 500 - ...

Serialize long values in exponential notation format using Spring MVC's REST JsonSerializer

My Spring MVC REST application has a custom Json serializer (for ObjectMapper) for handling LocalDate objects: public class DateSerializer extends JsonSerializer<LocalDate> { public LocalDateSerializer() { super(); } @Overr ...

Performing Bag operations concurrently with Python Dask

I'm currently working on processing a JSON file using Dask and read_text. However, I've noticed that only one core is being utilized at 100% when I check the Linux Systems Monitor. How can I determine if my operations on a Dask Bag can actually b ...

Attempted everything... Clicking away but a div refuses to show up post-click, resulting in an error message

When attempting to click a button, I encountered an error stating that the element is not clickable at point (1333, 75) as another element would receive the click. This issue arose while using Google Chrome version 65. <li class="slds-dropdown-trigge ...