Spring 4 MVC REST Esempio Helloworld

REST, o Representional State Transfer, è una architettura usata per il design dei web  services. L’idea di base è che invece di usare delle tecnologie come CORBA, RPC o SOAP per far dialogare due applicazioni, si può usare semplicemente l’HTTP.
Quindi ipotizzando di avere un web services relativo ad un oggetto bean generico si ha:
HTTP POST —> Crea bean
HTTP GET —> Visualizza bean
HTTP PUT —> Update bean
HTTP DELETE –> Delete bean
In genere, la response è in formato JSON o XML.
Vediamo come integrare la tecnologia REST con Spring 4 MVC

Tecnologie Usate

apache tomcat 7.068
Spring 4.2.0.RELEASE

 

Creazione Progetto

Dal meù File/New/Other  cliccare su “Maven Project” spring-mvc-file-upload-1

Selezionare  “Create a simple project (skip archetype selection)

spring-mvc-file-upload-31

spring-mvc-file-upload-32

Inserire i seguenti valori:
Group ID: iljavarolo
Artifact ID: Spring4MVCRest
Packaging: war

Cliccare su “Finish

Il progetto creato dovrebbe avere una struttura del genere:

spring-mvc-file-upload-34

Se non esiste la cartella WEB-INF crearla.

Configurazione Spring

 

pom.xml

[xml]

<project xmlns=”http://maven.apache.org/POM/4.0.0″ xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”>
<modelVersion>4.0.0</modelVersion>
<groupId>iljavarolo</groupId>
<artifactId>Spring4MVCRest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!– Servlet API –>
<!– http://mvnrepository.com/artifact/javax.servlet/javax.servlet-api –>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!– Jstl for jsp page –>
<!– http://mvnrepository.com/artifact/javax.servlet/jstl –>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!– JSP API –>
<!– http://mvnrepository.com/artifact/javax.servlet.jsp/jsp-api –>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.4.1</version>
</dependency>

<!– Spring dependencies –>
<!– http://mvnrepository.com/artifact/org.springframework/spring-core –>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>

<!– http://mvnrepository.com/artifact/org.springframework/spring-web –>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>

<!– http://mvnrepository.com/artifact/org.springframework/spring-webmvc –>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>

</dependencies>

<build>
<finalName>Spring4MVCRest</finalName>
<plugins>

<!– Config: Maven Tomcat Plugin –>
<!– http://mvnrepository.com/artifact/org.apache.tomcat.maven/tomcat7-maven-plugin –>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>

</plugin>
</plugins>
</build>
</project>

[/xml]

web.xml

[xml]

<web-app xmlns=”http://java.sun.com/xml/ns/javaee”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd”
version=”3.0″>

<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<!– Other XML Configuration –>
<!– Load by Spring ContextLoaderListener –>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/root-context.xml
</param-value>
</context-param>

<!– Spring ContextLoaderListener –>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

</web-app>
[/xml]

da notare che nel web.xml si definiscono i due file di configurazione di Spring

  •          /WEB-INF/spring-security.xml
  •          /WEB-INF/root-context.xml

Inoltre, la DispatcherServlet di Spring leggerà il suo file di configurazione in base al principio {servlet-name} ==> /WEB-INF/{servlet-name}-servlet.xml, quindi in questo caso la servlet name vale “spring-mvc”   e il file avrà il nome di “spring-mvc-servlet.xmlspring-mvc-file-upload-38

root-context.xml

[xml]

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

<!– Empty –>

</beans>

[/xml]
spring-mvc-servlet.xml
[xml]

<?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:p=”http://www.springframework.org/schema/p”
xmlns:context=”http://www.springframework.org/schema/context”
xmlns:mvc=”http://www.springframework.org/schema/mvc”
xsi:schemaLocation=”http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd”>

<!– Package Scan –>
<context:component-scan base-package=”com” />

