-
RestTemplatemakes interacting with most RESTful services a one-line incantation. And it can even bind that data to custom domain types. - As you can see, this is a simple Java class with a handful of properties and matching getter methods. It’s annotated with
@JsonIgnorePropertiesfrom the Jackson JSON processing library to indicate that any properties not bound in this type should be ignored. - Because the Jackson JSON processing library is in the classpath,
RestTemplatewill use it (via a message converter) to convert the incoming JSON data into aPageobject. From there, the contents of thePageobject will be printed to the console.Here you’ve only usedRestTemplateto make an HTTPGETrequest. ButRestTemplatealso supports other HTTP verbs such asPOST,PUT, andDELETE. - http://spring.io/guides/gs/consuming-rest/
Apart from coding and design interview questions, this page contains updates on my learnings with Java. It helps me organize my learning. Read about my future self here : https://siliconvalleystories.blogspot.com/
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
What does the spring framework do ?
- Dependency Injection
- AOP (Aspect oriented programming)
- 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
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
- @RestController - HTTP requests are handled by a controller. These components are easily identified by the
@RestControllerannotation.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 aGreetingobject. The object data will be written directly to the HTTP response as JSON.This code uses Spring 4’s new@RestControllerannotation, which marks the class as a controller where every method returns a domain object instead of a view. It’s shorthand for@Controllerand@ResponseBodyrolled together.TheGreetingobject 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’sMappingJackson2HttpMessageConverteris automatically chosen to convert theGreetinginstance to JSON. - @RequestMapping - The
@RequestMappingannotation ensures that HTTP requests to/greetingare mapped to thegreeting()method. - @RequestParam -
@RequestParambinds the value of the query string parameternameinto thenameparameter of thegreeting()method. This query string parameter is notrequired; if it is absent in the request, thedefaultValueof "World" is used.
Subscribe to:
Posts (Atom)