0

I am getting below error when trying to initializing a model class and set the velue.

2021-10-29T22:31:59,017 ERROR SecureExceptionHandlerImpl,http-nio-8080-exec-8:116 - No serializer found for class java.util.Timer and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)

Below is the model class

    public class ModelClass implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 613977314761191083L;
    private String id;
    private String name;
    private Timer timer;
    
    public ModelClass() {
        timer = new Timer();
    }

    /**
     * @return the id
     */
    public String getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println(name + " " + new Date().toString());
            }
        }, 0, 2000);
    }

    /**
     * @return the timer
     */
    public Timer getTimer() {
        return timer;
    }

    /**
     * @param timer the timer to set
     */
    public void setTimer(Timer timer) {
        this.timer = timer;
    }

  }

I gone through many suggestion from stackoverflow but could not get any proper solution for it. Could anyone please help me how can I fixed this exception?

Debasish Halder
  • 173
  • 3
  • 12
  • 1
    It would be good if you could add links to the questions which you tried but did not solve the issue. Also, why do you want to serialize the `timer` object ? – Gautham M Oct 30 '21 at 06:05
  • https://stackoverflow.com/questions/55388862/no-serializer-found-for-class-java-io-bufferedreader https://stackoverflow.com/questions/61076443/how-to-solve-the-error-no-serializer-found-for-class-java-io-bytearrayinputstre https://stackoverflow.com/questions/46212246/no-serializer-found-for-class-java-io-bytearrayinputstream I followed the above links... @Gautham – Debasish Halder Oct 30 '21 at 06:26
  • 4
    Why do you want to serialize the `timer` object? If the timer depends on some values, then include those in the class and annotate `timer` with `JsonIgnore` – Gautham M Oct 30 '21 at 07:29
  • 1
    Why would a model even have a timer? Models are supposed to be data, not functionality. – VGR Oct 30 '21 at 17:28
  • 1
    And instead of `java.util.Timer`, developers should use a `ScheduledExecutorService` anyway. – Holger Nov 01 '21 at 08:26
  • @JsonIgnoreProperties(value = { "foo" }) Solved my prblem after used the above annotation in the model class. – Debasish Halder Nov 02 '21 at 09:35

0 Answers0