<!– Enables the Spring MVC Annotation Configuration –>
<context:annotation-config />

<!– Important!! –>
<!– Default Servlet Handler (For Resources *.css, *.js, image,..) –>
<mvc:default-servlet-handler />
<mvc:annotation-driven />

<!– Config resource mapping –>
<mvc:resources mapping=”/styles/**” location=”/WEB-INF/resources/css/” />

<mvc:annotation-driven />
<bean id=”contentManager”
class=”org.springframework.web.accept.ContentNegotiationManagerFactoryBean”>
<property name=”favorPathExtension” value=”true”/>
<property name=”ignoreAcceptHeader” value=”true” />
<property name=”defaultContentType” value=”text/html” />
<property name=”useJaf” value=”false”/>
<property name=”mediaTypes”>
<map>
<entry key=”html” value=”text/html” />
<entry key=”json” value=”application/json” />
<entry key=”xml” value=”application/xml” />
</map>
</property>
</bean>
<bean id=”jspViewResolver”
class=”org.springframework.web.servlet.view.InternalResourceViewResolver”>
<property name=”prefix” value=”/WEB-INF/jsp/” />
<property name=”suffix” value=”.jsp” />
</bean>

</beans>

 

[/xml]

BackEnd

E’ presente un unico controller, in cui ci sono tutte le funzioni CRUD relative al bean User esposte tramite web service REST.
HelloWorldRestController.java
[java]
package com.iljavarolo;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import com.iljavarolo.model.User;
import com.iljavarolo.service.UserService;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.util.UriComponentsBuilder;
import java.util.ArrayList;
import java.util.Iterator;

@RestController
public class HelloWorldRestController {

@Autowired
UserService userService; //Service which will do all data retrieval/manipulation work

//——————-Retrieve All Users——————————————————–
@RequestMapping(value = “/user”, method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
public List getUser1() {
List users = userService.findAllUsers();

return users;
}

//——————-Retrieve Single User——————————————————–

@RequestMapping(value = “/user/{id}”, method = RequestMethod.GET)
public ResponseEntity getUser(@PathVariable(“id”) long id) {
System.out.println(“Fetching User with id ” + id);
User user = userService.findById(id);
if (user == null) {
System.out.println(“User with id ” + id + ” not found”);
return new ResponseEntity(HttpStatus.NOT_FOUND);
}
return new ResponseEntity(user, HttpStatus.OK);
}

//——————-Create a User——————————————————–

@RequestMapping(value = “/user”, method = RequestMethod.POST)
public ResponseEntity createUser(@RequestBody User user, UriComponentsBuilder ucBuilder) {
System.out.println(“Creating User ” + user.getName());

if (userService.isUserExist(user)) {
System.out.println(“A User with name ” + user.getName() + ” already exist”);
return new ResponseEntity(HttpStatus.CONFLICT);
}

userService.saveUser(user);

HttpHeaders headers = new HttpHeaders();
headers.setLocation(ucBuilder.path(“/user/{id}”).buildAndExpand(user.getId()).toUri());
return new ResponseEntity(headers, HttpStatus.CREATED);
}

//——————- Update a User ——————————————————–

@RequestMapping(value = “/user/{id}”, method = RequestMethod.PUT)
public User updateUser(@PathVariable(“id”) long id, @RequestBody User user) {
System.out.println(“Updating User ” + id);

User currentUser = userService.findById(id);

if (currentUser==null) {
System.out.println(“User with id ” + id + ” not found”);
return new User();
}

currentUser.setName(user.getName());
currentUser.setAge(user.getAge());
currentUser.setSalary(user.getSalary());

userService.updateUser(currentUser);
return currentUser;
}

//——————- Delete a User ——————————————————–

@RequestMapping(value = “/user/{id}”, method = RequestMethod.DELETE)
public User deleteUser(@PathVariable(“id”) long id) {
System.out.println(“Fetching & Deleting User with id ” + id);

User user = userService.findById(id);
if (user == null) {
System.out.println(“Unable to delete. User with id ” + id + ” not found”);
return new User();
}

userService.deleteUserById(id);
return new User();
}

//——————- Delete All Users ——————————————————–

@RequestMapping(value = “/user/”, method = RequestMethod.DELETE)
public User deleteAllUsers() {
System.out.println(“Deleting All Users”);

userService.deleteAllUsers();
return new User();
}

}
[/java]

Con @RestController si indica a Spring che si tratta di un controller Rest.
Per ogni metodo viene specificato il modo con cui viene invocato (GET, PUT etc)

User.java
[java]
package com.iljavarolo.model;

public class User {

private long id;

private String name;

private int age;

private double salary;

public User(){
id=0;
}

public User(long id, String name, int age, double salary){
this.id = id;
this.name = name;
this.age = age;
this.salary = salary;
}

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public double getSalary() {
return salary;
}

public void setSalary(double salary) {
this.salary = salary;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (int) (id ^ (id >>> 32));
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (id != other.id)
return false;
return true;
}

@Override
public String toString() {
return “User [id=” + id + “, name=” + name + “, age=” + age
+ “, salary=” + salary + “]”;
}

}
[/java]

UserService.java
[java]
package com.iljavarolo.service;

import java.util.List;

import com.iljavarolo.model.User;

public interface UserService {

User findById(long id);

User findByName(String name);

void saveUser(User user);

void updateUser(User user);

void deleteUserById(long id);

List findAllUsers();

void deleteAllUsers();

public boolean isUserExist(User user);

}
[/java]

UserServiceImpl.java
[java]
package com.iljavarolo.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.iljavarolo.model.User;

@Service(“userService”)
@Transactional
public class UserServiceImpl implements UserService{

private static final AtomicLong counter = new AtomicLong();

private static List users;

static{
users= populateDummyUsers();
}

public List findAllUsers() {
return users;
}

public User findById(long id) {
for(User user : users){
if(user.getId() == id){
return user;
}
}
return null;
}

public User findByName(String name) {
for(User user : users){
if(user.getName().equalsIgnoreCase(name)){
return user;
}
}
return null;
}

public void saveUser(User user) {
user.setId(counter.incrementAndGet());
users.add(user);
}

public void updateUser(User user) {
int index = users.indexOf(user);
users.set(index, user);
}

public void deleteUserById(long id) {

for (Iterator iterator = users.iterator(); iterator.hasNext(); ) {
User user = iterator.next();
if (user.getId() == id) {
iterator.remove();
}
}
}

public boolean isUserExist(User user) {
return findByName(user.getName())!=null;
}

private static List populateDummyUsers(){
List users = new ArrayList();
users.add(new User(counter.incrementAndGet(),”Sam”,30, 70000));
users.add(new User(counter.incrementAndGet(),”Tom”,40, 50000));
users.add(new User(counter.incrementAndGet(),”Jerome”,45, 30000));
users.add(new User(counter.incrementAndGet(),”Silvia”,50, 40000));
return users;
}

public void deleteAllUsers() {
users.clear();
}

}
[/java]

Client per poter invocare i servizi Rest
SpringRestTestClient.java
[java]
package com.iljavarolo.client;

import java.net.URI;
import java.util.LinkedHashMap;
import java.util.List;

import org.springframework.web.client.RestTemplate;

import com.iljavarolo.model.User;

public class SpringRestTestClient {

public static final String REST_SERVICE_URI = “http://localhost:8080/Spring4MVCRest”;

/* GET */
@SuppressWarnings(“unchecked”)
private static void listAllUsers(){
System.out.println(“Testing listAllUsers API———–“);

RestTemplate restTemplate = new RestTemplate();
List<LinkedHashMap<String, Object>> usersMap = restTemplate.getForObject(REST_SERVICE_URI+”/user/”, List.class);

if(usersMap!=null){
for(LinkedHashMap<String, Object> map : usersMap){
System.out.println(“User : id=”+map.get(“id”)+”, Name=”+map.get(“name”)+”, Age=”+map.get(“age”)+”, Salary=”+map.get(“salary”));;
}
}else{
System.out.println(“No user exist———-“);
}
}

