Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 88 additions & 53 deletions blog-web/pom.xml
Original file line number Diff line number Diff line change
@@ -1,64 +1,99 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>codetest</artifactId>
<groupId>com.pierceecom.sample</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath />
</parent>
<artifactId>blog-web</artifactId>
<packaging>jar</packaging>
<name>blog-web</name>

<groupId>com.pierceecom.sample</groupId>
<artifactId>blog-web</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<name>blog-web</name>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax/javaee-api -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>8.0</version>
<scope>provided</scope>
</dependency>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.5.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.17</version>
<scope>test</scope>
</dependency>
</dependencies>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-processing</artifactId>
</dependency>
</dependencies>

<build>
<finalName>blog-web</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>

</plugins>
</build>
<build>
<finalName>blog-web</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
22 changes: 12 additions & 10 deletions blog-web/src/main/java/com/pierceecom/blog/HelloPierceResource.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@

package com.pierceecom.blog;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("hello-pierce")
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@RequestMapping("/blog-web")
public class HelloPierceResource {
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response hello() {
return Response.ok("{\"message\":\"Hello Pierce\"}").build();
}


@GetMapping(value = "/hello-pierce", produces = MediaType.APPLICATION_JSON_VALUE)
public Response hello() {
return Response.ok("{\"message\":\"Hello Pierce\"}").build();
}
}
25 changes: 10 additions & 15 deletions blog-web/src/main/java/com/pierceecom/blog/JAXRSConfiguration.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
package com.pierceecom.blog;

import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("/")
public class JAXRSConfiguration extends Application {
@Override
public Set<Class<?>> getClasses() {
HashSet<Class<?>> classes = new HashSet<>();
classes.add(HelloPierceResource.class);
return classes;
}
}
package com.pierceecom.blog;

import java.util.HashSet; import java.util.Set; import
javax.ws.rs.ApplicationPath; import javax.ws.rs.core.Application;

@ApplicationPath("/") public class JAXRSConfiguration extends Application {

@Override public Set<Class<?>> getClasses() { HashSet<Class<?>> classes = new
HashSet<>(); classes.add(HelloPierceResource.class); return classes; } }

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.pierceecom.blog;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;


@Configuration
@EnableAutoConfiguration
@SpringBootApplication
@ComponentScan
@EntityScan("com.pierceecom.blog.entity")
@EnableJpaRepositories("com.pierceecom.blog.repository")
public class PierceSampleApplication {

public static void main(String[] args) {
SpringApplication.run(PierceSampleApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.pierceecom.blog.controller;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.pierceecom.blog.entity.BlogPost;
import com.pierceecom.blog.payload.ApiResponse;
import com.pierceecom.blog.payload.BlogPostRequest;
import com.pierceecom.blog.payload.BlogPostResponse;
import com.pierceecom.blog.service.BlogPostService;

@RestController
@RequestMapping("/blog-web")
public class BlogPostController {

@Autowired
BlogPostService blogPostService;

@GetMapping(value = "/posts/getAllPosts")
public ResponseEntity<?> getAllPosts() {
return blogPostService.getAllPosts();
}

@PostMapping(value = "/posts/addPost")
public BlogPostResponse addNewPost(@Valid @RequestBody BlogPostRequest newPostRequest){
return blogPostService.addPost(newPostRequest);
}

@GetMapping(value = "/posts/getPostById/{postId}")
public ResponseEntity<BlogPost> findByPostId(@Valid @PathVariable String postId){
BlogPost allPosts = blogPostService.getPost(postId);
return new ResponseEntity< >(allPosts, HttpStatus.OK);
}

@PutMapping(value = "/posts/updatePost/{postId}")
public ResponseEntity<BlogPost> updatePost(@Valid @RequestBody BlogPostRequest newPostRequest) {
BlogPost post = blogPostService.updatePost(newPostRequest);

return new ResponseEntity< >(post, HttpStatus.OK);
}

@DeleteMapping(value = "/posts/deletePost/{postId}")
public ResponseEntity<ApiResponse> deletePost(@PathVariable(name = "postId") String id) {
ApiResponse apiResponse = blogPostService.deletePost(id);

return new ResponseEntity< >(apiResponse, HttpStatus.OK);
}
}
32 changes: 32 additions & 0 deletions blog-web/src/main/java/com/pierceecom/blog/entity/BlogPost.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.pierceecom.blog.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;

import lombok.Data;

@Entity
@Data
@Table(name = "posts")
public class BlogPost {
private static final long serialVersionUID = 1L;

@Id
@GenericGenerator(name="posts" , strategy="increment")
@GeneratedValue(generator="posts")
private Long id;

@Column(name = "postId", unique=true)
private String postId;

@Column(name = "title")
private String title;

@Column(name = "content")
private String content;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.pierceecom.blog.exception;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

import com.pierceecom.blog.payload.ApiResponse;

@ResponseStatus(value = HttpStatus.NOT_FOUND)
public class ResourceNotFoundException extends RuntimeException {
private static final long serialVersionUID = 1L;

private transient ApiResponse apiResponse;

private String resourceName;
private String fieldName;
private Object fieldValue;

public ResourceNotFoundException(String resourceName, String fieldName, Object fieldValue) {
super();
this.resourceName = resourceName;
this.fieldName = fieldName;
this.fieldValue = fieldValue;
}

public String getResourceName() {
return resourceName;
}

public String getFieldName() {
return fieldName;
}

public Object getFieldValue() {
return fieldValue;
}

public ApiResponse getApiResponse() {
return apiResponse;
}

private void setApiResponse() {
String message = String.format("%s not found with %s: '%s'", resourceName, fieldName, fieldValue);

apiResponse = new ApiResponse(Boolean.FALSE, message);
}
}
Loading