This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Entity | |
public class Hero { | |
@Id | |
@GeneratedValue(strategy = GenerationType.AUTO) | |
private Long id; | |
private String name; | |
public Long getId() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.navan.heroesbackend; | |
import org.springframework.data.jpa.repository.Query; | |
import org.springframework.data.repository.CrudRepository; | |
import org.springframework.data.repository.query.Param; | |
import org.springframework.data.rest.core.annotation.RestResource; | |
public interface HeroRepository extends CrudRepository<Hero, Long> { | |
@RestResource(path = "name", rel="name") | |
@Query("from Hero h where lower(h.name) like CONCAT('%', lower(:contains), '%')") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
insert into hero(name) values('Ms Nice'); | |
insert into hero(name) values('Yes Man'); | |
insert into hero(name) values('Bombastico'); | |
insert into hero(name) values('Fennel'); | |
insert into hero(name) values('Magnostic'); | |
insert into hero(name) values('Duck Man'); | |
insert into hero(name) values('Silly Putty Man'); | |
insert into hero(name) values('Company Man'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.navan.heroesbackend; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.data.rest.core.config.RepositoryRestConfiguration; | |
import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurerAdapter; | |
import io.navan.heroesbackend.Hero; | |
@Configuration | |
public class RepositoryConfig extends RepositoryRestConfigurerAdapter { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.navan.heroesbackend; | |
import java.util.Arrays; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.web.cors.CorsConfiguration; | |
import org.springframework.web.cors.UrlBasedCorsConfigurationSource; | |
import org.springframework.web.filter.CorsFilter; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.navan.heroesbackend; | |
import org.springframework.boot.SpringApplication; | |
import org.springframework.boot.autoconfigure.SpringBootApplication; | |
import org.springframework.boot.autoconfigure.data.rest.RepositoryRestMvcAutoConfiguration; | |
@SpringBootApplication(exclude = RepositoryRestMvcAutoConfiguration.class) | |
public class HeroesBackendApplication { | |
public static void main(String[] args) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@PostMapping(consumes = "application/json", produces = "application/json") | |
@ResponseStatus(HttpStatus.CREATED) | |
public Hero createHero(Hero hero) { | |
LOG.debug("createHero: {}", hero.getName()); | |
Hero createdHero = heroRepository.save(hero); | |
LOG.debug("Created hero {} with id {}", createdHero.getName(), createdHero.getId()); | |
return createdHero; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@PostMapping(consumes = "application/json", produces = "application/json") | |
@ResponseStatus(HttpStatus.CREATED) | |
public Hero createHero(Hero hero) { | |
LOG.debug("createHero: {}", hero.getName()); | |
Hero createdHero = heroRepository.save(hero); | |
LOG.debug("Created hero {} with id {}", createdHero.getName(), createdHero.getId()); | |
return createdHero; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.navan.heroesbackend; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.dao.EmptyResultDataAccessException; | |
import org.springframework.data.rest.webmvc.ResourceNotFoundException; | |
import org.springframework.http.HttpStatus; | |
import org.springframework.web.bind.annotation... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RunWith(SpringRunner.class) | |
@SpringBootTest | |
@AutoConfigureMockMvc | |
@ActiveProfiles("test") | |
public class HeroesBackendApplicationTests { | |
private static final String BASE_URL = "/heroes/"; | |
private static final Logger LOG = LoggerFactory.getLogger(HeroesBackendApplicationTests.class); | |
@Autowired |