sorry, didn't check this forum for a bit.
here is the 'full code' for a minimal servlet (plus a fair bit of extra printlns)
the other thing you need to do is configure web.xml (the servlet configuration)
with the above web.xml declaration you then say RestDataSource.setDataURL("/myrestyapi/");
here is the 'full code' for a minimal servlet (plus a fair bit of extra printlns)
Code:
package com.sample.server;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class RestDataSourceServlet extends HttpServlet {
private Gson gson;
@Override
public void init() throws ServletException {
gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss")
.create();
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
System.out.println("doGet " + req.getRequestURI());
for (Object key : req.getParameterMap().keySet()) {
System.out.println(key + " = "
+ Arrays.toString(req.getParameterValues((String) key)));
}
try {
List<?> list = processRequest(req);
String string = gson
.toJson(new ResponseWrapper(new Response(list)));
// String string = gson.toJson(list);
System.out.println(string);
resp.getOutputStream().print(string);
} catch (Exception e) {
e.printStackTrace();
e.getMessage());
}
}
private List<?> processRequest(HttpServletRequest req) {
String operationType = req.getParameter("_operationType");
String dataSource = req.getParameter("_dataSource");
//YOUR CODE TO PERFORM THE REQUEST GOES HERE
return <-- A LIST OF RESULTS -->
}
}
the other thing you need to do is configure web.xml (the servlet configuration)
Code:
<servlet> <servlet-name>RESTapi</servlet-name> <servlet-class>com.sample.server.RestDataSourceServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>RESTapi</servlet-name> <url-pattern>/myrestyapi/*</url-pattern> </servlet-mapping>
with the above web.xml declaration you then say RestDataSource.setDataURL("/myrestyapi/");
Comment