0

I'm using Spring boot trying to obtain a JSON response with @RestController and @GetMapping and it does not come out with JSON on the local host. This is my code. Can anyone fix this?

@SpringBootApplication
@RestController
public class DemoApplication {
    public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
}
    @GetMapping
public List<Employee> hello () {
    return List.of(
            new Employee(
                    1L,
                    "Pedro",
                    "rt.pedrosantos@gmail.com",
                    LocalDate.of(1989, Month.JUNE, 21),
                    32
            )
    );

}

}

The Following is the "Employee" Class with setters and getters I created to go with it.

package com.example.employee;

import java.time.LocalDate;

public class Employee {
private Long id;
private String name;
private String email;
private LocalDate dob;
private Integer age;

public Employee() {
}

public Employee(Long id,
                String name,
                String email,
                LocalDate dob,
                Integer age) {
    this.id = id;
    this.name = name;
    this.email = email;
    this.dob = dob;
    this.age = age;
}

public Employee(String name,
                String email,
                LocalDate dob,
                Integer age) {
    this.name = name;
    this.email = email;
    this.dob = dob;
    this.age = age;
}

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public LocalDate getDob() {
    return dob;
}

public void setDob(LocalDate dob) {
    this.dob = dob;
}

public Integer getAge() {
    return age;
}

public void setAge(Integer age) {
    this.age = age;
}

@Override
public String toString() {
    return "Employee{" +
            "id=" + id +
            ", name='" + name + '\'' +
            ", email='" + email + '\'' +
            ", dob=" + dob +
            ", age=" + age +
            '}';
}
}

class ends here. I'm not able to properly return the code to JSON and I'm not sure why. Can anyone explain?

1 Answers1

0

Edit: It returns a Json. Check if your browser or rest client is properly configured.

enter image description here

Previous answer: Refer to this: As you have annotated with @RestController there is no need to do explicit json conversion. Since you're not returning a POJO, and according to the code you've posted you don't have any getters, changing it to the following does the job:

@RequestMapping(value = "hello_world", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
        public List<StringResponse> hello() {
            return List.of(new StringResponse("Hello"),new StringResponse("World"));
        }
    
    }
    
    class StringResponse {
        private String response;
    
        public StringResponse(String s) {
            this.response = s;
        }
    
        public String getResponse() {
            return response;
        }
    
        public void setResponse(String response) {
            this.response = response;
        }
    }

or use a library like Gson: There is a clean way to return string as json in a Spring Web API?

Example:

  @RequestMapping(value = "hello_world", method = RequestMethod.GET,   produces = MediaType.APPLICATION_JSON_VALUE)
    public List<String> hello() {

        Gson gson = new GsonBuilder().create();
        Type type = new TypeToken<List<String>>() {}.getType();
        String json =  "[ \"Hello\", \"World\"]";
        List<String> responseList = gson.fromJson(json, type);
        
        return responseList;
    }

More info: Spring MVC - How to return simple String as JSON in Rest Controller

How to return JSON data from spring Controller using @ResponseBody

Not sure what you're trying to do, but creating a model class with corresponding setters / getters should be the way.

moondev
  • 151
  • 1
  • 4