1

I have two classes PlaylistResource which has a method createPlaylist which takes an object of type PlaylistRequest. I want to create a POST request on localhost:9999/playlists I am using Postman and I am not sure how to pass the object of PlaylistRequest which is request to the method createPlaylist.

@XmlType(propOrder= {"title", "artistSeeds", "numberOfSongs"})
@XmlAccessorType(XmlAccessType.FIELD)
public class PlaylistRequest {

    private String title = "";
    @XmlElement(name = "seeds")
    private List<String> artistSeeds;
    @XmlElement (name = "size")
    private int numberOfSongs = 0;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public List<String> getArtistSeeds() {
        return artistSeeds;
    }

    public void setArtistSeeds(List<String> artistSeeds) {
        this.artistSeeds = artistSeeds;
    }

    public int getNumberOfSongs() {
        return numberOfSongs;
    }

    public void setNumberOfSongs(int numberOfSongs) {
        this.numberOfSongs = numberOfSongs;
    }
}

The other class:

@Path("playlists")
public class PlaylistResource implements PlaylistApi {

    @Override
    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Response createPlaylist(PlaylistRequest request) {

        if(request == null) {
            System.out.println("Was here");
            throw new ClientRequestException(new ErrorMessage("no parameter passed."));
        }

        try {
            List<Song> playList = new ArrayList<>();
            List<Song> songs = new ArrayList<>();
            List<String> artistsIds = new ArrayList<>();
            ArtistResource artistsResources = new ArtistResource();
            int playlistDefaultSize = 10;

            int i = 0;
            do {
                playList.add(songs.get(i));
                i++;
            }while( i < playlistDefaultSize);

            Playlist playlist = new Playlist();
            playlist.setTitle(request.getTitle());
            playlist.setSize(songs.size());
            playlist.setTracks(playList);

            return Response.status(Response.Status.CREATED.getStatusCode()).entity(playlist).build();
        } catch (Exception e) {
            throw new RemoteApiException(new ErrorMessage(e.getMessage()));
        }
    }

}

deanavenger
  • 582
  • 1
  • 7
  • 24
  • If I understood correctly, You mean you want to know how to test post method with raw payload in the postman? Correct ? If yes, You can watch this video : https://www.youtube.com/watch?v=eYQyqf-DtCQ or It has clear explanation here : https://www.toolsqa.com/postman/post-request-in-postman/ – Gautam Jun 11 '19 at 09:37
  • Possible duplicate of [How to send post request to the below post method using postman rest client](https://stackoverflow.com/questions/29364862/how-to-send-post-request-to-the-below-post-method-using-postman-rest-client) – Gautam Jun 11 '19 at 10:32

2 Answers2

0

Simply add a JSON object in Body of request, select Raw Json in Postman and paste the following object:

NOTE: Add a key Content-Type and set its value to application/json in Header of Request

{
  "title": "Some title",
  "artistSeeds": [
    "string1",
    "string2"
  ],
  "numberOfSongs": 0
}

HEADER Header BODY Body

Mustahsan
  • 3,852
  • 1
  • 18
  • 34
0

Change this parameter from data class to string,

public Response createPlaylist(PlaylistRequest request) {

to

public Response createPlaylist(String request) {

then convert it using GSON into your data class.

PlaylistRequest request = new Gson().fromJson(request, new TypeToken<PlaylistRequest >(){}.getType());
madqori
  • 37
  • 8