How to marshall requests?

Hello all,

I'm trying to make an custom endpoint receive some data from a javascript application that uses Nuxeo JS client. The JS application uses the following code to perform the request:

doEndpointTestRequest(){
    // create the opts dict to be inserted into the request
    var opts = {};
    opts.json = true;
    opts.method = "POST";


    var body_json = { myKey: "myValue" };

    opts.body = JSON.stringify(body_json);

    this.nuxeo.request('ext/poc/mytestendpoint')
        .post(opts)
        .then(function(data) {
            console.log("Post is done.")
        })
        .catch(function(error) {
            throw error
        });
}

To handle the data inside my endpoint, I'm trying to get a JSON-to-Java marshaller to work. (You can notice I'm trying to do the full mode feature/layer here) For that, I went into my project folder and wrote:

./nuxeoctl bootstrap contribution

I chose some names and parameters that may not be relevant, except this: I chose the org.nuxeo.ecm.core.io.MarshallerRegistry as the target and the marshallers as the extension point. It created a myextension-contrib.xml file. In this file I registered my marshaller:

  <extension target="org.nuxeo.ecm.core.io.MarshallerRegistry" point="marshallers">
      <register class="com.gregoreki.core.MyMarshaller" enable="true" />    
  </extension>

Its code is listed below:

// ... imports omitted
@Setup(mode = Instantiations.SINGLETON, priority = Priorities.REFERENCE)
public class MyMarshaller extends AbstractJsonReader<MyBodyRequest> {

    @Override
    public MyBodyRequest read(JsonNode jn) throws IOException {
        return new ObjectMapper().readValue(jn.toString().getBytes(), MyBodyRequest.class);
    }
}

Of course, I need the MyBodyRequest.java:

public class MyBodyRequest {
    private String myKey;

    public String getMyKey(){
        return myKey;
    }

    public void setMyKey(String myKey){
        this.myKey = myKey;
}

And then, in my endpoint, I have:

    @POST
    @Path("mytestendpoint")
    @Consumes(MediaType.APPLICATION_JSON)
    public Object postTestEndpoint(MyBodyRequest request){
        // do something with the request marshalled object
    }

Well, it seems I got it right, doesn't it? What happens is that the endpoint is never hit and I get a HTTP::405 error: method not supported. When I change the endpoint code to

    public Object postTestEndpoint(String request){ ... }

it works. Why isn't my marshaller working? Why does it only accept a String as the parameter?

Regards, Carlos G.

0 votes

1 answers

53 views

ANSWER



Hi,

Did you follow the documentation here: https://doc.nuxeo.com/nxdoc/howto-contribute-to-the-rest-api/#extending-the-rest-api-the-workflow-case?

You need to deploy 2 different bundles:

  • one with the Fragment-Host: org.nuxeo.ecm.platform.restapi.server entry in the MANIFEST.MF. This bundle will contain the REST endpoint (annotated with @WebObject)
  • another one for the Nuxeo contribution (the MyBodyRequest object, the marshaller, …)

Contributions from the bundle with the Fragment-Host are not deployed by Nuxeo.

See the warning on the page:

This also causes all of the other components in the fragment bundle not to be deployed. Thus, any endpoint or web adapter class you add, respectively annotated with @WebObject or @WebAdapter, need to be in a standalone bundle, not in the same bundle as extension point XML contributions for instance.

1 votes



Thanks for your answer and for the heads up.

However, I fixed the problem by not adding a marshaller at all. Magically, the MyBodyRequest object was parsed without declaring a marshaller for that type.

10/11/2019