我只想知道,如何發送JSON對象到createTrackInJSON(Track track)
方法,使用@Post
註釋通過postman rest客戶端。 在這裏,如何通過@Post註釋將JSON對象傳遞給createTrackInJSON(Track track)方法?如何使用postman rest客戶端發送post請求到下面的post方法
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import com.mkyong.Track;
@Path("/json/metallica")
public class JSONService {
@GET
@Path("/get")
@Produces(MediaType.APPLICATION_JSON)
public Track getTrackInJSON() {
Track track = new Track();
track.setTitle("Enter Sandman");
track.setSinger("Metallica");
System.out.println("inside get method . . .");
return track;
}
@POST
@Path("/post")
@Consumes(MediaType.APPLICATION_JSON)
public Response createTrackInJSON(Track track) {
System.out.println("inside post method . .");
String result = "Track saved : " + track;
return Response.status(201).entity(result).build();
}
}
//Track class is:
public class Track {
String title;
String singer;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getSinger() {
return singer;
}
public void setSinger(String singer) {
this.singer = singer;
}
@Override
public String toString() {
return "Track [title=" + title + ", singer=" + singer + "]";
}
}
如何發送,如果我有多個參數? – Raghu 2017-12-06 05:28:33