简体   繁体   中英

I have REST API and want to reuse it for exposing differen format API on same server

I implemented and exposed REST API on my server. Now I need to expose same API, but I can't use REST (it's actually websocket messages), it will be some custom format. Don't ask why )

I imagine message transformation from my custom format to http request, then process it in my web-server, transform response back to my custom format, and send to client.

The simplest way is regular http call to localhost. For example (java):

HttpURLConnection con = (HttpURLConnection) new URL("http://localhost/api/...").openConnection();

and so on, or using some http client library. But I'm afraid there will be too much overhead, creating connection, etc.

Another ways:

  1. I use Tomcat. Push my request directly to tomcat somehow.
  2. I use Guice, and all requests go through GuiceFilter. Craft ServetRequest, ServletResponce and FilterChain objects and directly call GuiceFilter.doFilter.
  3. I use GuiceContainer for Jersey. Some test frameworks use it for REST API testing, but also need to craft request/response objects.

There is no standard way to craft request object at all. And I don't know on which level it's better to add my custom requests.

Hope I described my problem clearly.

Atmosphere may be a good fit here. It's allegedly compatible with both Resteasy and Jersey, so that's a plus, it simply adds WebSocket functionality on top of these.

Caveat: I haven't tried this myself, but came across it when looking for the same capability. :-)

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