diff --git a/build.gradle b/build.gradle index 3212546..3956fdb 100644 --- a/build.gradle +++ b/build.gradle @@ -22,6 +22,7 @@ testImplementation('org.springframework.boot:spring-boot-starter-test') { exclude group: 'org.junit.vintage', module: 'junit-vintage-engine' } + compile("com.fasterxml.jackson.core:jackson-databind") } test { diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 9492014..e0b3fb8 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-6.0.1-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/src/main/java/com/example/springtest/demo/entities/User.java b/src/main/java/com/example/springtest/demo/entities/User.java new file mode 100644 index 0000000..d298436 --- /dev/null +++ b/src/main/java/com/example/springtest/demo/entities/User.java @@ -0,0 +1,42 @@ +package com.example.springtest.demo.entities; + +public class User { + public String uId; + public String name; + public String password; + public String uri; + + public User(String uId, String name, String password) { + this.uId = uId; + this.name = name; + this.password = password; + } + + public String getuId() { + return uId; + } + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public void setuId(String uId) { + this.uId = uId; + } + + public void setName(String name) { + this.name = name; + } + + public void setPassword(String password) { + this.password = password; + } + + public void setUri(String uri) { + this.uri = uri; + } +} diff --git a/src/main/java/com/example/springtest/demo/resources/UserRest.java b/src/main/java/com/example/springtest/demo/resources/UserRest.java new file mode 100644 index 0000000..aaa26eb --- /dev/null +++ b/src/main/java/com/example/springtest/demo/resources/UserRest.java @@ -0,0 +1,82 @@ +package com.example.springtest.demo.resources; + +import com.example.springtest.demo.entities.User; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.springframework.lang.Nullable; +import org.springframework.stereotype.Component; + +import javax.persistence.NoResultException; +import javax.ws.rs.*; +import javax.ws.rs.core.MediaType; +import java.util.*; + +@Component +@Path("/users") +public class UserRest { + + private Map users = new HashMap<>(); +// @GET +// public String getUser(@QueryParam("uId") String uId, @QueryParam("name") String name, @QueryParam("password") String password) { +// ObjectMapper objectMapper = new ObjectMapper(); +// String json = null; +// try { +// json = objectMapper.writeValueAsString(new User(uId, name, password)); +// } catch (JsonProcessingException e) { +// e.printStackTrace(); +// } +// return json; +// } + @GET + @Produces(MediaType.APPLICATION_JSON) // 戻り値をJSONで返す. + public Collection getUsers() { + return users.values(); // JSONが返る + } + + @Path("/{uId}") + @GET + @Produces(MediaType.APPLICATION_JSON) // 戻り値をJSONで返す. + public User getUser(@PathParam("uId") String uId) { + User user = users.get(uId); + if (user != null) { + return user; // JSONが返る + } else { // uIdが無ければ + throw new WebApplicationException(404); // 404が返る + } + } + + @POST + @Produces(MediaType.APPLICATION_JSON) + public User createUser(@FormParam("name") String name, @FormParam("password") String password) { + String uId = UUID.randomUUID().toString(); + User user = new User(uId, name, password); + users.put(uId, user); + return user; + } + + @Path("/{uId}") + @PUT + @Produces(MediaType.APPLICATION_JSON) + public User updateUser(@PathParam("uId") String uId, @Nullable @FormParam("name") String name, @Nullable @FormParam("password") String password) { + User user = users.get(uId); + if (user != null) { + user.setName(name); + user.setPassword(password); + return user; // JSONが返る + } else { // uIdが無ければ + throw new WebApplicationException(404); // 404が返る + } + } + + @Path("/{uId}") + @DELETE + @Produces(MediaType.APPLICATION_JSON) + public User deleteUser(@PathParam("uId") String uId) { + User user = users.remove(uId); + if (user != null) { + return user; // JSONが返る + } else { // uIdが無ければ + throw new WebApplicationException(410); // 410が返る(GONE) + } + } +}