How to convert data from PostGIS database into GeoJSON in Java

This can be done in the query that pulls the data out of the database. You can use the postgis function ST_AsGeoJSON(). Here is a link to the documentation for it.

http://postgis.org/docs/ST_AsGeoJSON.html

One thing to note is that the result of ST_AsGeoJSON() only returns the geometry portion of the data. If you need to get a geojson feature, then you will have to create the feature object and add the geometry to it.

An example of the result that you should expect when using ST_AsGeoJSON() would be {"type": "Point", "coordinates": [12, 15]}.

If you wanted to create a feature out of the geometry object, that would look something like {"type": "Feature", "properties": {}, "geometry": {"type": "Point", "coordinates": [12, 15]}}.