Categories
discuss

@JsonIgnoreProperties usage for both known and unknown properties

My current annotation for ignoring the known properties for a JPA entity is:

@JsonIgnoreProperties({"hibernateLazyInitializer", "handler","created","updated","createdBy","lastUpdatedBy"})

In addition to ignoring these class properties, I would also like to ignore any unknown properties that the server receives. I know the alone way to ignore the unknown properties by the following annotation:

@JsonIgnoreProperties(ignoreUnknown=true)

But not sure how to add this to my current annotation given above. I tried multiple methods ås below but none seem to work and I could not find an example online for this scenario.

Any example or leads on documentation would also help.

Answer

Set ignoreUnknown to true and define the names of properties to ignore in the value element:

@JsonIgnoreProperties(ignoreUnknown = true, 
                      value = {"hibernateLazyInitializer", "handler", "created"})

How does it work?

Have a look at this quote from the documentation (highlight is mine):

In its simplest form, an annotation looks like the following:

@Entity

The at sign character (@) indicates to the compiler that what follows is an annotation. In the following example, the annotation’s name is Override:

@Override
void mySuperMethod() { ... }

The annotation can include elements, which can be named or unnamed, and there are values for those elements:

@Author(name = "Benjamin Franklin", date = "3/27/2003")
class MyClass() { ... }

or

@SuppressWarnings(value = "unchecked")
void myMethod() { ... }

If there is just one element named value, then the name can be omitted, as in:

@SuppressWarnings("unchecked")
void myMethod() { ... }

Other way to handle unknown properties

To ignore unknown properties, you also could do:

ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
Categories
discuss

Uncheck radio buttons in react

I think its a very silly doubt but I am stuck in this.. I have multiple radio buttons and I want to uncheck all the radio buttons as soon as I click the other radio button. How can I achieve it in react?

var options = [{id:1,nm:"Appointment fixed"},{id:2,nm:"Call later"},{id:3,nm:"Wrong number"},{id:4,nm:"Don't call again"},{id:5,nm:"Call later"}];
{options.map((item,i) => {
                     return (
                        <div onChange={(i)=>this.callOptions(i)}>
                           <label className="radio-inline"><input type="radio" value={i} ref={'ref_' + i} value={item.id}/>{item.nm}</label>
                        </div>
                      )
                  })}

Answer

Give a common name attribute for you radio button. Change

<input type="radio" value={i} ref={'ref_' + i} value={item.id}/>

to

<input type="radio" value={i} name="abc" ref={'ref_' + i} value={item.id}/>

Categories
discuss

What is the exact meaning of instantiate in JAVA

I’am a newbie in JAVA and this came across this word called. “A class i.e. created inside a method is called local inner class in java. If you want to invoke the methods of local inner class, you must instantiate this class inside the method”. The word in bold. Can anyone please help me out with this one.I know it’s embarrassing and i should’ve researched more but I just cannot understand. Thanks.

Answer

First of all Declaring mean:

ClassName obj;

Simple meaning of instantiate is creating an object from class.

ClassName obj = new ClassName();

What is a object?

  • An instance of a class. From one class we can create many instances.
  • They are the basic runtime entities in in our program.
  • They may also represent user-defined data types such as lists and vectors.
  • Any programming problem is analyzed in terms of objects and nature of communication between them.

As a example:

//Define a reference(a variable) which can hold a `Person` obect.
Person p;
//Create a Person object(instantiate).
//new - use to allocate memory space for the new object
p = new Person();

What is a nested class?

A class that defined inside a class is called nested class. There 2 categories of nested classes.

  1. inner classes
  2. local classes
  3. annonymous classes

Inner class:

  • Inner class can only be accessed by the outer class. Not by any other class.
  • Inner class is a member of outer class.
  • Outer class can access inner class without importing.
  • Inner class can access any attribute or a method belong to outer directly.
  • Outer class cannot access directly to a inner class.

Example for a inner class:

class Outer{
   int i = 10;
   void main(){
      //instantiate inner class.
      Inner in = new Inner();
      in.show();
   }

   class Inner{
      void show(){
         System.out.print(i);
      }
   }
}

What is a local class?

Which are classes that are defined in a block.

Example:

public class{
  int i = 10;

  public main(){
     class A{
        void show(){
          System.out.println(i);
        }
     }

     //inside the method instantiate local class.
     A obj = new obj();
     obj.show();
  }
  //outside the main() -block(method)
  //inside another method instantiate local class.
  public test(){
    A obj = new A();
    obj.show();
  }
}
Categories
discuss

When do browsers download sourcemaps?

When do browsers download the sourcemap for a minified file?

I am struggling to find documentation for any browser on if/when they download a minified file’s sourcemap (an external file ending in a .map extension, like scriptname.min.js.map).

I am trying to decide how verbose a map file I want to create (which drastically affects the file size) but I don’t see where the file is downloaded in Google Chrome (not showing in the network tab of Dev Tools) yet it is available when I debug in the source tab.

Answer

Source Maps are downloaded and attached to the browser when you open the developer tools. Until then the browser is not aware of the sourceMap.

There is a code reference to the Chrome’s Dev tools

https://chromium.googlesource.com/chromium/src/+/refs/tags/75.0.3770.67/third_party/blink/renderer/devtools/front_end/sdk/DebuggerModel.js

this._sourceMapManager.setEnabled(Common.moduleSetting('jsSourceMapsEnabled').get());

Short Gist of what happens in the above code is

Once Devtools is attached the modal triggers and enables the sourceMapManager. The sourceMapManager manages the sourceMap in Chrome. Once the SourceMapManager is initialized it looks for the #sourceMapUrl downloads and attaches the map to the debugger.

Update Note: It’s not a practice to add sourcemap’s to a minified file, unless the sourcemap is served from a private network. This is for various reasons security, obfuscation etc. But this is just my opinion,it varies depending on your actual requirement. For Example: If you have any error tracking tools which needs the code to be attached, then you will have to attach the sourcemap.

Categories
discuss

JavaScript timer for a quiz

I am making a quiz game for a project in HTML and JavaScript. On every question, the player has 15 seconds to answer. I managed to do it like this:

<body onload="setTimeout(Timer,15000)">

and then in Js:

function Timer()
    {
         alert("You are out of time!");
    }

However, I want to be able to display how much time the player has left in a <p> tag. How could I do that?

Answer

<div id="count">Start</div>

var count = 15;
var interval = setInterval(function(){
  document.getElementById('count').innerHTML=count;
  count--;
  if (count === 0){
    clearInterval(interval);
    document.getElementById('count').innerHTML='Done';
    // or...
    alert("You're out of time!");
  }
}, 1000);
Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..