I'm new to REST on Java and I would like to create "simple" REST services that queries a database and returns some JSON info.
What I would like is to use JDBC access to DB, no JPA.
When working with servlets I configure a server datasource and in my servlets in the "init()" method I store a reference to the datasource and on every request create a connection.
What must I do for REST services?
Thanks in advance.
If you are planning to connect via JDBC based on the REST API requests and already familiar with REST, you should consider looking into Spring JDBC support: Current JDBC Spring Support. This would take care of all your concerns regarding the right place and time of instantiating/looking up a data source or connection.
Related
I have established connection between my Spring project and MySQL DB, I also have an existing HTML site with login and register templates. Can someone point me into the right direction on how to connect Spring with my existing HTML?
In order to implement login and register workflow you need to implement few things:
REST API http endpoints for login and register - you can use Spring for that
Data base of some sort to to store users data (that should be encrypted) - you can use MySQL for that
Some sort of mechanism to authenticate users - I recommend using JWT for that
Considering your tech stack, which include Spring (I guess it is Spring Boot), and MySQL I can suggest going through this great article which walks you through building login workflow using Spring boot, MySQL and JWT
I can also recommend using existing technology for managing user data, specifically I recommend KeyCloak which is a Open Source Identity and Access Management.
So I'm busy with a Spring Boot REST server which connects to a PostgreSQL database.
My goal is to make an application which can easiliy be ported to android that is able to send a login JSON POST request and then maintain a session so that you don't require to send the credentials again for other GET requests. What are some ways that I could implement something like this?
I'm a bit confused as to what to use. Because there are so many library's, I also heard about a Spring Sessions library and Auth2. Auth2 however seems to be more applicable for integration with facebook/google login, which I do not want.
You will want to add spring-security to that spring boot in order to handle and maintain sessions. Take a look into their documentation and view this blog post to help you out. You will have some reading fun time, yeah!
I have a spring 3 project, and I now need to share session among multiple server instance. Naturally, I thought about mongoDB, I want to use MongoDB as session storage for Spring project. But I googled a lot, and I can only find reference for Spring boot application, there is NO documentation telling me how to use mongoDB as sessionStorage for Spring 3.1.2 project.
Can anyone help to refer some good material telling me how to use mongoDB as session storage for Spring 3.1.2 project?
Thanks in advance.
Basically you want to issue your clients unique identifiers (UUIDs/IDs of any kind) and save any data you want to any DB you want by this unique ID. On subsequent requests you extract the ID the client sends back to your server via cookies/headers/HTTP parameters/whatever. You look up you "session" information in DB by this ID and use it in any manner.
I have this simple REST service running in our JBoss app server which works great. For now, the service has one #GET method (getAllPeople), which makes a JDBC connection to a mysql database, to retrieve some data (SELECT name, address FROM Person).
My team lead wants me to experiment with Mule ESB, and use this simple project as a starting point. I'm confused on how to make this work with a Mule flow. I set up my flow with an HTTP inbound point, REST component, and JDBC component, like this:
I configured the JDBC component to use MySQL, and added a query to it (SELECT name, address FROM Person).
The question: The code in my REST service #GET method is still connecting directly to the DB with JDBC. I think I need to change this to instead, invoke the Mule JDBC datasource instead, but I have no idea how to do this. What code do I need to put in my REST service #GET method to utilize the Mule JDBC component, and get a result? I am trying to get a grasp on how the components talk to each other.
EDIT: new thought - Should an ESB be used to link different components of a single system together, such as REST services, SOAP services, and JDBC data sources, or should an ESB be used only to connect completely independent systems together?
To make your trivial example work, just delete the REST component and add an Object to JSON transformer after the JDBC component. Your flow will then return the query results in JSON format.
We can use mule here to call the rest api, rest api will return some data that may not be sufficient. In order to get more data from db based on the result of rest call (transformed by JsontoObject) jdbc connector could be used. In your case if you want to call rest api which eventually calls the DB, you will get json or xml which can be transformed to send the data to outbounds.
Thanks-
I am not sure why you have that 'rest' component in between. that may not be needed. the simplest would be to have the http, and a transformer (to transform the data from http to/from), and a jdbc component. with this approach you are leaving behind the existing ReST service in JBoss App Server.
I think in the posted picture you try to call the pre-existing ReST service in Jboss App Server which may not be needed. You can simply immitate what the existing service does. or you may consider the existing service and the new mule service can act as a proxy service - in such a case the JDBC component is not required.
I have created a simple JSF application, now must to connect to SQL Server and perform CRUD operations on those tables from database.
I was a .NET programmer and i don't know how to connect to SQL Server from JSF. I have read something about JNDI, but not understood well. My questions are:
where should JNDI be defined: on Tomcat or my application?
where to define the connection string?
which driver/jar should be used?
Can you recommend any code samples, links to tutorials how to perform crud operations, or any other guidance?
where should JNDI be defined: on Tomcat or my application?
In the JNDI container. That's thus Tomcat.
where to define the connection string?
In the JNDI container. In case of Tomcat, that'll go in the context.xml. You can either modify Tomcat's own context.xml or supply your own in META-INF folder of your webapp. More details can be found in the Tomcat JNDI resources HOW-TO.
which driver/jar should be used?
The one which can communicate with the DB in question. In case of Microsoft SQL Server, that's under each the DB-vendor provided JDBC driver or the performancetechnically better jTDS driver.
Here are some useful tutorials which might help you step by step further:
Basic DAO tutorial (with JDBC) - Using in JSF
CRUD with JSF 2.0
JPA tutorial (better than JDBC)
Netbeans CRUD with JSF 2.0 and JPA 2.0
This is a very broad question. I will take a shot at trying too keep it simple and short.
Here are the steps.
First create a backing bean that works with your front end face page.
Create a service class that encapsulates the CRUD tasks.
Create a database methods class that performs each CRUD task.
This is how the code should flow:
"Your UI face invokes a method in the backing bean->the backing bean invokes service class->service invokes the database methods class. This is commonly referred to as the DAO pattern."
For details on how to connect to a database.
You can either create a local data source and connect via standard JDBC procedure.
Or you can create connection pools in your container (JBOSS, WebLogic, etc). Then look up those connection pools in your app via JNDI lookup.
If you are very new to this, then I would recommend to start out with creating a basic database connection using JDBC and running your queries against it. In the long term, you will want to get yourself familiar with connection pool (actually this will give you a better performance too), Spring JDBC framework, ORM support (hibernate, iBatis).
Here is a link to starting a jdbc connection for Microsoft SQL server (example on step1).