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",
                    "[email protected]",
                    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?

5
  • 1
    What does is not working for me mean? Is there a compile error? Runtime error? Commented Jul 23, 2021 at 23:02
  • edited it with full detail now. Please advise Commented Jul 24, 2021 at 0:02
  • 1
    "does not come out with JSON on the local host". What url do you type in browser to call your endpoint? Are you getting any error? Do you see resoponse in network tab of browser? Commented Jul 24, 2021 at 0:10
  • I am using localhost:8080 and the list comes out just not in JSON format. Commented Jul 24, 2021 at 8:28
  • Please add the actual response, you receive, to your post. Commented Jul 24, 2021 at 11:13

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I edited it to show what I was trying to do, added the Employee class with the setters and getters. It just does not come out as a JSON. Please advise.
it works as it is. Check the edited answer. It returns a JSON array
It was both my safari and chrome browser, it didn't have JSON attached. Thank you so much for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.