Skip to content

Commit 7b257e4

Browse files
committed
Merge remote-tracking branch 'java/subdir' into java-app
Signed-off-by: Tom Wilkie <[email protected]>
2 parents aa18d48 + 5ff6aca commit 7b257e4

File tree

10 files changed

+356
-0
lines changed

10 files changed

+356
-0
lines changed

app/.gitapp/ignore

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
4+
### STS ###
5+
.apt_generated
6+
.classpath
7+
.factorypath
8+
.project
9+
.settings
10+
.springBeans
11+
.sts4-cache
12+
13+
### IntelliJ IDEA ###
14+
.idea
15+
*.iws
16+
*.iml
17+
*.ipr
18+
19+
### NetBeans ###
20+
/nbproject/private/
21+
/build/
22+
/nbbuild/
23+
/dist/
24+
/nbdist/
25+
/.nb-gradle/

app/pom.xml

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.example</groupId>
7+
<artifactId>TianMiao</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>war</packaging>
10+
11+
<name>TianMiao</name>
12+
<description>Demo project for Online Shopping</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>2.0.4.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>10</java.version>
25+
</properties>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-data-jpa</artifactId>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-web</artifactId>
35+
<exclusions>
36+
<exclusion>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-starter-tomcat</artifactId>
39+
</exclusion>
40+
</exclusions>
41+
</dependency>
42+
<dependency>
43+
<groupId>org.springframework.boot</groupId>
44+
<artifactId>spring-boot-starter-tomcat</artifactId>
45+
<scope>provided</scope>
46+
</dependency>
47+
<dependency>
48+
<groupId>org.springframework.boot</groupId>
49+
<artifactId>spring-boot-devtools</artifactId>
50+
<scope>runtime</scope>
51+
</dependency>
52+
<dependency>
53+
<groupId>mysql</groupId>
54+
<artifactId>mysql-connector-java</artifactId>
55+
<scope>runtime</scope>
56+
</dependency>
57+
<dependency>
58+
<groupId>org.springframework.boot</groupId>
59+
<artifactId>spring-boot-starter-test</artifactId>
60+
<scope>test</scope>
61+
</dependency>
62+
<dependency>
63+
<groupId>javax.xml.bind</groupId>
64+
<artifactId>jaxb-api</artifactId>
65+
</dependency>
66+
<dependency>
67+
<groupId>javax.servlet</groupId>
68+
<artifactId>javax.servlet-api</artifactId>
69+
</dependency>
70+
<dependency>
71+
<groupId>org.javassist</groupId>
72+
<artifactId>javassist</artifactId>
73+
<version>3.25.0-GA</version>
74+
</dependency>
75+
</dependencies>
76+
77+
<build>
78+
<finalName>${artifactId}</finalName>
79+
<plugins>
80+
<plugin>
81+
<groupId>org.springframework.boot</groupId>
82+
<artifactId>spring-boot-maven-plugin</artifactId>
83+
</plugin>
84+
</plugins>
85+
</build>
86+
87+
88+
</project>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.example.TianMiao;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.web.filter.CommonsRequestLoggingFilter;
6+
7+
@Configuration
8+
public class RequestLoggingFilterConfig {
9+
10+
@Bean
11+
public CommonsRequestLoggingFilter logFilter() {
12+
CommonsRequestLoggingFilter filter
13+
= new CommonsRequestLoggingFilter();
14+
filter.setIncludeQueryString(true);
15+
filter.setIncludeHeaders(true);
16+
filter.setIncludeClientInfo(true);
17+
return filter;
18+
}
19+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.example.TianMiao;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.boot.builder.SpringApplicationBuilder;
6+
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
7+
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
8+
9+
@SpringBootApplication
10+
@EnableJpaAuditing
11+
public class TianMiaoApplication extends SpringBootServletInitializer {
12+
13+
@Override
14+
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
15+
return application.sources(TianMiaoApplication.class);
16+
}
17+
18+
public static void main(String[] args) {
19+
SpringApplication.run(TianMiaoApplication.class, args);
20+
}
21+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.example.TianMiao.controller;
2+
3+
import java.util.List;
4+
import java.util.Map;
5+
6+
import javax.validation.Valid;
7+
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.http.ResponseEntity;
10+
import org.springframework.web.bind.annotation.DeleteMapping;
11+
import org.springframework.web.bind.annotation.GetMapping;
12+
import org.springframework.web.bind.annotation.PathVariable;
13+
import org.springframework.web.bind.annotation.PostMapping;
14+
import org.springframework.web.bind.annotation.PutMapping;
15+
import org.springframework.web.bind.annotation.RequestBody;
16+
import org.springframework.web.bind.annotation.RequestMapping;
17+
import org.springframework.web.bind.annotation.RestController;
18+
19+
import com.example.TianMiao.exception.ResourceNotFoundException;
20+
import com.example.TianMiao.model.User;
21+
import com.example.TianMiao.repository.UserRepository;
22+
23+
@RestController
24+
@RequestMapping("/api")
25+
public class UserController {
26+
@Autowired
27+
UserRepository userRepository;
28+
29+
@GetMapping("/users")
30+
public List<User> getAllUsers() {
31+
return userRepository.findAll();
32+
}
33+
34+
@PostMapping("/users")
35+
public User createUser(@Valid @RequestBody User user) {
36+
return userRepository.save(user);
37+
}
38+
39+
@GetMapping("/users/{id}")
40+
public User getUserById(@PathVariable(value = "id") Long userId) {
41+
return userRepository.findById(userId)
42+
.orElseThrow(() -> new ResourceNotFoundException("User", "id", userId));
43+
}
44+
45+
@PutMapping("/notes/{id}")
46+
public User updateUser(@PathVariable(value = "id") Long userId, @Valid @RequestBody User userDetails) {
47+
User user = userRepository.findById(userId)
48+
.orElseThrow(() -> new ResourceNotFoundException("User", "id", userId));
49+
50+
user.setUsername(userDetails.getUsername());
51+
User updatedUser = userRepository.save(user);
52+
return updatedUser;
53+
}
54+
55+
@DeleteMapping("/notes/{id}")
56+
public ResponseEntity<?> deleteUser(@PathVariable(value = "id") Long userId) {
57+
User user = userRepository.findById(userId)
58+
.orElseThrow(() -> new ResourceNotFoundException("User", "id", userId));
59+
userRepository.delete(user);
60+
return ResponseEntity.ok().build();
61+
}
62+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.example.TianMiao.exception;
2+
3+
import org.springframework.http.HttpStatus;
4+
import org.springframework.web.bind.annotation.ResponseStatus;
5+
6+
@ResponseStatus(value = HttpStatus.NOT_FOUND)
7+
public class ResourceNotFoundException extends RuntimeException {
8+
private String resourceName;
9+
private String fieldName;
10+
private Object fieldValue;
11+
12+
public ResourceNotFoundException( String resourceName, String fieldName, Object fieldValue) {
13+
super(String.format("%s not found with %s : '%s'", resourceName, fieldName, fieldValue));
14+
this.resourceName = resourceName;
15+
this.fieldName = fieldName;
16+
this.fieldValue = fieldValue;
17+
}
18+
19+
public String getResourceName() {
20+
return resourceName;
21+
}
22+
23+
public String getFieldName() {
24+
return fieldName;
25+
}
26+
27+
public Object getFieldValue() {
28+
return fieldValue;
29+
}
30+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.example.TianMiao.model;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
import org.springframework.data.annotation.CreatedDate;
5+
import org.springframework.data.annotation.LastModifiedDate;
6+
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
7+
import javax.persistence.*;
8+
import javax.validation.constraints.NotBlank;
9+
10+
import java.io.Serializable;
11+
import java.util.Date;
12+
13+
@Entity
14+
@Table(name = "user")
15+
@EntityListeners(AuditingEntityListener.class)
16+
@JsonIgnoreProperties(value = {"createdAt", "updatedAt"}, allowGetters = true)
17+
public class User implements Serializable {
18+
/**
19+
*
20+
*/
21+
private static final long serialVersionUID = 1L;
22+
23+
@Id
24+
@GeneratedValue(strategy = GenerationType.IDENTITY)
25+
private Long id;
26+
27+
@NotBlank
28+
private String username;
29+
30+
@Column(nullable = false, updatable = false)
31+
@Temporal(TemporalType.TIMESTAMP)
32+
@CreatedDate
33+
private Date createdAt;
34+
35+
@Column(nullable = false)
36+
@Temporal(TemporalType.TIMESTAMP)
37+
@LastModifiedDate
38+
private Date updatedAt;
39+
40+
public Long getId() {
41+
return id;
42+
}
43+
44+
public void setId(Long id) {
45+
this.id = id;
46+
}
47+
48+
public String getUsername() {
49+
return username;
50+
}
51+
52+
public void setUsername(String username) {
53+
this.username = username;
54+
}
55+
56+
public Date getCreatedAt() {
57+
return createdAt;
58+
}
59+
60+
public void setCreatedAt(Date createdAt) {
61+
this.createdAt = createdAt;
62+
}
63+
64+
public Date getUpdatedAt() {
65+
return updatedAt;
66+
}
67+
68+
public void setUpdatedAt(Date updatedAt) {
69+
this.updatedAt = updatedAt;
70+
}
71+
72+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.example.TianMiao.repository;
2+
3+
import org.springframework.data.jpa.repository.JpaRepository;
4+
import org.springframework.stereotype.Repository;
5+
6+
import com.example.TianMiao.model.User;
7+
8+
@Repository
9+
public interface UserRepository extends JpaRepository<User, Long> {
10+
11+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
## Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
2+
spring.datasource.url = jdbc:mysql://mysql/TianMiao?allowPublicKeyRetrieval=true&useSSL=false
3+
spring.datasource.username = root
4+
spring.datasource.password = password
5+
6+
7+
## Hibernate Properties
8+
# The SQL dialect makes Hibernate generate better SQL for the chosen database
9+
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
10+
11+
# Hibernate ddl auto (create, create-drop, validate, update)
12+
spring.jpa.hibernate.ddl-auto = update
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.example.TianMeow;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.springframework.boot.test.context.SpringBootTest;
6+
import org.springframework.test.context.junit4.SpringRunner;
7+
8+
@RunWith(SpringRunner.class)
9+
@SpringBootTest
10+
public class TianMeowApplicationTests {
11+
12+
@Test
13+
public void contextLoads() {
14+
}
15+
16+
}

0 commit comments

Comments
 (0)