AFNetworking with Java and Glassfish

2019-06-06 02:50发布

问题:

This could be a longshot question, but I haven't found any specific tutorials or articles on this particular combination of technologies.

My intentions are to use my own APIs (from Java Beans deployed on a GlassFish server) on the iOS device using AFNetworking.

The problem is, I don't really understand the linkage method between the server and the iOS device. How does the iOS code gain access to GlassFish webservices (for example)?

Disclaimer: I have no experience in this particular field (i.e. WebServices and their security), so any newbie-tweaks to my question are very welcome.

回答1:

The fact that you're using Java and GlassFish are really irrelevant - these two technologies are capable of publishing all kinds of services. Instead, look at the protocol that your web-services are using. So you need to ask:

  • Is the transport HTTP? (Most probably)
  • Is the payload XML or JSON?
  • In the case of XML, is there an additional envelope wrapping the actual data? Something like a soap request?
  • Are the web-services being secured? What security spec is being adhered to? OAuth? (recommended for mobile).

By the way, I recommend that you don't expose the Java Beans themselves - in domain driven development terms this is your core model. Exposing your core model is known as spilling your guts - if you change this then all subscribers to your service will need to change too.

Instead you should expose a use-case specific service contract with a payload in/out. You can then map the contents of this onto your core model. (If there's a lot to map, you could use a Framework like Dozer to do it for you).

If you're making the technology decisions yourself there are loads of technologies you could choose from. My personal choice would be:

Either of the following:

  • Spring WebServices (XML)
  • Spring MVC (JSON)

Secured with:

  • Spring Security OAuth provider - (AFAIK, a little complicated compared to Google's provider, but lots of tutorials on integrating with the above two).

Update targeting first-time with these technologies:

  • Java is a language (obviously ;) )
  • GlassFish is an Enterprise Application Server - this means it is capable of hosting Enterprise Java Beans, Accessing LDAP Directories, providing security, etc.
  • The features of GlassFish described above, you probably aren't going to need. All you will need is the servlet engine - which provides a way to host threaded Java code over an HTTP interface.

You'll need to choose how your data is going to be serialized. JSON is much more popular. (Although John Blanco's excellent RaptureXML library makes working with XML really easy).

Therefore, I recommend that you follow a tutorial to deploy a Spring MVC app onto glassfish.

Here's a tutorial on how to get Spring MVC: http://www.mkyong.com/spring-mvc/spring-3-mvc-and-json-example/

Make sure you get it working without security first, before moving on to that part - otherwise it'll be more complicated