Spring Boot ist eher eine pragmatische Umsetzung des Spring Frameworks und bietet etliche Vereinfachungen.
Seit der Lancierung im Jahr 2013 mit Version 1.x wurde 2018 die Version 2.x freigegeben.
In diesem Blog lernen Sie die Key Features von Spring Boot 2 kennen und gewinnen damit eine kompakte Übersicht zu diesem effizienten Ökosystem.
package ch.std.hello.rest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
private static final String template = "Hello, %s!";
@GetMapping("/hello")
public String greeting(@RequestParam(value="name", defaultValue="World") String name) {
return String.format(template, name);
}
}
Der Maven oder Gradle Build erstellt ein ausführbares FAT Jar:java -jar springboothellomaven-0.0.1-SNAPSHOT.jar
Das FAT Jar entspricht der folgenden Struktur:Nach dem Startup finden wir unseren Hello REST Service unter der folgenden URL http://localhost:8080/hello:package ch.std.jumpstart.repository;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
...
import ch.std.jumpstart.jpa.City;
import ch.std.jumpstart.repository.BookRepository.IsbnTitleOnly;
@RunWith(SpringRunner.class)
@DataJpaTest
@ActiveProfiles("test")
public class CityRepositoryIntegrationTest {
@Autowired
private CityRepository cityRepository;
private City city;
private List<City> cityList;
@Before
public void setup() {
city = new City("Bern");
this.cityRepository.save(city);
this.cityList = new Array<>();
for (int i = 0; i < 10; i++) {
City localCity = new City("city-" + i);
this.cityRepository.save(localCity);
this.cityList.add(localCity);
}
}
@After
public void tearDown() {
this.cityRepository.deleteAllInBatch();
}
@Test
public void testFindById() {
City foundCity = this.cityRepository.findById(city.getId()).orElse(null);
assertNotNull(foundCity);
assertEquals(this.city, foundCity);
}
@Test
public void testFindByName() {
City foundCity = this.cityRepository.findByName("Bern").orElse(null);
assertNotNull(foundCity);
assertEquals(this.city, foundCity);
}
}
package ch.std.jumpstart;
...
import ch.std.jumpstart.jpa.City;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@ActiveProfiles("test")
public class CityControllerTests {
@LocalServerPort
private int port;
@Autowired
private ServletContext servletContext;
@Autowired
private TestRestTemplate restTemplate;
private List<City> citiesList = new ArrayList<>();
@Before
public void setup() {
this.citiesList.clear();
String postUrl = this.getUrl("/rest/city");
HttpEntity<City> request = new HttpEntity<>(new City("Bern"));
City bernCity = this.restTemplate.postForObject(postUrl, request, City.class);
this.citiesList.add(bernCity);
}
@Test
public void testGetCities() throws Exception {
String url = this.getUrl("/rest/cities?value=Bern");
City[] cities = this.restTemplate.getForObject(url, City[].class);
assertNotNull(cities);
assertEquals(1, cities.length);
City city = cities[0];
assertNotNull(city);
assertEquals("Bern", city.getName());
}
@Test
public void testGetAllCities() throws Exception {
String url = this.getUrl("/rest/cities");
City[] cities = this.restTemplate.getForObject(url, City[].class);
assertNotNull(cities);
assertEquals(this.citiesList.size(), cities.length);
for (City city : this.citiesList) {
assertTrue(citiesList.contains(city));
}
}
@Test
public void testGetCityById() throws Exception {
String url = this.getUrl("/rest/cities?value=Bern");
City[] cities = this.restTemplate.getForObject(url, City[].class);
assertNotNull(cities);
assertEquals(1, cities.length);
City city = cities[0];
String urlById = this.getUrl("/rest/city/" + city.getId());
City cityById = this.restTemplate.getForObject(urlById, City.class);
assertEquals(city, cityById);
}
public String getUrl(String path) {
return "http://localhost:" + port + servletContext.getContextPath() + path;
}
}