1
votes

I am trying to return a a list of objects to the /data endpoint. Below is the code for the rest controller:

@RestController
public class ApiController {
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String home() {
        return "<h1>Welcome to Spring REST API</h1>";
    }

    @RequestMapping(value = "/data", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody List<UserData> data() {
        return new UserService().getData();
    }

}

Code for the service:

public class UserService {

    private List<UserData> data;

    public UserService() {
        this.data = new ArrayList<UserData>();
        this.data.add(new UserData(1, "Leanne Graham", "bret", "[email protected]", "Romaguera-Crona", "hildegard.org",
                "1-770-736-8031 x56442"));
        this.data.add(new UserData(2, "Ervin Howell", "Antonette", "[email protected]", "Deckow-Crist", "anastasia.net",
                "010-692-6593 x09125"));
        this.data.add(new UserData(3, "Clementine Bauch", "Samantha", "[email protected]", "Romaguera-Jacobson",
                "ramiro.info", "1-463-123-4447"));
        this.data.add(new UserData(4, "Patricia Lebsack", "Karianne", "[email protected]", "Robel-Corkery",
                "kale.biz", "1-770-736-8037"));
        this.data.add(new UserData(5, "Chelsey Dietrich", "Kamren", "[email protected]", "Keebler LLC",
                "demarco.info", "1-344-736-8031 x564"));
    }

    /**
     * @return the data
     */
    public List<UserData> getData() {
        return data;
    }

    /**
     * @param data the data to set
     */
    public void setData(List<UserData> data) {
        this.data = data;
    }

}

UserData code:

public class UserData {

    int id;
    String name, username, email, company, website, phone;

    public UserData(int id, String name, String username, String email, String company, String website, String phone) {
        this.id = id;
        this.name = name;
        this.username = username;
        this.email = email;
        this.company = company;
        this.website = website;
        this.phone = phone;
    }

}

The UserData class is used to represent individual data to be passed in the list.

The following error and exceptions are thrown:

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class com.example.springrest.UserData]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class com.example.springrest.UserData and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: java.util.ArrayList[0])] with root cause

1
can you add your UserData code and which serializer do you use?Sergei Podlipaev
make sure all your fields in UserData has proper getters.Jimmy
I have used no serializer... I am a beginner to Spring. Whatis a serializer.. I am adding the UserData code.Shivam Kumar
Thanks @Jimmy... Adding getters and setters worked.Shivam Kumar
And also please use @Autowirefor dependency injection, don't create objects outside of contextDeadpool

1 Answers

1
votes

try this

Code for controller

@RestController
public class ApiController {

    @AutoWired
    UserService userservice;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String home() {
        return "<h1>Welcome to Spring REST API</h1>";
    }

    @RequestMapping(value = "/data", method = RequestMethod.GET, produces = produces = MediaType.APPLICATION_JSON_VALUE)
    public List<UserData> data() {
        return new userservice.getData();
    }

}

Code for service

@Service
public class UserService {

    private List<UserData> data;

    public UserService() {
        this.data = new ArrayList<UserData>();
        this.data.add(new UserData(1, "Leanne Graham", "bret", "[email protected]", "Romaguera-Crona", "hildegard.org",
                "1-770-736-8031 x56442"));
        this.data.add(new UserData(2, "Ervin Howell", "Antonette", "[email protected]", "Deckow-Crist", "anastasia.net",
                "010-692-6593 x09125"));
        this.data.add(new UserData(3, "Clementine Bauch", "Samantha", "[email protected]", "Romaguera-Jacobson",
                "ramiro.info", "1-463-123-4447"));
        this.data.add(new UserData(4, "Patricia Lebsack", "Karianne", "[email protected]", "Robel-Corkery",
                "kale.biz", "1-770-736-8037"));
        this.data.add(new UserData(5, "Chelsey Dietrich", "Kamren", "[email protected]", "Keebler LLC",
                "demarco.info", "1-344-736-8031 x564"));
    }

    /**
     * @return the data
     */
    public List<UserData> getData() {
        return data;
    }

    /**
     * @param data the data to set
     */
    public void setData(List<UserData> data) {
        this.data = data;
    }

}