1

I've been searching around here for a solution , but I found nothing useful for my case.

My Dao needs a String[] and a single String, so I did this:

@RequestMapping(value = "/add", method = RequestMethod.POST, consumes = { "application/json" })
public void newRent(@RequestBody String[] isbn,String username) {
    rentService.newRent(isbn, username);
}

Now, I'm trying to do a POST from Postman calling the mapped link, but I keep getting method not allowed (405).

I tried a lot, this looks the best ways to do it, but still doesn't work.

[
 { {   "isbn":"123"},{"isbn":"1234"},
 { "username" : "zappa"}
]

or

{
  "isbn": ["123", "1234"],
  "username": "zappa"
}

Am I missing something? cant figure it out!

lealceldeiro
  • 14,342
  • 6
  • 49
  • 80
Zappa
  • 25
  • 8

2 Answers2

2

You have to create a new entity Rent

public class Rent{public string[] isbn; public string username;}

Then you change your method to:

 @RequestMapping(value = "/add", method = RequestMethod.POST, consumes = { "application/json" })
public void newRent(@RequestBody Rent rentRequest) {
    rentService.newRent(rentRequest.isbn, rentRequest.username);
}
lealceldeiro
  • 14,342
  • 6
  • 49
  • 80
Anass TIssir
  • 240
  • 2
  • 7
0

First off, this is the correct JSON (the other one is incorrect, check it here):

{
  "isbn": ["123", "1234"],
  "username": "zappa"
}

Now, in order to get those values you need to use @RequestBody along with some POJO, JavaBean or Map, in order to get the values correctly. For example with a Map it would be like this:

@RequestMapping(value = "/add", method = RequestMethod.POST, consumes = { "application/json" })
public void newRent(@RequestBody Map data) {
    rentService.newRent((String [])data.get("isbn"), data.get("username").toString());
}

With a POJO, it would be something like this:

public class RentEntity {
    private String[] isbn;
    private String username;

    public String[] getIsbn() {
        return isbn;
    }

    public void setIsbn(String[] isbn) {
        this.isbn = isbn;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }
}

@RequestMapping(value = "/add", method = RequestMethod.POST, consumes = { "application/json" })
public void newRent(@RequestBody RentEntity data) {
    rentService.newRent(data.getIsbn(), data.getUsername());
}

Additional info

lealceldeiro
  • 14,342
  • 6
  • 49
  • 80