/* GET */
private static void getUser(){
System.out.println(“Testing getUser API———-“);
RestTemplate restTemplate = new RestTemplate();
User user = restTemplate.getForObject(REST_SERVICE_URI+”/user/1”, User.class);
System.out.println(user);
}

/* POST */
private static void createUser() {
System.out.println(“Testing create User API———-“);
RestTemplate restTemplate = new RestTemplate();
User user = new User(0,”Sarah”,51,134);
URI uri = restTemplate.postForLocation(REST_SERVICE_URI+”/user/”, user, User.class);
System.out.println(“Location : “+uri.toASCIIString());
}

/* PUT */
private static void updateUser() {
System.out.println(“Testing update User API———-“);
RestTemplate restTemplate = new RestTemplate();
User user = new User(1,”Tomy”,33, 70000);
restTemplate.put(REST_SERVICE_URI+”/user/1″, user);
System.out.println(user);
}

/* DELETE */
private static void deleteUser() {
System.out.println(“Testing delete User API———-“);
RestTemplate restTemplate = new RestTemplate();
restTemplate.delete(REST_SERVICE_URI+”/user/3”);
}

/* DELETE */
private static void deleteAllUsers() {
System.out.println(“Testing all delete Users API———-“);
RestTemplate restTemplate = new RestTemplate();
restTemplate.delete(REST_SERVICE_URI+”/user/”);
}

