These slides are up on GitHub
jeffsheets.github.io/BuzzworthyJavaAlso a printable version | Open Printable PDF
@RestController
class GBR {
@RequestMapping("/")
String gbr() {
"Go Big Red! #Nebrasketball"
}
}
@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
public class PropertiesInitializer implements ApplicationContextInitializer {
public void initialize(ConfigurableApplicationContext applicationContext) {
ConfigurableEnvironment env = applicationContext.getEnvironment();
String[] activeProfiles = getActiveProfiles(env);
for (String profileName : activeProfiles) {
log.info("Loading properties for Spring Active Profile: {}", profileName);
try {
ResourcePropertySource propertySource = new ResourcePropertySource(profileName
+ "EnvProperties", "classpath:application-" + profileName + ".properties");
env.getPropertySources().addLast(propertySource);
} catch (IOException e) {
log.error("ERROR during environment properties setup - TRYING TO LOAD: " + profileName, e);
//Okay to silently fail here, as we might have profiles
// that do not have properties files (like dev1, dev2, etc)
}
}
}
public class SpringPropertiesConfig {
@PostConstruct
public void initializeDatabasePropertySourceUsage() {
MutablePropertySources propertySources = ((ConfigurableEnvironment) env).getPropertySources();
try {
//dataSource, Table Name, Key Column, Value Column
DatabaseConfiguration databaseConfiguration = new DatabaseConfiguration(dataSource(), "AppConfiguration", "propertyKey", "propertyValue");
//CommonsConfigurationFactoryBean comes from https://java.net/projects/springmodules/sources/svn/content/tags/release-0_8/projects/commons/src/java/org/springmodules/commons/configuration/CommonsConfigurationFactoryBean.java?rev=2110
//Per https://jira.spring.io/browse/SPR-10213 I chose to just copy the raw source into our project
CommonsConfigurationFactoryBean commonsConfigurationFactoryBean = new CommonsConfigurationFactoryBean(databaseConfiguration);
Properties dbProps = (Properties) commonsConfigurationFactoryBean.getObject();
PropertiesPropertySource dbPropertySource = new PropertiesPropertySource("dbPropertySource", dbProps);
//By being First, Database Properties take precedence over all other properties that have the same key name
//You could put this last, or just in front of the application.properties if you wanted to...
propertySources.addFirst(dbPropertySource);
} catch (Exception e) {
log.error("Error during database properties setup", e);
throw new RuntimeException(e);
}
}
public interface AttendeeRepository extends JpaRepository {
List findByOrderByNameDesc();
List findByNameOrderByNameAsc(String name);
List findByOrderByNameAsc();
}
@RepositoryRestResource(collectionResourceRel = "people", path = "people")
public interface PersonRepository extends PagingAndSortingRepository {
List findByLastName(@Param("name") String name);
}
@ControllerAdvice
public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler({ Exception.class })
@ResponseBody
public ResponseEntity> handleAnyException(Exception e) {
return errorResponse(e, HttpStatus.INTERNAL_SERVER_ERROR);
}
@ExceptionHandler({ InvocationTargetException.class, IllegalArgumentException.class})
@ResponseBody
public ResponseEntity handleMiscFailures(Throwable t) {
return errorResponse(t, HttpStatus.BAD_REQUEST);
}
class AccountControllerTest extends Specification {
def accountController = new AccountController()
MockMvc mockMvc = standaloneSetup(accountController).build()
def "getAccount test hits the URL and parses JSON output"() {
when: 'rest account url is hit'
def response = mockMvc.perform(get('/rest/account')).andReturn().response
def content = new JsonSlurper().parseText(response.contentAsString)
then: 'securityService correctly returned account in JSON'
response.status == OK.value()
//Can test the whole content string that is returned
response.contentAsString == '{"username":"spockUser"}'
//Or can use the JsonSlurper version to test the JSON as object
content.username == 'spockUser'
}
}
npm install -g yo
npm install -g generator-jhipster
yo jhipster
gradle bootRun
grunt server
yo jhipster:entity location
//city, state, one-to-many attendee
yo jhipster:entity attendee
//name, previousAttendee, notes, many-to-one location
mvn -Pprod package
java -jar jhipster-0.0.1-SNAPSHOT.war --spring.profiles.active=prod