简体   繁体   中英

MobileFirst Adapter to Servlet

I am creating a project with IBM Mobile First, from the application I am collecting some data and sending it to worklight adapter. From the MobileFirst adapter I have to send it to servlet and save it to a variable. Once that is done it has to send a response as success. Next, whenever the MobileFirst adapter requests for the data, the servlet has to return it.

Now, I am able to send data from MobileFirst adapter to servlet via POST , but from the servlet I have no idea on how to extract the data which I have sent from MobileFirst adapter.

Any Idea?

I am not a Java expert, but I assume that your servlet needs to handle POST requests and send an appropriate response - depending on what you are expected to return (which you did not mention in your question. Perhaps you should).

As such, perhaps the following will help you:

It sounds as though you are writing both an adapter and the service that the adapter will call; you are in control of both. A couple of design questions follow.

First, why do you have a servlet at all? What are you going to to do in the servlet that you cannot do in the adapter? I'm not quite clear what your servlet is doing, but saving some data in a variable and retrieving it is something the adapter itself can do. Adapters have session state if you need to keep data for each user's session. Also, you can keep "global" data in Java static properties. If this is something you want to explore further you might ask some further questions.

Second, let's suppose that the servlet is playing the role of a service-provider, so not only will adapters call this service but perhaps other clients too; we are trying to create a general purpose service. In that case it is reasonable to use a servlet, but we should consider using an appropriate service standard. Two possibilities are SOAP-based Web Services and RESTful JSON-returning services. The advantage of using such standards is that any client probably has standard libraries to consume such services.

I would start by considering the REST service option. You may be concerned that writing a servlet to manage REST services is a lot of work, but in fact it's amazingly easy. There are many tutorials, for example . All you do is write a suitable annotated Java class, like this.

@Path("/myResource")    
public class SomeResource {       

    @POST
    public String doPost2(FormURLEncodedProperties formData) {
        ...
    }
}

This is actually less work that explicitly writing a servlet. The only overhead is the addition of the @PATH and @POST annotations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM