• Post Reply Bookmark Topic Watch Topic
  • New Topic

Handling POST request in the REST webservice

 
Yogs Kumar
Greenhorn
Posts: 23
Eclipse IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everyone,

I am trying to write a simple REST service which writes a new team of players into a text file every time the client is executed.The client is sending the POST request as follows:
private void sendPostRequest() {
byte [] team_to_add;
Player hibbert = new Player("Hibbert", "Roy Hibbert");
Player paul = new Player("Paul", "Paul George");
Player west = new Player("West", "David West");
List<Player> mb = new ArrayList<Player>();
mb.add(hibbert);
mb.add(paul);
mb.add(west);
Team Pacers = new Team("Indiana_Pacers",mb);
// TODO Auto-generated method stub
XMLEncoder enc;
try {
enc = new XMLEncoder(new BufferedOutputStream(new FileOutputStream("team_to_add.txt")));
enc.writeObject(Pacers);
enc.close();
}
catch(Exception e)
{

}
HttpURLConnection PostConn = get_connection(endpoint,"POST"); //method defined in the class to get a HTTP connection.
System.out.println(PostConn.getRequestMethod());
try {
PostConn.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(PostConn.getOutputStream());
String cwd = System.getProperty ("user.dir");
String sep = System.getProperty ("file.separator");
int len = (int) new File(cwd+sep+"team_to_add.txt").length();
team_to_add=new byte[len];
new FileInputStream(cwd+sep+"team_to_add.txt").read(team_to_add);
wr.write(team_to_add);
wr.flush();
wr.close();
System.out.println(PostConn.getResponseCode());

In the REST service I am obtaining the Message Context object.

Can someone help me to figure out how i can get access to the body of the POST message being sent from the client.

Thanks in advance
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My first question would be: why are you not using a REST framework like Jersey? Implementing all those low-level parts is a lot of work that other folks have already done for you.
 
Yogs Kumar
Greenhorn
Posts: 23
Eclipse IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:My first question would be: why are you not using a REST framework like Jersey? Implementing all those low-level parts is a lot of work that other folks have already done for you.


Hello Ulf,

I am working through the examples in Web services up and running and this is the second example in that book where all oher CRUD operations are implemented.So i am still not using any of the frameworks you mentioned.

Thinking from a production scenario,will all applications use one or the other framework of REST services?.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't think of a good reason why they wouldn't. Those frameworks do so much for you, and are so easy to use, that it'd be a waste of time to reinvent the wheel, IMO.
 
Yogs Kumar
Greenhorn
Posts: 23
Eclipse IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:I can't think of a good reason why they wouldn't. Those frameworks do so much for you, and are so easy to use, that it'd be a waste of time to reinvent the wheel, IMO.


Hello Ulf, thanks for the response, coming back to my original question, can you please provide me some pointers on how i can handle this POST request
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!