user630209
user630209

Reputation: 1207

DTO Mapping for RESt service of type GET

Developing a REST service in Spring BOOT application.

This is the input json

{
    "rcode": 122,
    "ecode": [11, 12]
}

Controller code

 @RequestMapping("/getPersonDTOList")
    public List < PersonDTO > getPersonDTOList(
        @RequestParam(value = "personDTO") String personDTO){

    //how can I map this to DTO

        }

//DTO

 public class PersonDTO {
        private Int rcode;
        private List<Int> ecode;

        }

How can I map this string to DTO in spring BOOT, Since this is GET we need to do it manually.

Upvotes: 0

Views: 1446

Answers (2)

ville
ville

Reputation: 57

I am not so experiensed but if you use @RequestMapping I think you have to set a type also:

 @RequestMapping("/getPersonDTOList", method = RequestMethod.POST ) // or GET...

You can also use @PostMapping , @GetMapping, etc.

@PostMapping("/getPersonDTOList")
public PersonDTO getPersonDTOList(@RequestBody PersonDTO person){ 
     return person;
}

I think this is only valid only with @RestController

Upvotes: 0

Mạnh Quyết Nguyễn
Mạnh Quyết Nguyễn

Reputation: 18235

The GET method does not carry any body data.

You either:

  • Change your API to POST method (which is supported by default with @RequestMapping

    @RequestMapping("/getPersonDTOList")
    public List < PersonDTO > getPersonDTOList(@RequestBody PersonDTO person)
    
  • Change your API to accept @RequestParameter instead of @RequestBody:

    @RequestMapping("/getPersonDTOList")
    public List < PersonDTO > getPersonDTOList(@RequestParam("rcode") int rcode, @RequestParam("ecode") List<Integer> ecode )
    

Upvotes: 1

Related Questions