I'm still learning spring and currently I'm trying to create webapplication and RESTful API at the same server. It was easy until I tried to add OAuth2 authorizaton to be able to consume that API in Android application.
I have a problem because I don't completely understand what is happening under the hood of spring.
I want achieve something like this:
localhost:8080**/** - webapp authorized in normal way
localhost:8080**/login** - webapp login page
localhost:8080**/api/** -RESTfull API
localhost:8080**/oauth/[authorize|token]** - OAuth2
Android application to consume API
Is it even possible? Can you provide an example code or just the way I should do it?
Well, Spring Boot has plenty of documentation.
I'd recommend the Spring Boot manual
As for what you want, yes, it's possible.
See this demo which has OAuth, REST API and normal HTML, CSS, JS files all in one. Also, this demo is based on spring-boot, so what more do you want :)
Again, the demo url:
https://github.com/szerhusenBC/jwt-spring-security-demo/
As I said above, it's possible. Not recommended, but possible. It would be recommended to split them, for a better separation of concerns. Also for performance reasons: Apache for instance can serve static files much faster than SpringBoot/Tomcat can.
Of course you can start this way and do it later. Enjoy.
Yes! Take a look at JHipster and how they realize OAuth2. http://jhipster.github.io You can generate app and check how everything is done under the hood. You can also find many useful patterns for development.
Related
I have created a simple Web Portal using JSP/Servlets. It is connected to MySQL Database and basically the portal is just used to manage the records in the MySQL Database.
I now want to expose REST APIs for anyone to retrieve and insert information into the MySQL database.
Would I be able to do so using JSP/Servlets.
It would be great to have suggestions.
Lets first get some short definitions:
REST is an architecture design. You can read this clear explanation from Wikipedia. Today most REST services use JSON as a message format, but this is not mandatory neighter part of REST. You can have XML, images, etc.
Servlet is a RESTful implementation. You can extend HttpServlet in Java an implement the RESTful methods (GET, PUT, POST, DELETE). Lot of people just go with POST and GET, so it is not RESTful by definition, but it do the work.
JSP is not what you are looking for in this case. Its mostly designed for user interface, and is not the right approach to expose "API"s as services and such.
Now about your question, the answer is yes you can use Servlets, but there are better tools to implement REST today. You'll have a litte more work to set up your structure but it will be an elegant solution.
The way to go with Java is with JAX-RS, the Java Enterprise specification for RESTful Services. You can follow the official tutorial Building RESTful Web Services with JAX-RS. I highly recommend you to follow this tutorial since it will give you a good grasp over the concepts involved.
You can use Spring too: Building a RESTful Web Service. I use Jackson 2.0 with Spring and it works like a charm.
Other example here.
Yes, you can do this only with servlets, but this is not a great idea, because there are a lot of good frameworks to do this.
Take a look a this link Jersey
Some years ago I made a Swing application that uses a MySQL database. Now I must change this app to work through Internet.
I can connect directly to a MySQL server through Internet, but I think that it's insecure, so I'm thinking about using webservices.
I know I must change lots of code in my current application, and I don't worry about that. But my problem is that I have never used webservices nor server side technologies in Java, and I don't know where to start.
Do you know any good "webservices for dummies" tutorial? I need authentication (and ACL or similar) and database access.
Thank you!
Easiest adaptation: have a look at apache axis2, it has everything you need for webservices. That would offer to extend you current applicatino by webservices functionality.
If you need to re-engineer everything, either spring or Java EE should be your friend.
Adding web services won't make it any more secure. The best you can do is to enable SSL with JDBC.
Take a look at the core servlets site. There is an EJB tutorial and an AXIS tutorial. As mentioned by Andreas_D, spring and Java EE are both options. I suspect you want a simple REST implementation, in which case, spring is a good option.
I have to come up with a basic CRUD app. as fast as possible (and by "fast" I mean like 3 days).
I considered going with Django, but I don't have a lot of experience with Python/Django, and also I have a complete set of EntityBeans that I need do CRUD on. So I thought reusing them would be better.
The resulting CRUD app. should be similar to the Django admin page (but the Django admin page per-se won't work though).
Is there some framework with which I can start with EntityBeans and get a quick, Django admin like skeleton?
If you don't want to change a technology stack you're working on and still have an Django-like administration interface, there is a Java alternative - LightAdmin all-in-one library.
After declaring Maven dependency & enabling administration panel in your web.xml, you will have a complete CRUD with some additional features, like filtering, scopes, security, etc.
The only code related to data administration in your codebase would be DSL configurations for UI customization.
The only thing is that LightAdmin could be used only for JPA domain model entities administration for now.
I would recommend the playframework as it is easy to learn and quick to get up and running. They have a CRUD administration module you can use to generate CRUD framework based on your Entity model. Also check out their tutorial page.
You must try AppFuse or Spring Roo.
http://appfuse.org/display/APF/Demos+and+Videos (+online demo)
http://spring.io/
Spring Roo gives you a CRUD application up in 10 minutes. Unlike other frameworks this really is practically usable.
I have downloaded the Roo shell for the first time and without reading any documentation I managed to get an application up and running within minutes.
Naked Objects, if you're look at a prototype. Although it does have a "viewer" for Wicket apart from the HTML viewer, I would recommend using the framework primarily for fleshing out the functionality of your domain.
You might also want to check the Scimpi viewer, although I must admit that I havent worked with it.
A demo application using the HTML viewer is hosted online, and is accessible for having a quick look at the functionality. A tutorial demo is also hosted online.
Give Grails about 2 hours, and if you don't have a CRUD app after that you're doing something wrong.
I've used CodeCharge Studio by Yes Software for several years, and am able to build a CRUD app in less than 10 minutes (assuming database already exists). CodeCharge has a bit of a learning curve if you want to go beyond the interfaces created by the wizards, yet I think my development time has increased by a factor of at least 30 from writing my own code.
I use it at work to create PHP applications that hit MSSQL, and other projects that hit MySQL. Works like a charm for both.
http://www.yessoftware.com/index2.php
I am not a "pythonista" so this may be absolute rubbish but could Django running on Java be the answer for you?
Jython is an implementation of python for the JVM and can run Django.
http://docs.djangoproject.com/en/dev/howto/jython/
Yes Java has many such framework for both web and desktop but .net has very few..
I recently used RocketFramework , which was to my expectation.. So just thought to post it here so that anybody interested have the link :-)
You can create a Quick Crude App in minutes using HTML5, i found www.quickcrud.com that allows you to generate the HTML5 on line and also it's possible to generate Android Apk also in minutes!
I've been looking into JAX-RS lately because I really like the Java platform and a RESTful style of web development. I've read a lot about using JAX-RS for creating RESTful Web Services, but is it possible to use JAX-RS to create RESTful web sites? I guess more specifically, is it possible to use JAX-RS as a controller (to retrieve required data from the server) and then forward control to a view engine to render the appropriate HTML?
I've been googling around but haven't found any resources that show you how to do this.
Thanks for any insight/help.
I think you are having difficulty with your google searches because not many people are doing this. JAX-RS was designed for web services, so developers don't think of using it as a controller for web applications. However, there is no reason it wouldn't work.
Check out this blog post: JAX-RS as the one Java web framework to rule them all?
I think it's exactly the kind of thing you are looking for.
If you truly want to rely just only JAX-RS for your web framework, Jersey might be your best bet. Keep in mind the features you get from it are going to be bare minimum and you are obviously not going to get all the bells and whistles like what's provided by JSF, Wicket, etc.
If you know your web application is going to rely on Spring, perhaps you should consider using Spring MVC 3.0. It provides restful web services-alike and it gives you better features so that you don't need to implement most of them yourself. Granted, Spring MVC 3.0 is not an implementation of JAX-RS and based on what the Spring developer said, it seems like they will never make Spring MVC as an implementation of JAX-RS since they are already quite a few stable implementations out there. However, the syntax is pretty similar in my opinion, or at least I was able to understand them rather quickly even though I have been using Jersey for quite awhile.
dbyrne is right that almost no one is doing this. It's more conventional to use JAX-RS to dump information to JSON or XML. Then you fancy up the web browser with an RIA framework (e.g. Ext JS), which handles manipulating the DOM and injecting data as its fetched in JSON/XML form. This approach is powerful. You can write multiple, possibly non-browser clients for the service, all parsing the same JSON/XML. You can write "one-page" webapps, where all information exchange happens through AJAX after the initial pageload. I urge you to investigate and consider its strengths and weaknesses in the context of your particular problem.
Returning to your question: the answer is "sort of". This functionality is not directly provided by the JAX-RS spec (as of 1.1). However, it is in the JAX-RS reference implementation, Jersey, through the Viewable response object. See this blog post if you want to investigate further: http://blogs.oracle.com/sandoz/entry/mvcj. I want to point out that I have no experience with this side of Jersey. It has been pleasant to write XML/JSON-returning web services with Jersey, but I can't speak to this server-side HTML templating business.
Edit: dbyrne's edited his answer to include a blog post which points to the one mentioned above. I think we've both converged on approximately the same answer.
I'm looking for a light version of REST for a Java web application I'm developing.
I've looked at RESTlet (www.restlet.org) and the REST plugin for Struts 2, but I haven't made up my mind. I'm leaning towards RESTlet, as it seems to be lighter.
Has anyone implemented a RESTful layer without any of the the frameworks or with the frameworks?
Any performance issues that you've seen because of the new web layer?
Did the introduction of REST added unmanageable or unreasonable complexity to your project? (Some complexity is understandable, but what I mean is just plain overkilling your design just to add REST)
I'm a huge fan of JAX-RS - I think they've done a great job with that specification. I use it on a number of projects and its been a joy to work with.
JAX-RS lets you create REST resources using POJOs with simple annotations dealing with the URI mappings, HTTP methods and content negotiation all integrated nicely with dependency injection. There's no complex APIs to learn; just the core REST concepts (URIs, headers/response codes and content negotiation) are required. FWIW JAX-RS is quite Rails-ish from the controller point of view
There are a number of JAX-RS implementations out there - see this thread for a discussion.
My personal recommendation is to use Jersey as its got the biggest, most active community behind it, has the best features at the time of writing (WADL support, implicit views, spring integration, nice REST client API); though if you are using JBoss/SEAM you might find RESTeasy integrates a little better.
I'm a big fan of Restlet, but I usually use it to implement apps whose primary role is to be a RESTful web service. It sounds like you're looking to add a RESTful API to an existing application. If that's the case, JAX-RS's (or Enunciate's) annotation-based approach might be a better fit for your project.
As for Restlet, I can tell you that I've been very impressed with the developers and the community; they're very active, engaged, responsive, and committed to a stable, efficient, reliable, and effective framework. My single favorite aspect of the framework is that it is a ground-up implementation of the REST paradigm; therefore there is no impedance-mismatch between a Restlet app's external API and internal implementation. I also really like how flexible it is - it can run inside a Java application container/server such as JBoss, Tomcat, Jetty, etc, or standalone, with an embedded HTTP server library.
Well, I've used Enunciate quite a bit. It uses simple annotations to provide either REST and/or SOAP endpoints.
http://enunciate.codehaus.org
Plus, Ryan Heaton has always provided top-notch support for things, too.
You know there is a new JCP API for Accessing RESTful Services, also:
JAX-RS JCP311
https://jsr311.dev.java.net/
The open source version is called Project Jersey
I am working on a REST API for gliffy.com and we ended up rolling our own. We didn't want to have to bring in Struts 2, Spring, or any other framework. I looked at RESTLet and found it incredibly confusing and over complicated.
Apache has an implementation of the JAX-RS spec, but it is not finalized and also has some oddities to it. We're tentatively planning to open source our solution, but that's not for a few months.
Rolling your own is easy, though. The Servlet Specification gives you everything you need, and you can easily connect to a database via Hibernate (see http://www.naildrivin5.com/daveblog5000/?p=39 for how to set up JPA without using EJB3).
I found restlet to be a really elegant architecture. I'm working in the .net world so it was not an option for me, but I was able to build my own framework following the same basic principles of restlet.
I have found the conversion of our WCF contract-based SOA application to REST based one has significantly simplified the application,