upgrade bootstrap project

This commit is contained in:
Loredana Crusoveanu 2018-05-08 22:04:21 +03:00
parent 88a3f6c0fe
commit 08c0b590b0
6 changed files with 34 additions and 19 deletions

View File

@ -9,10 +9,10 @@
<description>Demo project for Spring Boot</description> <description>Demo project for Spring Boot</description>
<parent> <parent>
<artifactId>parent-boot-5</artifactId> <artifactId>parent-boot-2</artifactId>
<groupId>com.baeldung</groupId> <groupId>com.baeldung</groupId>
<version>0.0.1-SNAPSHOT</version> <version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-boot-5</relativePath> <relativePath>../parent-boot-2</relativePath>
</parent> </parent>
<!-- add dependency management if you have a different parent pom <!-- add dependency management if you have a different parent pom

View File

@ -0,0 +1,20 @@
package org.baeldung.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.anyRequest()
.permitAll()
.and().csrf().disable();
}
}

View File

@ -8,5 +8,4 @@ import java.util.Optional;
public interface BookRepository extends CrudRepository<Book, Long> { public interface BookRepository extends CrudRepository<Book, Long> {
List<Book> findByTitle(String title); List<Book> findByTitle(String title);
Optional<Book> findOne(long id);
} }

View File

@ -6,6 +6,7 @@ import org.baeldung.web.exception.BookIdMismatchException;
import org.baeldung.web.exception.BookNotFoundException; import org.baeldung.web.exception.BookNotFoundException;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
@ -37,21 +38,22 @@ public class BookController {
@GetMapping("/{id}") @GetMapping("/{id}")
public Book findOne(@PathVariable long id) { public Book findOne(@PathVariable long id) {
return bookRepository.findOne(id) return bookRepository.findById(id)
.orElseThrow(BookNotFoundException::new); .orElseThrow(BookNotFoundException::new);
} }
@PostMapping @PostMapping
@ResponseStatus(HttpStatus.CREATED) @ResponseStatus(HttpStatus.CREATED)
public Book create(@RequestBody Book book) { public Book create(@RequestBody Book book) {
return bookRepository.save(book); Book book1 = bookRepository.save(book);
return book1;
} }
@DeleteMapping("/{id}") @DeleteMapping("/{id}")
public void delete(@PathVariable long id) { public void delete(@PathVariable long id) {
bookRepository.findOne(id) bookRepository.findById(id)
.orElseThrow(BookNotFoundException::new); .orElseThrow(BookNotFoundException::new);
bookRepository.delete(id); bookRepository.deleteById(id);
} }
@PutMapping("/{id}") @PutMapping("/{id}")
@ -59,7 +61,7 @@ public class BookController {
if (book.getId() != id) { if (book.getId() != id) {
throw new BookIdMismatchException(); throw new BookIdMismatchException();
} }
bookRepository.findOne(id) bookRepository.findById(id)
.orElseThrow(BookNotFoundException::new); .orElseThrow(BookNotFoundException::new);
return bookRepository.save(book); return bookRepository.save(book);
} }

View File

@ -7,10 +7,8 @@ spring.thymeleaf.enabled=true
spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html spring.thymeleaf.suffix=.html
security.basic.enabled=true spring.security.user.name=john
security.user.name=john spring.security.user.password=123
security.user.password=123
spring.datasource.driver-class-name=org.h2.Driver spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:bootapp;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE spring.datasource.url=jdbc:h2:mem:bootapp;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE

View File

@ -1,6 +1,5 @@
package org.baeldung; package org.baeldung;
import static io.restassured.RestAssured.preemptive;
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
import static org.apache.commons.lang3.RandomStringUtils.randomNumeric; import static org.apache.commons.lang3.RandomStringUtils.randomNumeric;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
@ -11,7 +10,6 @@ import io.restassured.response.Response;
import java.util.List; import java.util.List;
import org.baeldung.persistence.model.Book; import org.baeldung.persistence.model.Book;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
@ -24,11 +22,6 @@ import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest(classes = Application.class, webEnvironment = WebEnvironment.DEFINED_PORT) @SpringBootTest(classes = Application.class, webEnvironment = WebEnvironment.DEFINED_PORT)
public class SpringBootBootstrapIntegrationTest { public class SpringBootBootstrapIntegrationTest {
@Before
public void setUp() {
RestAssured.authentication = preemptive().basic("john", "123");
}
private static final String API_ROOT = "http://localhost:8081/api/books"; private static final String API_ROOT = "http://localhost:8081/api/books";
@Test @Test
@ -94,6 +87,8 @@ public class SpringBootBootstrapIntegrationTest {
final Book book = createRandomBook(); final Book book = createRandomBook();
final String location = createBookAsUri(book); final String location = createBookAsUri(book);
System.out.println(location);
book.setId(Long.parseLong(location.split("api/books/")[1])); book.setId(Long.parseLong(location.split("api/books/")[1]));
book.setAuthor("newAuthor"); book.setAuthor("newAuthor");
Response response = RestAssured.given() Response response = RestAssured.given()
@ -135,6 +130,7 @@ public class SpringBootBootstrapIntegrationTest {
.contentType(MediaType.APPLICATION_JSON_VALUE) .contentType(MediaType.APPLICATION_JSON_VALUE)
.body(book) .body(book)
.post(API_ROOT); .post(API_ROOT);
System.out.println(response.asString());
return API_ROOT + "/" + response.jsonPath() return API_ROOT + "/" + response.jsonPath()
.get("id"); .get("id");
} }