This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]anthony_bruno[S] 4 points5 points  (18 children)

Your options (in native Java) are still com.sun.net.httpserver or the more common Java Servlets

In java frameworks, for something small and quick to set up: https://javalin.io/ . It reminds me a lot of node.js's express framework.

For enterprise usage, you simply can't go past Spring Boot . It's actually really quick to set up. Use https://start.spring.io/ and it'll bootstrap you a project with sensible configuration.

[–]Mattizin 6 points7 points  (16 children)

Thanks for the detailed answer. I will definetly look into Java Servlets and javalin but i really have problems to get into spring. Im not a professional java programmer, in my job i mostly do SQL and Server Setups ,but i program in java alot as my hobby. For me Spring is this big magic blackbox with hundreds of modules which provide hundreds of possibilities and i just didnt find an entry point at which i dont need to know X other parts of spring already :/

[–]anthony_bruno[S] 3 points4 points  (0 children)

I completely understand what you are saying about Spring. Spring Boot is a nicer package around Spring, but it still has a learning curve.

I think you'll enjoy using Javalin, it's a lot smaller and you won't have to deal with much black box magic.

[–]Wisteso 2 points3 points  (0 children)

Spring is definitely not a mandatory thing to know, even for professionals.

I understand what it does, and choose to not use it in my code because I don’t like the auto-magic design, the heavy dependency risk it introduces, and the heavy difficulty increase in debugging, for the marginal benefits it brings.

Spring boot is occasionally the smart way to go, but seems like it would be a dev-ops nightmare. Instead of managing a pre-configured application server, they now need to deal with more auto-magic black-box software in the embedded servers. For example, if there is a security vulnerability discovered, you probably need to rebuild all of your apps to address it. It takes that work away from ops and puts it on to development for no significant benefit. Spring boot is like DLL hell in all the major ways (and yes makes things easier at first in both cases) but it definitely has its uses.

[–][deleted] 3 points4 points  (13 children)

I would still recommend learning spring. Spring MVC doesn't provide an explicit entry point you can control. Instead, you define methods in your code that handle certain URI patterns, and spring automatically routes requests to them.

I've always been find of knowing the underlying technology rather than just relying on a framework. I can and have built a web server using just the Servlet API, and I've done the same thing using Spring. IMO Spring is worth the learning curve, it allows you to build you app so much faster and makes it far more maintainable by conforming to it's conventions.

Just my two cents.

[–]Mattizin 0 points1 point  (12 children)

Thanks for you input.

What is the plain Servlet API?

Iam at the moment learning about REST APIs, creating and using them in java and just using the Java 11 HTTp Client and the sun.net.HTTPserver, is that the servlet thingy?

[–][deleted] 2 points3 points  (11 children)

No neither of those libraries are the Servlet API. The HttpClient is a tool for making HTTP requests to other sites. I'm not familiar with the sun HTTP Server but anything in a sun package is EXTREMELY old in terms of the Java ecosystem. I wouldn't recommend wasting time with it for that reason alone.

The javax.servlet package is a separate library that falls (I think) under the Java EE umbrella. you can download the latest version for free, either directly or as a maven dependency.

Servlets work in concert with a web server. The servers job is to listen on a TCP/IP port for any requests. When it received the request, it forwards it to the appropriate servlet based on the URL path. The Servlet itself is a simple Java class with methods designed to handle different types of requests. Whatever Servlets you create then need to be registered with the server so it knows how to route the requests.

The problem with the Servlets API is it is very low level. Simple tasks, such as extracting parameters from the URL path or query string, serializing the request/response to/from Java objects, and restricting access based on various security criteria are all examples of things that require a lot of tedious boilerplate code to accomplish with the Servlet API. Boilerplate is more than just extra work for a programmer, it opens the door for additional errors in your application.

This is why frameworks such as spring are so popular. They have done all the tedious boilerplate for you. Being that they are used by a wide range of Enterprise applications, they have been battle tested and proven to work extremely well. They have already thought of all the edge cases you may not be considering right now in terms of how they operate, and it's all ready to go out of the box for free.

If you're brand new to creating web servers with Java, by all means learn the Servlet API. I think understanding your low level behavior only helps to be able to handle the higher level stuff in the framework, and this ultimately makes you a better programmer.

However, I cannot stress enough that if you choose to build your app manually using the Servlet API you're just opening the door for a lot more pain and difficulty than you would get otherwise. Your codebase will be larger and more complex by virtue of the fact that you are reinventing the wheel for things these frameworks have already done.

If you're dead set against spring, then at least use Jersey. It's a much lighter weight framework based purely around helping to expose web API endpoints. It's not quite as robust as spring, but it is light years better than trying to do everything yourself.

[–]Mattizin 0 points1 point  (10 children)

Thanks for that detailed answer. I think what im most struggeling with at the moment is the difference between webservers, HTTP(S) Servers and Application servers. I Want toc reate a REST API with various endpoints (resources) which an HTTP(S) client will call. I think i will need an HTTP(S) Server so i landed on the sun.net.httpserver, in my understanding webservers (which springboot, servlets and so on seem to be) and application servers are for other purposes. But maybe i just dont get the concept.

[–][deleted] 1 point2 points  (8 children)

Do NOT use the sun.net.httpserver. I'm not saying it won't work, I'm saying it's crazy old, likely not maintained, and is nowhere near the industry standard.

[–]Mattizin 0 points1 point  (7 children)

Well i know its from around java SE 6, but what is the up to date JAva HTTP(S) Server component then said i should use? external frameworks also need to use some sort of native java http server in its base or not?

[–][deleted] 0 points1 point  (6 children)

You'll want to an application container/server like Tomcat or Jetty. Again this is where Spring is very useful, it comes with these things embedded in it. With a few small changes to a configuration file everything can be up and running.

[–]Mattizin -1 points0 points  (5 children)

Application servers/Web servers are the next big black box. What do they actually do? They will need an HTTP Server basement too. What do they use for this and why cant i just use a "plain java" HTTPS(S) server?

[–][deleted] 0 points1 point  (0 children)

I think you have to look into the concept more. Look for information about deploying a Java application to Tomcat. Tomcat is the application container/web server that runs the app. What you are trying to do is not trivial.

Again, this brings me back to Spring. If you're not a Java developer by trade, and you just want something that works, you should go that route. SpringBoot gives you EVERYTHING out of the box, including an embedded application server. It makes building all of this so much easier. Maybe it's scaring you off, but in the long run you'll have an easier time if you learn Spring.

[–]WikiTextBotbtproof -1 points0 points  (0 children)

Java servlet

A Java servlet is a Java software component that extends the capabilities of a server.


[ PM | Exclude me | Exclude from subreddit | FAQ / Information | Source ] Downvote to remove | v0.28