Potatosaurus Potatosaurus - 1 month ago 16
Java Question

Building a Rest Server

I've learned how to send and handle Rest API requests, so am reasonably comfortable with how an outside user would submit requests to an API through methods such as opening a connection to the API, setting a request method and processing returned messages.

However the time has come to create my own Rest Web Service, and I'm a bit stuck as I'm not entirely familiar with just how the server will handle the communication.

I can set up a connection just fine to allow for communication, but I'm not particularly comfortable with the specifics of a connection.

Below is the method where I initialise the server, which will then wait until something connects and have a brief conversation until the client disconnects.

public void Initialise(){
try {
ServerSocket s = new ServerSocket(21); //TODO: Change port?
while(true){
Socket incoming = s.accept();
Runnable r = new ConnectionManager(incoming);
Thread t = new Thread(r);
t.start();
}
}
catch(IOException e){
e.printStackTrace();
}
}


And the class handling the connection.

public class ConnectionManager implements Runnable{
private Socket incoming;

public ConnectionManager(Socket i){
this.incoming = i;
}

@Override
public void run() {
try{
try{
//Initialise IOStreams
InputStream inStream = incoming.getInputStream();
OutputStream outStream = incoming.getOutputStream();

Scanner in = new Scanner(inStream);
PrintWriter out = new PrintWriter(outStream, true);

out.println("TODO: Change this message. Press Q to quit.");

boolean done = false;
while (!done && in.hasNextLine()){
String line = in.nextLine();
out.println("Echo: " +line);
if (line.trim().equals("Q")){
done = true;
}
}
}
finally{
incoming.close();
}
}
catch (IOException e){
e.printStackTrace();
}
}


}

What I'm not sure of how to do, is how I start building an application that listens for Rest requests, and then processes them accordingly. I feel confident that upon examining the connection, I'd be able to engineer functionality to process the API request, however I do not know how to view the specifics of a Rest request sent by a user.

For example, if a user were to send a request, such as

GET exampleAPI/endpoint?params


How do I examine the request, and then subsequently process it accordingly?

For example, on the client side when you initiate the request, an approach such as the use of HttpURLConnection would allow you to follow the process of

HttpURLConnection c = (HttpURLConnection) targetURL.openConnection();
c.setRequestMethod("GET");


Which would allow you to submit a Get request to the target URI.

Looking through the Java Documentation for Socket, I don't quite know how I can view incoming requests and active connections.

Am I missing something, or just doing things completely wrong?

Answer

As I mentioned in my comments, you can use Jersey/Spring/etc.. for this rather than you rewriting the logic for whole server and handling multiple threads, etc..

Also, you should know that there is a JDK API called JAX-RS (specification plus implementation) for the same. you can look here

You can look here for Spring REST Controller or here for Jersey.

I recommend you go through JDK JAX-RS API first, then you may have a look at Spring and Jersey.

There are other vendors implementing JAX-RS, which you may be interested in, you can look here for comparison for JAX-RS implementations.

P.S.: You should also know that Spring and Jersey do not compliant to JAX-RS API, rather they have got their own API.