public static void main(String args[]){
listAllUsers();
getUser();
createUser();
listAllUsers();
updateUser();
listAllUsers();
deleteUser();
listAllUsers();
deleteAllUsers();
listAllUsers();
}
}
[/java]

Build  Applicazione

Buildare l’intero progetto, cliccando su run As–>Maven Install

spring-mvc-file-upload-10

spring-mvc-file-upload-11

Dal menù “Run Configuration” settare la seguente configurazione per l’avvio:

spring-mvc-file-upload-12

spring-mvc-file-upload-13

inserire i seguenti valori:
Name: Run Spring4MVCRest
Base directory: ${workspace_loc:/Spring4MVCRest}
Goals: tomcat7:run

Spring Security Login Esempio 14

Buildare l’intero progetto cliccando su “Run“:

Spring Security Login Esempio 15

Avvio Applicazione

Elenco degli user
Digitare sul browser
http://localhost:8080/Spring4MVCRest/user

Spring4MVCRest_1

Dettagli user con id 2
Digitare:
http://localhost:8080/Spring4MVCRest/user/1
Spring4MVCRest_2

per testare le altre funzioni installare Postman un plugin di chrome

POSTMAN
Postman

 

Creazione User
Avviare Postman. Selezionare POST e nel tag body scegliere come type [application/json].

Spring4MVC Rest 4
Nel body copiare il seguente JSON
[json]

{
“name”:”prova”,”age”:50,”salary”:50000.0
}

[/json]

POSTMAN aggiunge automaticamnente un header Content-Type in cui indica il tipo di dati.
Spring4MVC Rest 5

premere Send

Lo user è stato creato. Per verificare digitare sul browser:
http://localhost:8080/Spring4MVCRest/user/5
Spring4MVCRest_2

Update User
Avviare Postman. Selezionare PUT

Nel body copiare il seguente JSON
[json]

{
“name”:”prova”,”age”:40,”salary”:50000.0
}

[/json]

6

Test tramite client Java
eseguire il main della classe SpringRestTestClient
11

 

Download sorgenti intero progetto
icona_zip

(Visited 909 times, 1 visits today)

Lascia un commento

Il tuo indirizzo email non sarà pubblicato.