Академический Документы
Профессиональный Документы
Культура Документы
features, but there are some new usage scenarios like type-safe queries that warrant some
extension to be provided. The core part of the Spring Data JDBC Extensions project provides this
type of extension and it can be used together with any supported SQL database.
While integrating spring and hibernate, we wont create hibernate.cfg.xml file. We give this file
information with in spring configuration file. It is recommended. We will configure all information
to localsessionfactorybean of spring framework.
Singleton pattern controls the instantiation of a class and ensures that only one instance of the
class exists in the java virtual machine. The singleton class must provide a global access point to get
the instance of the class. Singleton pattern is used for logging, driver objects, caching and thread
pool.
In Factory pattern, we create object without showing the creation logic to the client and refer to
newly created object using a common interface.
MVC Pattern stands for Model-View-Controller Pattern. This pattern is used to separate
application's concerns. Model - Model represents an object or JAVA POJO carrying data. It can also
have logic to update controller if its data changes. View - View represents the visualization of the
data that model contains. Controller - Controller acts on both model and view. It controls the data
flow into model object and updates the view whenever data changes. It keeps view and model
separate.
Comparator vs Comparable
Implementation Class whose objects to be sorted must Class whose objects to be sorted do not
implement this interface.e.g Country class need to implement this interface.Some
needs to implement comparable to other class can implement this interface.
collection of country object by id E.g.-CountrySortByIdComparator class can
implement Comparator interface to sort
collection of country object by id
Java.lang.Comparable
Package Java.util.Comparator
Shown below. DispatcherServlet acts as the front controller. Simplified actions taken by
DispatcherServlet are listed below.
@RequestMapping(value="/players", method=RequestMethod.GET)
Example 2 : If a url /players/15 is keyed in, playerId is populated with value 15.
@RequestMapping(value="/players/{playerid}", method=RequestMethod.GET)
Handler interceptors are used when you want to apply specific functionality to certain requests.
Handler Interceptors should implement the interface HandlerInterceptor.
<bean id="handlerMapping"
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping
">
<property name="interceptors">
<list>
<ref bean="yourCustomHandlerInterceptor"/>
</list>
</property>
</bean>
Spring 3.0 introduced TaskScheduler abstract to deal with scheduling jobs. Spring has support for
Timer (Jdk) and Quartz. Sample methods in the interface TaskScheduler are shown below:
@Scheduled(fixedDelay=5000)
</task:scheduled-tasks>
Tiles helps us to define the layout for a web page. We can integrate Spring MVC with tiles by
configuring TilesConfigurer and setting up appropriate view resolver.
<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles2.TilesConfigurer"
p:definitions="/WEB-INF/tiles-defs/templates.xml" />
<bean id="tilesViewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver"
p:viewClass="org.springframework.web.servlet.view.tiles2.TilesView" />
How do you configure Spring MVC web application to use UTF-8 encoding for handling
forms?
<filter>
<filter-name>encoding-filter</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding-filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>
org.springframework.web.filter.DelegatingFilterProxy
</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager" p:sessionFactory-
ref="sessionFactory" /><tx:annotation-driven/>
Next, we can add the @Transactional annotation on the methods which need to part of a
transactions.@Transactional(readOnly = true)
How does Struts (Struts 1.x) handle requests? Can you explain with an example?
User clicks on a link in an HTML page.
Servlet controller (ActionServlet) receives the request (Front Controller pattern) , forwards
it to RequestProcessor.
RequestProcessor looks up mapping information in struts-config.xml, and routes to an
action.
Action makes calls to populate the Model.
Action forwards to a View resource (JSP page).
RequestProcessor looks up the mapping for the requested resource and forwards to the
appropriate JSP page.
JSP file is invoked and sent to the browser as HTML.
User is presented with a new HTML page in a web browser.
<interceptors>
<interceptor name="authenticationInterceptor"
class="com.java.interview.AuthenticationInterceptor"/>
<interceptor-stack name="authenticatedStack">
<interceptor-ref name="authenticationInterceptor"/>
<interceptor-ref name="defaultStack"/>
</interceptor-stack></interceptors>
There are a lot of misconceptions about REST Web Services : They are over HTTP , based on JSON
etc. Yes : More than 90% of RESTful Web Services are JSON over HTTP. But these are not necessary
constraints. We can have RESTful Web Services which are not using JSON and which are not over
HTTP.
Level 0 : Expose SOAP web services in REST style. Expose action based services
(http://server/getPosts, http://server/deletePosts, http://server/doThis,
http://server/doThat etc) using REST.
Level 1 : Expose Resources with proper URIs (using nouns). Ex: http://server/accounts,
http://server/accounts/10. However, HTTP Methods are not used.
Level 2 : Resources use proper URI's + HTTP Methods. For example, to update an account,
you do a PUT to . The create an account, you do a POST to . Uris look like
posts/1/comments/5 and accounts/1/friends/1.
Level 3 : HATEOAS (Hypermedia as the engine of application state). You will tell not only
about the information being requested but also about the next possible actions that the
service consumer can do. When requesting information about a facebook user, a REST
service can return user details along with information about how to get his recent posts,
how to get his recent comments and how to retrieve his friends list.
What are the best practices in designing RESTful APIs?
While designing any API, the most important thing is to think about the api consumer i.e.
the client who is going to use the service. What are his needs? Does the service uri make
sense to him? Does the request, response format make sense to him?
In Rest, we think Nouns (resources) and NOT Verbs (NOT actions). So, URIs should
represent resources. URIs should be hierarchical and as self descriptive as possible. Prefer
plurals.
Always use HTTP Methods. Best practices with respect to each HTTP method is described in
the next question.
What are the best practices in using HTTP methods with Restful Web Services?
GET : Should not update anything. Should be idempotent (same result in multiple calls).
Possible Return Codes 200 (OK) + 404 (NOT FOUND) +400 (BAD REQUEST)
POST : Should create new resource. Ideally return JSON with link to newly created resource.
Same return codes as get possible. In addition : Return code 201 (CREATED) is possible.
PUT : Update a known resource. ex: update client details. Possible Return Codes : 200(OK)
DELETE : Used to delete a resource.
Important Annotations:
@ApplicationPath("/"). @Path("users") : used on class and methods to define the url path.
@GET @POST : Used to define the HTTP method that invokes the method.
@Produces(MediaType.APPLICATION_JSON) : Defines the output format of Restful service.
@Path("/{id}") on method (and) @PathParam("id") on method parameter : This helps in
defining a dynamic parameter in Rest URL. @Path("{user_id}/followers/{follower_id}") is a
more complicated example.
@QueryParam("page") : To define a method parameter ex: /users?page=10.
Useful methods:
All comparison is between the Sample Restful and SOAP implementations described above.
REST is built over simple HTTP protocol. SOAP services are more complex to implement
and more complex to consume.
REST has better performance and scalability. REST reads can be cached, SOAP based reads
cannot be cached.
REST permits many different data formats (JSON is the most popular choice) whereas SOAP
only permits XML.
SOAP services have well defined structure and interface (WSDL).
SOAP is based on well-defined standards (WS-Security, WS-AtomicTransaction and WS-
ReliableMessaging).
HashSet No No No No Yes No
TreeSet Yes No No No No No