0

I have developed 1-REST web services with help of Spring-Boot technology.

Now, while i am going to requesting any thing it isn't responding me into JSON format ? in stead of it it's responding into plain "String" format.

Also, note i have used annotation @RestController at Controller class level.

Some how i am thinking some configuration i am missing. is it so ?

My current Maven Configuration is ,

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.6.RELEASE</version>
</parent>

Also, i have noticed that while i am requesting(POST) for getting List then in such case it's returns an array of JSON object.

Can anyone guide me what's wrong with me ?

Vishal Gajera
  • 4,137
  • 5
  • 28
  • 55

1 Answers1

0

if i understand you right, you want to produce a JSON object in response ?

you have an option in your @RequestMapping that produces a specific format of response.

@RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)

Also you can use a specific format of response to add your object to return, and type of response with ResponseEntity<?>

something similar to this:

public ResponseEntity<?> getAll() {
    List<Category> categories = categoryDAO.getAll();
    if (!categories.isEmpty()) {
        return new ResponseEntity<>(categories, HttpStatus.OK);
    } else {
        return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    }
}

EDIT

i add the annotations for @PostMapping it's similar to @RequestMapping

@PostMapping(value = "/list", produces = "application/json; charset=UTF-8")

or also is valid:

@PostMapping(value = "/list", produces = MediaType.APPLICATION_JSON_VALUE)

i'll leave you a link so you can see what properties had @PostMapping

@PostMapping

Paulo Galdo Sandoval
  • 2,173
  • 6
  • 27
  • 44