From 4350c939eb4a81e7f073403318652dbd6d47e741 Mon Sep 17 00:00:00 2001 From: Sjmillington Date: Sat, 2 Nov 2019 13:10:11 +0000 Subject: [PATCH] [BAEL-18364] - moved the findBugs link to testing-modules/testing-libraries, and copied over the sample code from the article --- spring-resttemplate/README.md | 1 - testing-modules/testing-libraries/README.md | 1 + testing-modules/testing-libraries/pom.xml | 7 ++ .../web/controller/SimplePostController.java | 74 +++++++++++++++++++ .../com/baeldung/sampleapp/web/dto/Foo.java | 42 +++++++++++ 5 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 testing-modules/testing-libraries/src/main/java/com/baeldung/sampleapp/web/controller/SimplePostController.java create mode 100644 testing-modules/testing-libraries/src/main/java/com/baeldung/sampleapp/web/dto/Foo.java diff --git a/spring-resttemplate/README.md b/spring-resttemplate/README.md index 1fba4bc242..054071a4df 100644 --- a/spring-resttemplate/README.md +++ b/spring-resttemplate/README.md @@ -16,5 +16,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring - [Using the Spring RestTemplate Interceptor](https://www.baeldung.com/spring-rest-template-interceptor) - [Uploading MultipartFile with Spring RestTemplate](https://www.baeldung.com/spring-rest-template-multipart-upload) - [Get and Post Lists of Objects with RestTemplate](https://www.baeldung.com/spring-rest-template-list) -- [Introduction to FindBugs](https://www.baeldung.com/intro-to-findbugs) - [Copy of RestTemplate Post Request with JSON](https://www.baeldung.com/spring-resttemplate-post-json-test) diff --git a/testing-modules/testing-libraries/README.md b/testing-modules/testing-libraries/README.md index d41c85679a..d76b7b1308 100644 --- a/testing-modules/testing-libraries/README.md +++ b/testing-modules/testing-libraries/README.md @@ -7,5 +7,6 @@ - [Cucumber Java 8 Support](http://www.baeldung.com/cucumber-java-8-support) - [Introduction to Lambda Behave](http://www.baeldung.com/lambda-behave) - [Introduction to CheckStyle](https://www.baeldung.com/checkstyle-java) +- [Introduction to FindBugs](https://www.baeldung.com/intro-to-findbugs) diff --git a/testing-modules/testing-libraries/pom.xml b/testing-modules/testing-libraries/pom.xml index c9d6049530..0838e81d14 100644 --- a/testing-modules/testing-libraries/pom.xml +++ b/testing-modules/testing-libraries/pom.xml @@ -35,6 +35,13 @@ ${cucumber.version} test + + + org.springframework.boot + spring-boot-starter-web + 2.2.0.RELEASE + + diff --git a/testing-modules/testing-libraries/src/main/java/com/baeldung/sampleapp/web/controller/SimplePostController.java b/testing-modules/testing-libraries/src/main/java/com/baeldung/sampleapp/web/controller/SimplePostController.java new file mode 100644 index 0000000000..7b57d35088 --- /dev/null +++ b/testing-modules/testing-libraries/src/main/java/com/baeldung/sampleapp/web/controller/SimplePostController.java @@ -0,0 +1,74 @@ +package com.baeldung.sampleapp.web.controller; + +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; + +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.multipart.MultipartFile; + +import com.baeldung.sampleapp.web.dto.Foo; + +// used to test HttpClientPostingTest +@RestController +public class SimplePostController { + + @RequestMapping(value = "/users", method = RequestMethod.POST) + public String postUser(@RequestParam final String username, @RequestParam final String password) { + return "Success" + username; + } + + @RequestMapping(value = "/users/detail", method = RequestMethod.POST) + public String postUserDetail(@RequestBody final Foo entity) { + return "Success" + entity.getId(); + } + + @RequestMapping(value = "/users/multipart", method = RequestMethod.POST) + public String uploadFile(@RequestParam final String username, @RequestParam final String password, @RequestParam("file") final MultipartFile file) { + if (!file.isEmpty()) { + try { + final DateFormat dateFormat = new SimpleDateFormat("yyyy_MM_dd_HH.mm.ss"); + final String fileName = dateFormat.format(new Date()); + final File fileServer = new File(fileName); + fileServer.createNewFile(); + final byte[] bytes = file.getBytes(); + final BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(fileServer)); + stream.write(bytes); + stream.close(); + return "You successfully uploaded " + username; + } catch (final Exception e) { + return "You failed to upload " + e.getMessage(); + } + } else { + return "You failed to upload because the file was empty."; + } + } + + @RequestMapping(value = "/users/upload", method = RequestMethod.POST) + public String postMultipart(@RequestParam("file") final MultipartFile file) { + if (!file.isEmpty()) { + try { + final DateFormat dateFormat = new SimpleDateFormat("yyyy_MM_dd_HH.mm.ss"); + final String fileName = dateFormat.format(new Date()); + final File fileServer = new File(fileName); + fileServer.createNewFile(); + final byte[] bytes = file.getBytes(); + final BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(fileServer)); + stream.write(bytes); + stream.close(); + return "You successfully uploaded "; + } catch (final Exception e) { + return "You failed to upload " + e.getMessage(); + } + } else { + return "You failed to upload because the file was empty."; + } + } +} diff --git a/testing-modules/testing-libraries/src/main/java/com/baeldung/sampleapp/web/dto/Foo.java b/testing-modules/testing-libraries/src/main/java/com/baeldung/sampleapp/web/dto/Foo.java new file mode 100644 index 0000000000..de1d76ed92 --- /dev/null +++ b/testing-modules/testing-libraries/src/main/java/com/baeldung/sampleapp/web/dto/Foo.java @@ -0,0 +1,42 @@ +package com.baeldung.sampleapp.web.dto; + +public class Foo { + private long id; + private String name; + + public Foo() { + super(); + } + + public Foo(final String name) { + super(); + + this.name = name; + } + + public Foo(final long id, final String name) { + super(); + + this.id = id; + this.name = name; + } + + // API + + public long getId() { + return id; + } + + public void setId(final long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(final String name) { + this.name = name; + } + +} \ No newline at end of file