0

Rest Controller:

@RequestMapping(value = "/admin/rest/new-subscriptions")
public List<NewSubscriptionDTO> getNewSubscriptions() {
    NewSubscriptionDTO dto = new NewSubscriptionDTO();
    dto.setId("54");
    dto.setName("John Doe");
    return Arrays.asList(dto);
}

NewSubscriptionDTO:

package dermatica.web.admin.rx;

import com.fasterxml.jackson.annotation.JsonProperty;
import org.joda.time.DateTime;
import java.io.Serializable;

public class NewSubscriptionDTO implements Serializable {

    private String id;
    private 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;
    }
}

I get the following exception:

no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)

If I annotate the fields with @JsonProperty it work fine.

Is there a way for the serialization to work automatically without needing this annotation?

DV82XL
  • 5,350
  • 5
  • 30
  • 59
DD.
  • 21,498
  • 52
  • 157
  • 246
  • A few good suggestions here as well: https://stackoverflow.com/questions/8367312/serializing-with-jackson-json-getting-no-serializer-found – DV82XL Feb 28 '21 at 01:35
  • Just following up, have you solved this issue yet or did you decide to leave the annotations? – DV82XL Mar 06 '21 at 06:34

1 Answers1

0

@JsonProperty auto-generates a getter/setter that Jackson uses to read/write to the fields during serialization/deserialization. Here are some alternative approaches:

  • Provide your own public getters/setters for all fields
  • Make the fields public, generally frowned upon, but if you're creating a simple DTO, that may be acceptable.
  • Setting ObjectMapper Visibility for FIELD to ANY (see here)
  • Disable the FAIL_ON_EMPTY_BEANS exception (see here)

Given that your DTO class has getters and setters, this should work without @JsonProperty. I wasn't able to reproduce the exact error message you showed, but here are some suggestions that may help:

  • [Controller] Explicitly specify the method type as GET, either using method = GET or @GetMapping - not necessary, but it's good to be explicit
  • [Controller] Make sure you annotate the controller class with @RestController, indicating the response is serialized to JSON and wrapped in an HttpResponse object.
  • [DTO] You don't need to extend Serializable (see here).

The final controller would look like this:

@RestController
public class MyController {

  @GetMapping(value = "/admin/rest/new-subscriptions")
  public List<MyDTO> getDTO() {
    MyDTO dto = new MyDTO();
    dto.setId("54");
    dto.setName("John Doe");
    return Collections.singletonList(dto);
  }
}

Response:

[{"id":"54","name":"John Doe"}]
DV82XL
  • 5,350
  • 5
  • 30
  • 59
  • The class has getters and setters but its still throwing the exception. The only way I can get it to serialize as json is if I use the @JsonProperty annotation. – DD. Feb 27 '21 at 23:10
  • @DD. I updated my answer to point out some possible issues with your controller code. Please try it out and let me know if it works. – DV82XL Feb 28 '21 at 00:25
  • It didnt work....do I need to do something in Spring config to get Jackson to do this automatically? – DD. Feb 28 '21 at 08:50
  • @DD. I didn't need to do anything else to get it to work. Can you share the relevant parts of your POM/gradle file in your question indicating any spring/jackson libraries you're using and their versions? – DV82XL Feb 28 '21 at 13:21