16

As part of a possible solution to this question i want create a layer in GeoServer via REST that supplies a SQL statement along with a userid.

Reading I think i would create a feature type and publish it? I need to do this all in REST. Does anyone have any code examples?

  • Is looks like it is not possible to add a layer via rest interface, did you resolve it in the end? I get the error: HTTP Status 405 - The specified HTTP method is not allowed for the requested resource – a1an Jun 4 '15 at 16:08
39

There are many things you can do with the GeoServer REST API that are not specifically documented and that there are not code examples for. Here's the strategy for tackling those.

First, start with the examples in the documentation. Make sure you are familiar with how you can create a simple new layer or workspace using an HTTP POST with either XML or JSON.

Then, through the GeoServer UI, manually create the object you need for which there is no documentation (in this case, a feature type).

Finally, manually browse to the GeoServer REST index (http://your-server/rest or http://your-server/geoserver/rest). Browse through the index until you find the feature type you just created. Append ".xml" or ".json" to the URL of this resource, and you will see its XML or JSON representation.

This representation is what you would have needed to POST to create the feature type through the API. The URL of the representation is the URL that you would have needed to POST to (for example, http://your-server/geoserver/rest/..../myFeatureTypeName.json).

You can use this strategy to figure out how to programmatically create or configure any resource in GeoServer.

  • 2
    THANK YOU! Easy answer and just saved me another 2 hours of mocking around – Ragi Yaser Burhum Mar 3 '12 at 0:24
  • 4
    In addition, a lot of the parameters can be skipped, and GeoServer will figure them out (just like in the UI). eg. bounding boxes – rcoup Mar 7 '12 at 4:21
  • 1
    Wow - this is the best description of this I've seen. – Kieveli Mar 4 '17 at 16:14
  • 3
    Using GeoServer 2.10.2, I found that I needed to POST to the featuretypes endpoint of the store, eg geoserver/rest/workspaces/<workspacename>/datastores/<storename>/featuretypes/ – Stephen Lead Jun 14 '17 at 3:25
  • Clear workflow description, relevant links included. UPVOTED! :) – tony gil Aug 25 '18 at 10:40
9

I know this is an old question, but just in case anyone else is confused as I was. The important part is that you cannot create a layer from the http://geoserver/rest/layers endpoint.

If you are looking to add a vector layer, it is done through the feature type resource:

http://docs.geoserver.org/stable/en/user/rest/api/featuretypes.html

A POST to:

/workspaces/<ws>/datastores/<ds>/featuretypes

where is the workspace you want the feature type to live under, and the datasource to use will create a new vector feature type, ie layer.

If you are wondering about what you can POST, @Rohan is correct, the easiest way is to query for an existing feature type, IE an http GET to

/workspaces/<ws>/datastores/<ds>/featuretypes/<ft>.json 

which will return an existing feature type as json.

1

Your best reference is in the user guide. http://docs.geoserver.org/stable/en/user/rest/examples/index.html

  • Yeah, but there is no example for creating a feature type from a SQL statement... i was hoping someone had example code to share. – CaptDragon Aug 2 '11 at 14:56
  • That user guide is severely lacking. – Kieveli Mar 4 '17 at 16:15
0

If you understand Java you might want to take a look at this simple code that uses REST to do all kinds of operations on GeoServer.

0

To add a layer, a datastore and a featuretype should be defined. Assuming they are already defined along with a style (as described in the guide at http://boundlessgeo.com/2012/10/adding-layers-to-geoserver-using-the-rest-api/ to add a layer a PUT request has to be sent to geoserver at:

http://geoserver.host/geoserver/rest/layers/NEW_LAYER_NAME

With a data content like:

<layer>
  <name>NEW_LAYER_NAME</name>
  <type>VECTOR</type>
  <defaultStyle>
    <name>myStyle</name>
  </defaultStyle>
  <resource class="featureType">
    <name>myFeature</name>
  </resource>
</layer>
  • That blog talks about adding a style to an existing layer, which makes sense as that is what PUT is designed for. You cannot create a new layer this way. Here is the docs: docs.geoserver.org/stable/en/user/rest/api/layers.html, a PUT to rest/layers will return a 405. A PUT to /rest/layers/<existing_layer> will modify a particular layer, not create a new one. – lostintranslation Mar 4 '17 at 17:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.