About Creating Web APIs in java

Asked 2 years ago, Updated 2 years ago, 32 views

I want to create a WebAPI in java

I am a beginner in java.I am studying creating web applications using java.
"After reading the book ""Introduction to Servlet & JSP"", I was able to create a simple web app using Tomcat."

Now I'm thinking of creating a WebAPI, but when I looked it up online, I found out that I used a library called Jersey. There are many samples.

I have a question, but in my perception, the image of returning Json instead of HTML was WebAPI.
So I feel that I can make a WebAPI with Tomcat+sarvlet, but why do I use jersey?

Also, I would like to create a simple web application that can be used internally for my business.
Should we use some kind of framework?
(It does not appear to have been used in previous systems.)

java

2022-09-30 19:11

2 Answers

You can create WebAPI with Tomcat and Servlet alone.However, Jersey makes it easier and more standard.

Jersey is a Java OSS framework dedicated to delivering RESTful Web services.Common reasons for using the framework include:

·Reduce development man-hours
·Improve productivity and serviceability
·Prevent design level defects
·Homogenization of deliverables

Common logic is implemented in the framework, so these can be achieved.Here is a sample of a RESTful web service class created using Jersey on this page.If you made this without Jersey, you can see that it's impossible to do it with such a short number of lines.Also, since Jersey implements JAX-RS, one of the standard Java EE specifications, the WebAPI developed using Jersey is not proprietary, but will be of a more standard specification.

However, the larger the development, the more effective the framework is, so if it's a "simple web application to use in-house for business", I think it's an option not to use Jersey.

[FYI]
Incidentally, in Tomcat 8.0, the following checks have been added, so the DELETE method for JSP is an error.Be careful if you're thinking of using JSP instead of servlets.

http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/jasper/servlet/JspServlet.java?view=diff&r1=1497877&r2=1497878&pathrev=1497878

This check was added because of #4 of the following specifications added in the JSP 2.3 specification:

https://jcp.org/aboutJava/communityprocess/maintenance/jsr245/245-MR3.html


2022-09-30 19:11

The exact and official answers should be found on Jersey site's front page "About".

I'll try to explain it from a different perspective and a little bit more specific.

Jersey calls itself a framework, but I think it's easy to understand that it's an extension that makes servlet more convenient than a framework.
Even if you use Jersey to develop a web API, you will still be creating a web API with Tomcat+Servlet.

For example, if you consider providing identity-based search for user information such as /user/[id],

public class User{

    private long id;
    private String name;

    // getter, setter, ...
}

If you implement it with the original servlet, it will look like this:

@WebServlet("/servlet/user/*")
public class UserServlet extensions HttpServlet {

    @ Override
    protected void doGet(HttpServletRequest req, HttpServletResponse response)rowsServletException, IOException {

        long id = Long.parseLong(req.getPathInfo().split("/")[1]);

        User search using User user=//id

        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json");
        resp.getWriter().append(toJsonStr(user)) .close();
    }

    private String to JsonStr (User user) {
        return new StringBuilder()
                .append("{")
                .append("\"id\":").append(user.getId())
                .append(", ")
                .append("\"name\":\"")
                .append(user.getName()) .append("\"")
                .append("}").toString();
    }
}

If you incorporate a feature extension called Jersey, it looks like this:

@Path("/user/{id}")
public class UserResource {

    @ GET
    @Produce(MediaType.APPLICATION_JSON)
    publicUseruser(@PathParam("id")longid){

        User search using User user=//id

        return user;
    }
}

This is an example, but

If you look it up online, you can see many samples using a library called Jersey

The reason for this is that many sample makers thought it would be better/easy to use Jersey to write like the latter, although it could be done using the original servlet like the former.

Should I use some kind of framework?

Generally speaking, whether or not a framework should be used is determined by the balance between the advantages of the functions provided by the framework (for me) and the disadvantages such as learning costs and costs.
I think the sample makers are introducing Jersey because they think that it is OSS and the learning cost is relatively low.

Supplemental:
Jersey is not the framework in the nofollow noreferrer">narrow meaning (Jersey does not provide control inversion).
If you're too obsessed with the word framework at this stage, it might be a hindrance to your understanding.


2022-09-30 19:11

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.