问题描述:

I've been writing for a long time a Java Rest application and I want to send user name and file context using Post method.

I have two methods in Client's code:

public void postUser(String user)

public void postFile(File myFile)

These two methods send string.

And my Server's post method, which receive string from postUser method:

@Post

public void userPost(String name) {

User.getInstance().addUser(name);

}

I want to receive string from postFile method and save it to diffrent varaible than in userPost method. What is the best way to do that?

网友答案:

Do I understand it correctly? You want to invoke something like...

postUser(user);
postFile(myFile);

and in the client, to process the two different requests in some stateful manner(the receiver is supposed to know that ).

@Post                                       
public void userPost(String name) {
User.getInstance().addUser(name);
}

@Post                                       
public void filePost(File file) {
//processing the file, ONLY IF the userPost method is already invoked
}

Instead, can you encapsulate the two requests in one(one request with two parameters). Something like

 postUserFile(user,file);

And in the request reciever...

@Post                                       
public void nameFilePost(String name, File file) {
  userPost(name);
  filePost(file)
}

I don't recognize the framework which you are using, but in JAX-RS the method will looks something like this

@PATH("/yourPath/")
public class YourClassNameHere{

@POST                                    
public void nameFilePost(@QueryParam("name") String name, @QueryParam("file") File file) {
  userPost(name);
  filePost(file)
  }
}

One last thing: Don't use Singletons, they are global variables, and therefore bad. Why global variables are bad.

相关阅读:
Top