Pages

Showing posts with label Spring. Show all posts
Showing posts with label Spring. Show all posts

Sunday, 3 August 2014

Consuming REST Services using Spring


  1.  RestTemplatemakes interacting with most RESTful services a one-line incantation. And it can even bind that data to custom domain types.
  2. As you can see, this is a simple Java class with a handful of properties and matching getter methods. It’s annotated with @JsonIgnoreProperties from the Jackson JSON processing library to indicate that any properties not bound in this type should be ignored.
  3. Because the Jackson JSON processing library is in the classpath, RestTemplate will use it (via a message converter) to convert the incoming JSON data into a Page object. From there, the contents of the Page object will be printed to the console.
    Here you’ve only used RestTemplate to make an HTTP GET request. But RestTemplatealso supports other HTTP verbs such as POSTPUT, and DELETE.
  4. http://spring.io/guides/gs/consuming-rest/

What does the spring framework do ?


  1. Dependency Injection
  2. AOP (Aspect oriented programming)
  3. Boiler plate coding removal

Thursday, 17 July 2014

Spring Annotations vs XML

Annotations have their use, but they are not the one silver bullet to kill XML configuration. I recommend mixing the two!
For instance, if using Spring, it is entirely intuitive to use XML for the dependency injection portion of your application. This gets the code's dependencies away from the code which will be using it, by contrast, using some sort of annotation in the code that needs the dependencies makes the code aware of this automatic configuration.
However, instead of using XML for transactional management, marking a method as transactional with an annotation makes perfect sense, since this is information a programmer would probably wish to know. But that an interface is going to be injected as a SubtypeY instead of a SubtypeX should not be included in the class, because if now you wish to inject SubtypeX, you have to change your code, whereas you had an interface contract before anyways, so with XML, you would just need to change the XML mappings and it is fairly quick and painless to do so.
I haven't used JPA annotations, so I don't know how good they are, but I would argue that leaving the mapping of beans to the database in XML is also good, as the object shouldn't care where its information came from, it should just care what it can do with its information. But if you like JPA (I don't have any expirience with it), by all means, go for it.
In general: If an annotation provides functionality and acts as a comment in and of itself, and doesn't tie the code down to some specific process in order to function normally without this annotation, then go for annotations. For example, a transactional method marked as being transactional does not kill its operating logic, and serves as a good code-level comment as well. Otherwise, this information is probably best expressed as XML, because although it will eventually affect how the code operates, it won't change the main functionality of the code, and hence doesn't belong in the source files.

http://stackoverflow.com/questions/182393/xml-configuration-versus-annotation-based-configuration

Spring Annotations

What is dependency injection ?
Expanding this into a real system, we might have dozens of such services and components. In each case we can abstract our use of these components by talking to them through an interface (and using an adapter if the component isn't designed with an interface in mind). But if we wish to deploy this system in different ways, we need to use plugins to handle the interaction with these services so we can use different implementations in different deployments.
http://martinfowler.com/articles/injection.html 
http://stackoverflow.com/questions/tagged/spring 

Unit testing : http://docs.spring.io/spring/docs/2.5.x/reference/index.html
 
  1. @RestController - HTTP requests are handled by a controller. These components are easily identified by the @RestController annotation.
    A key difference between a traditional MVC controller and the RESTful web service controller above is the way that the HTTP response body is created. Rather than relying on a view technology to perform server-side rendering of the greeting data to HTML, this RESTful web service controller simply populates and returns a Greeting object. The object data will be written directly to the HTTP response as JSON.
    This code uses Spring 4’s new @RestController annotation, which marks the class as a controller where every method returns a domain object instead of a view. It’s shorthand for@Controller and @ResponseBody rolled together.
    The Greeting object must be converted to JSON. Thanks to Spring’s HTTP message converter support, you don’t need to do this conversion manually. Because Jackson 2 is on the classpath, Spring’s MappingJackson2HttpMessageConverter is automatically chosen to convert the Greeting instance to JSON.
  2. @RequestMapping - The @RequestMapping annotation ensures that HTTP requests to /greeting are mapped to the greeting() method.
  3. @RequestParam -  @RequestParam binds the value of the query string parameter name into the nameparameter of the greeting() method. This query string parameter is not required; if it is absent in the request, the defaultValue of "World" is used.
  4.