Saturday, 14 September 2019

Spring and Rest

REST capability is built in to Spring MVC

➤use domain objects as parameters/return values
mark with @RequestBody/@ResponseBody
Spring MVC automatically handles XML/JSON Conversion
Based on converters available in class path

Demonstrate REST Controllers in Spring MVC
 - Additional domain objects
 - Automatic HTTP Message Conversion

changes in model class
create classes-

1- Team
package com.domain;

import java.util.Set;

public class Team {
  String name;
  String location;
  String id;
  Set<Player> players;
  
public Team(String name, String location, String id, Set<Player> players) {
super();
this.name = name;
this.location = location;
this.id = id;
this.players = players;
}
public Set<Player> getPlayers() {
return players;
}
public void setPlayers(Set<Player> players) {
this.players = players;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
  

}

2- Player
package com.domain;

public class Player {
   public Player(String name, String position) {
super();
this.name = name;
this.position = position;
}
String name;
   String position;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
   
}

Controller
                               InitialController

package com.demo;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.annotation.PostConstruct;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.domain.Player;
import com.domain.Team;

@Controller
public class InitialController {
    
private Team team;
@PostConstruct
public void init() {
Set<Player> players = new HashSet<>();
players.add(new Player("player1","A"));
players.add(new Player("player2","B"));
players.add(new Player("player3","C"));
team= new Team("xyz","India","p",players);
}
@RequestMapping("/hi")
public @ResponseBody Team helloWorld(Map model) {
return team;
}
 
}


DemoApplication

package com.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer{

public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(DemoApplication.class);
}
}

response-

{
"name": "xyz",
"location": "India",
"id": "p",
"players": [{
"name": "player3",
"position": "C"
}, {
"name": "player1",
"position": "A"
}, {
"name": "player2",
"position": "B"
}
]
}


Description    ::::::::

➤Controller returned a domain object
 NOT a logical view name(page)

➤Spring MVC noticed @ResponseBody
- or @RestController

➤ invoked correct HttpMesageConverter
-Based on

  •   Requested format
  •   JARS on classpath

what if i want XML 

➤Annotate domain class with JAXB annotation
JAXB already part of java SE

➤when app starts
Spring creates HttpMessageConverter for JAXB

  • based on classpath contents 


➤XML or JSON returned
based on requested format


@XmlRootElement
public class Team {
.....

}

http://localhost:8080/hi/


No comments:

Post a Comment

Note: only a member of this blog may post a comment.