reset password

REST API Using Spring and Hibernate

2. Spring Framework

Make the following changes to the Maven web application we created in the previous step:

1. Add the following dependencies:

  • org.springframework:spring-webmvc:5.0.9.RELEASE
  • com.fasterxml.jackson.core:jackson-databind:2.9.6
  • javax.xml.bind:jaxb-api:2.3.0
  • com.sun.xml.bind:jaxb-core:2.3.0.1
  • com.sun.xml.bind:jaxb-impl:2.3.0.1

If you want to produce/consume resources in XML format, replace the jackson-databind dependency with com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.9.6.

2. Add the following to web.xml:

    <servlet>
        <servlet-name>springrest</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>springrest</servlet-name>
        <url-pattern>/api/*</url-pattern>
    </servlet-mapping>

This specifies a Spring servlet DispatcherServlet which will be loaded when the web application is started (notice the <load-on-startup>), and this servlet is responsible for initializing the Spring servlet context, i.e. creating all the beans specified in a bean configuration file. The default name for the bean configuration file is <dispatcher-servlet-name>-servlet.xml under the WEB-INF folder. In our case, the name of the DispatcherServlet is springrest (notice the <servlet-name>) so the bean configuration file is springrest-servlet.xml. During operation DispatcherServlet also serves as the front controller that dispatches requests to the proper controller based on controller URL mapping.

3. Create the Spring bean configuration file springrest-servlet.xml under WEB-INF as follows:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd  
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
        </mvc:message-converters>
    </mvc:annotation-driven>

    <context:component-scan base-package="springrest.api" />

</beans>

<mvc:annotation-driven> enables request dispatching to @Controller among some other things; it also includes a Jackson converter that automatically converts between Java and JSON objects . <context:component-scan> finds all the annotated beans in springrest.api and its sub-packages.

4. Add the following code to the project:

Run the project and use a browser to open the URL http://localhost:8080/springrest/api/test - you should see a JSON response like the following:

{"id":1,"username":"cysun","password":null,"enabled":true,"roles":[{"id":1,"name":"Admin"}]}

This page has been viewed 4061 times.