I have an application that is using JAX-RS (and swagger) annotations heavily and all is shown nicely in the swagger definition.
However one servlet implements a customer restful interface (oData, Apache oLingo) and this is not using any of the JAX-RS annotations. Yet I would like to document it.
To make it as simple as possible, let's say I have a class
#WebServlet("/rest1")
public class Login extends HttpServlet {
#Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
response.setHeader("Content-Type", "application/json");
PrintWriter out = resp.getWriter();
out.print("{ \"text\": \"Hello World\" }");
}
}
I would like that servlet to show up in the swagger output as well without losing the JAX-RS entities.
According to the docs I would think I need to extend the reader to output this servlet as well and a custom ModelConverter?? to add hardcoded properties. But even if that is correct, I can't find a good starting point. Not to mention that I hope there is a simpler, more direct, way. Samples of swagger have been of no help either.
Related
I am trying to develop a RESTful app with Spring. The REST service must be parametrized in a database, I mean, a generic Service that can change the whole URL from a database info doing the same work but pointing to differents URL directions.
I was searching for info related for ages. Does anyone know about useful tutorial?
is it possible to do?
Thanks everyone!
You're better off creating a simple Servlet that will listen to a static root url and respond dynamically according to the database value.
public class Config {
public static String restPath = "valueReadFromDB";
}
#WebServlet("/appName")
public class AppServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp) {
if (req.getURI().contains(Config.restPath) {
// add your logic
}
}
}
You can call this like so: http://your.host.name/appName/dynamicUrlReadFromDB
Don't blindly attempt to use Spring just because it is cool or fashionable. Sticking to the basics can always yield excellent results and allows for fine-grained control of your application something that Spring cannot always do.
How to use Servlet in creating a RESTful web service without using any JAX-RS implementation (Jersey, etc)?
Basically you absolutely right, you don't need a framework in order to implement REST API.
For instance, you could do basic crud operations in simple servlet class, like this:
#WebServlet(urlPatterns = "/book/*")
public class BookServlet extends HttpServlet {
#Override
public void doGet(HttpServletRequest request, HttpServletResponse response) {
// fetch from db
}
#Override
public void doPost(HttpServletRequest request, HttpServletResponse response) {
//update
}
#Override
public void doDelete(HttpServletRequest request, HttpServletResponse responce) {
//delete
}
}
It's a little bit inconvenient since you need to manually parse url params, do serialization, but under the hood, JAXRS and Spring MVC is just a servlets!
So, if you don't want dependencies in your code, I could suggest to just implement some convenient wrappers over servlet api.
Tip: you could parse path params from request like this:
String info = request.getPathInfo();
String[] parts = pathInfo.split("/");
String param1 = pathInfo[0];
So, for instance, if you have request like this:
HTTP GET /book/{id}
You'll get {id} in param1 which can be later used in database lookup.
Is there any way to use pure Java servlets not spring mvc request mapping to map a URL to a method?
something like:
#GET(/path/of/{id})
It's also possible with "plain vanilla" servlets (heck, Spring MVC and JAX-RS are also built on top of servlet API), it only requires a little bit more boilerplate.
#WebServlet("/path/of/*")
public class PathOfServlet extends HttpServlet {
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String id = request.getPathInfo().substring(1);
// ...
}
}
That's all. Thanks to the new Servlet 3.0 #WebServlet annotation, you don't need any web.xml entry.
See also:
Our Servlets wiki page
We have a #WebServlet that's annotated with a custom interceptor annotation like this:
#WebServlet("/path")
#CustomInterceptor
public class InitialHtmlServlet extends HttpServlet
{
#Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
}
}
We have the CustomInterceptor in the beans.xml in /WEB-INF and the interceptor works in other CDI-components. In this servlet, however, we cannot get it working.
We're running the latest JBoss EAP, which should be somewhat similar to JBoss 7.1.1. Is there something we should do different to get the interceptor to catch invocations on the servlet or is this not possible at all?
After some digging around I also found it somewhat confusing, that while being a good candidate for calling it a 'bean', servlet are exempt from interceptor mechanism.
It looks like various parts of JEE6 may or may not support interceptors at will :). Found some discussion here.
I am a newbie at java/java servlet. I need the simpleCaptcha for a form, using html and javaservlet for the code. With reference to http://simplecaptcha.sourceforge.net/extending.html.
Captcha captcha = new Captcha.Builder(200, 50)
.addText()
.addBackground()
.addNoise()
.gimp()
.addBorder()
.build(); // Required.
Where (java servlet) should I put this piece of code in order to create the captcha on html?
Thank you very much.
To extend SimpleCaptcha and customize your CAPTCHA, my understanding is that you'll have to create your own HttpServlet (maybe extends SimpleCaptchaServlet). To do so, I suggest to download the source code and to look at SimpleCaptchaServlet or StickyCaptchaServlet. This is what the doGet() method of SimpleCaptchaServlet looks like:
#Override
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
Captcha captcha = new Captcha.Builder(_width, _height)
.addText()
.addBackground(new GradiatedBackgroundProducer())
.gimp()
.addNoise()
.addBorder()
.build();
CaptchaServletUtil.writeImage(resp, captcha.getImage());
req.getSession().setAttribute(NAME, captcha);
}
This should be self-explaining: create your own servlet and put your custom Captcha Builder code in the doGet() method. Then, follow the instructions of the Installing section but, instead of using one of their servlet, declare yours in the web.xml. Finally, package/deploy your application. An example is bundled in the source distribution under examples. Check it out if you need more guidance about the structure, the dependencies and the packaging of your web application.
hey have you had a look at this page yet?
http://simplecaptcha.sourceforge.net/installing.html
this is about as straight forward as it gets I think. This project gives you a few Capcha servlets out of the box. You just have to map them in your web.xml file. You can follow along and create the jsp that will call them.