From 1c38ab94a1d3c67c2fc47789401ee8c9d127a9ad Mon Sep 17 00:00:00 2001 From: Muhammad Abdullah Azam Khan Date: Thu, 14 Oct 2021 01:15:13 +0400 Subject: [PATCH 1/5] 415 Unsupported Media Type 1. Basic user controller added 2. POST API explains the how to support differnet content-type formats --- .../UnsupportedMediaTypeApplication.java | 13 ++++ .../baeldung/unsupportedmediatype/User.java | 61 +++++++++++++++++++ .../unsupportedmediatype/UserController.java | 29 +++++++++ 3 files changed, 103 insertions(+) create mode 100644 spring-boot-rest-2/src/main/java/com/baeldung/unsupportedmediatype/UnsupportedMediaTypeApplication.java create mode 100644 spring-boot-rest-2/src/main/java/com/baeldung/unsupportedmediatype/User.java create mode 100644 spring-boot-rest-2/src/main/java/com/baeldung/unsupportedmediatype/UserController.java diff --git a/spring-boot-rest-2/src/main/java/com/baeldung/unsupportedmediatype/UnsupportedMediaTypeApplication.java b/spring-boot-rest-2/src/main/java/com/baeldung/unsupportedmediatype/UnsupportedMediaTypeApplication.java new file mode 100644 index 0000000000..ccc136ef86 --- /dev/null +++ b/spring-boot-rest-2/src/main/java/com/baeldung/unsupportedmediatype/UnsupportedMediaTypeApplication.java @@ -0,0 +1,13 @@ +package com.baeldung.unsupportedmediatype; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class UnsupportedMediaTypeApplication { + + public static void main(String[] args) { + SpringApplication.run(UnsupportedMediaTypeApplication.class, args); + } + +} diff --git a/spring-boot-rest-2/src/main/java/com/baeldung/unsupportedmediatype/User.java b/spring-boot-rest-2/src/main/java/com/baeldung/unsupportedmediatype/User.java new file mode 100644 index 0000000000..74a6f4383b --- /dev/null +++ b/spring-boot-rest-2/src/main/java/com/baeldung/unsupportedmediatype/User.java @@ -0,0 +1,61 @@ +package com.baeldung.unsupportedmediatype; + +import javax.xml.bind.annotation.XmlRootElement; +import java.io.Serializable; + +@XmlRootElement +public class User implements Serializable { + private Integer id; + private String name; + private Integer age; + private String address; + + public User(Integer id, String name, Integer age, String address){ + this.id = id; + this.name = name; + this.age = age; + this.address = address; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Integer getAge() { + return age; + } + + public void setAge(Integer age) { + this.age = age; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } + + @Override + public String toString() { + return "User{" + + "id=" + id + + ", name='" + name + '\'' + + ", age=" + age + + ", address='" + address + '\'' + + '}'; + } +} diff --git a/spring-boot-rest-2/src/main/java/com/baeldung/unsupportedmediatype/UserController.java b/spring-boot-rest-2/src/main/java/com/baeldung/unsupportedmediatype/UserController.java new file mode 100644 index 0000000000..a20043619a --- /dev/null +++ b/spring-boot-rest-2/src/main/java/com/baeldung/unsupportedmediatype/UserController.java @@ -0,0 +1,29 @@ +package com.baeldung.unsupportedmediatype; + +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.*; + +import java.util.Collections; +import java.util.List; + +@RestController +@RequestMapping("/user") +public class UserController { + + @GetMapping(value = "/") + List getAllUsers(){ + return Collections.singletonList(new User(1, "Andy", 28, "14th Street")); + } + + @GetMapping(value = "/{user-id}") + User getUser(@PathVariable("user-id") Integer userId){ + return new User(userId, "Andy", 28, "14th Street"); + } + + @PostMapping(value = "/", consumes = {MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE}) + void AddUser(@RequestBody User user){ + // Adding the User in the repository + } + + +} \ No newline at end of file From 842f27c77eb2e7e6fd6b2b920b37383ed2805d78 Mon Sep 17 00:00:00 2001 From: Muhammad Abdullah Azam Khan Date: Sun, 17 Oct 2021 00:24:08 +0400 Subject: [PATCH 2/5] Test case for unsupported media type error --- .../unsupportedmedia/ApplicationTest.java | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 spring-boot-rest-2/src/test/java/com/baeldung/unsupportedmedia/ApplicationTest.java diff --git a/spring-boot-rest-2/src/test/java/com/baeldung/unsupportedmedia/ApplicationTest.java b/spring-boot-rest-2/src/test/java/com/baeldung/unsupportedmedia/ApplicationTest.java new file mode 100644 index 0000000000..e5c56d0adc --- /dev/null +++ b/spring-boot-rest-2/src/test/java/com/baeldung/unsupportedmedia/ApplicationTest.java @@ -0,0 +1,59 @@ +package com.baeldung.unsupportedmedia; + +import com.baeldung.unsupportedmediatype.UserController; +import org.junit.jupiter.api.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.http.MediaType; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; + +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@RunWith(SpringRunner.class) +@WebMvcTest(UserController.class) +public class ApplicationTest { + @Autowired + private MockMvc mockMvc; + + @Test + void JsonTestCase() throws Exception { + mockMvc.perform(MockMvcRequestBuilders.post("/user/") + .contentType(MediaType.APPLICATION_JSON_VALUE) + .content("{\n" + + " \"name\": \"Andy\",\n" + + " \"age\": 1,\n" + + " \"address\": \"Hello world\"\n" + + "}")) + .andExpect(status().isOk()); + } + + @Test + void JsonFailTestCase() throws Exception {// Because no content-type added + mockMvc.perform(MockMvcRequestBuilders.post("/user/") + .content("{\n" + + " \"name\": \"Andy\",\n" + + " \"age\": 1,\n" + + " \"address\": \"Hello world\"\n" + + "}")) + .andExpect(status().isUnsupportedMediaType()); + } + + @Test + void XmlTestCase() throws Exception { + mockMvc.perform(MockMvcRequestBuilders.post("/user/") + .contentType(MediaType.APPLICATION_XML_VALUE) + .content("Andy1
Hello world
")) + .andExpect(status().isOk()); + } + + @Test + void StringTestCase() throws Exception { + mockMvc.perform(MockMvcRequestBuilders.post("/user/") + .contentType(MediaType.TEXT_PLAIN_VALUE) + .content("Andy1
Hello world
")) + .andExpect(status().isUnsupportedMediaType()); + } +} From 733c7e76b96ba59962dc5032ae6dd4f0de5fca1c Mon Sep 17 00:00:00 2001 From: Muhammad Abdullah Azam Khan Date: Sun, 17 Oct 2021 00:52:40 +0400 Subject: [PATCH 3/5] Resolving build failure cases --- .../com/baeldung/unsupportedmediatype/User.java | 4 ++++ .../ApplicationUnitTest.java} | 15 +++++++-------- 2 files changed, 11 insertions(+), 8 deletions(-) rename spring-boot-rest-2/src/test/java/com/baeldung/{unsupportedmedia/ApplicationTest.java => unsupportedmediatype/ApplicationUnitTest.java} (84%) diff --git a/spring-boot-rest-2/src/main/java/com/baeldung/unsupportedmediatype/User.java b/spring-boot-rest-2/src/main/java/com/baeldung/unsupportedmediatype/User.java index 74a6f4383b..5f5f2a972c 100644 --- a/spring-boot-rest-2/src/main/java/com/baeldung/unsupportedmediatype/User.java +++ b/spring-boot-rest-2/src/main/java/com/baeldung/unsupportedmediatype/User.java @@ -10,6 +10,10 @@ public class User implements Serializable { private Integer age; private String address; + public User(){ + + } + public User(Integer id, String name, Integer age, String address){ this.id = id; this.name = name; diff --git a/spring-boot-rest-2/src/test/java/com/baeldung/unsupportedmedia/ApplicationTest.java b/spring-boot-rest-2/src/test/java/com/baeldung/unsupportedmediatype/ApplicationUnitTest.java similarity index 84% rename from spring-boot-rest-2/src/test/java/com/baeldung/unsupportedmedia/ApplicationTest.java rename to spring-boot-rest-2/src/test/java/com/baeldung/unsupportedmediatype/ApplicationUnitTest.java index e5c56d0adc..a84388f750 100644 --- a/spring-boot-rest-2/src/test/java/com/baeldung/unsupportedmedia/ApplicationTest.java +++ b/spring-boot-rest-2/src/test/java/com/baeldung/unsupportedmediatype/ApplicationUnitTest.java @@ -1,7 +1,6 @@ -package com.baeldung.unsupportedmedia; +package com.baeldung.unsupportedmediatype; -import com.baeldung.unsupportedmediatype.UserController; -import org.junit.jupiter.api.Test; +import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; @@ -14,12 +13,12 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. @RunWith(SpringRunner.class) @WebMvcTest(UserController.class) -public class ApplicationTest { +public class ApplicationUnitTest { @Autowired private MockMvc mockMvc; @Test - void JsonTestCase() throws Exception { + public void JsonTestCase() throws Exception { mockMvc.perform(MockMvcRequestBuilders.post("/user/") .contentType(MediaType.APPLICATION_JSON_VALUE) .content("{\n" + @@ -31,7 +30,7 @@ public class ApplicationTest { } @Test - void JsonFailTestCase() throws Exception {// Because no content-type added + public void JsonFailTestCase() throws Exception {// Because no content-type added mockMvc.perform(MockMvcRequestBuilders.post("/user/") .content("{\n" + " \"name\": \"Andy\",\n" + @@ -42,7 +41,7 @@ public class ApplicationTest { } @Test - void XmlTestCase() throws Exception { + public void XmlTestCase() throws Exception { mockMvc.perform(MockMvcRequestBuilders.post("/user/") .contentType(MediaType.APPLICATION_XML_VALUE) .content("Andy1
Hello world
")) @@ -50,7 +49,7 @@ public class ApplicationTest { } @Test - void StringTestCase() throws Exception { + public void StringFailTestCase() throws Exception { // Because content-type is not supported mockMvc.perform(MockMvcRequestBuilders.post("/user/") .contentType(MediaType.TEXT_PLAIN_VALUE) .content("Andy1
Hello world
")) From 9d464b4f459987e0dbc548beec1dd8882dba21b6 Mon Sep 17 00:00:00 2001 From: Muhammad Abdullah Azam Khan Date: Fri, 5 Nov 2021 18:32:19 +0400 Subject: [PATCH 4/5] Refactoring for better Indentation and Formatting --- .../UnsupportedMediaTypeApplication.java | 6 ++-- .../baeldung/unsupportedmediatype/User.java | 17 +++++------ .../ApplicationUnitTest.java | 28 +++++++++---------- 3 files changed, 26 insertions(+), 25 deletions(-) diff --git a/spring-boot-rest-2/src/main/java/com/baeldung/unsupportedmediatype/UnsupportedMediaTypeApplication.java b/spring-boot-rest-2/src/main/java/com/baeldung/unsupportedmediatype/UnsupportedMediaTypeApplication.java index ccc136ef86..2bf6bb33cd 100644 --- a/spring-boot-rest-2/src/main/java/com/baeldung/unsupportedmediatype/UnsupportedMediaTypeApplication.java +++ b/spring-boot-rest-2/src/main/java/com/baeldung/unsupportedmediatype/UnsupportedMediaTypeApplication.java @@ -6,8 +6,8 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class UnsupportedMediaTypeApplication { - public static void main(String[] args) { - SpringApplication.run(UnsupportedMediaTypeApplication.class, args); - } + public static void main(String[] args) { + SpringApplication.run(UnsupportedMediaTypeApplication.class, args); + } } diff --git a/spring-boot-rest-2/src/main/java/com/baeldung/unsupportedmediatype/User.java b/spring-boot-rest-2/src/main/java/com/baeldung/unsupportedmediatype/User.java index 5f5f2a972c..f9c3d9c191 100644 --- a/spring-boot-rest-2/src/main/java/com/baeldung/unsupportedmediatype/User.java +++ b/spring-boot-rest-2/src/main/java/com/baeldung/unsupportedmediatype/User.java @@ -11,10 +11,9 @@ public class User implements Serializable { private String address; public User(){ - } - public User(Integer id, String name, Integer age, String address){ + public User(Integer id, String name, Integer age, String address) { this.id = id; this.name = name; this.age = age; @@ -55,11 +54,13 @@ public class User implements Serializable { @Override public String toString() { - return "User{" + - "id=" + id + - ", name='" + name + '\'' + - ", age=" + age + - ", address='" + address + '\'' + - '}'; + return "User{" + + "id=" + id + + ", name='" + name + '\'' + + ", age=" + age + + ", address='" + address + '\'' + + '}'; } + + } diff --git a/spring-boot-rest-2/src/test/java/com/baeldung/unsupportedmediatype/ApplicationUnitTest.java b/spring-boot-rest-2/src/test/java/com/baeldung/unsupportedmediatype/ApplicationUnitTest.java index a84388f750..95de780106 100644 --- a/spring-boot-rest-2/src/test/java/com/baeldung/unsupportedmediatype/ApplicationUnitTest.java +++ b/spring-boot-rest-2/src/test/java/com/baeldung/unsupportedmediatype/ApplicationUnitTest.java @@ -21,38 +21,38 @@ public class ApplicationUnitTest { public void JsonTestCase() throws Exception { mockMvc.perform(MockMvcRequestBuilders.post("/user/") .contentType(MediaType.APPLICATION_JSON_VALUE) - .content("{\n" + - " \"name\": \"Andy\",\n" + - " \"age\": 1,\n" + - " \"address\": \"Hello world\"\n" + - "}")) + .content( "{\n" + + " \"name\": \"Andy\",\n" + + " \"age\": 1,\n" + + " \"address\": \"Hello world\"\n" + + "}")) .andExpect(status().isOk()); } @Test public void JsonFailTestCase() throws Exception {// Because no content-type added mockMvc.perform(MockMvcRequestBuilders.post("/user/") - .content("{\n" + - " \"name\": \"Andy\",\n" + - " \"age\": 1,\n" + - " \"address\": \"Hello world\"\n" + - "}")) + .content( "{\n" + + " \"name\": \"Andy\",\n" + + " \"age\": 1,\n" + + " \"address\": \"Hello world\"\n" + + "}")) .andExpect(status().isUnsupportedMediaType()); } @Test public void XmlTestCase() throws Exception { mockMvc.perform(MockMvcRequestBuilders.post("/user/") - .contentType(MediaType.APPLICATION_XML_VALUE) - .content("Andy1
Hello world
")) + .contentType(MediaType.APPLICATION_XML_VALUE) + .content("Andy1
Hello world
")) .andExpect(status().isOk()); } @Test public void StringFailTestCase() throws Exception { // Because content-type is not supported mockMvc.perform(MockMvcRequestBuilders.post("/user/") - .contentType(MediaType.TEXT_PLAIN_VALUE) - .content("Andy1
Hello world
")) + .contentType(MediaType.TEXT_PLAIN_VALUE) + .content("Andy1
Hello world
")) .andExpect(status().isUnsupportedMediaType()); } } From 86dcb518b2d9d95321405d3099ea5ce8fe5857e7 Mon Sep 17 00:00:00 2001 From: Muhammad Abdullah Azam Khan Date: Fri, 26 Nov 2021 20:54:37 +0400 Subject: [PATCH 5/5] Indentation and Formatting Changes --- .../baeldung/unsupportedmediatype/User.java | 10 ++--- .../ApplicationUnitTest.java | 38 +++++++++---------- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/spring-boot-rest-2/src/main/java/com/baeldung/unsupportedmediatype/User.java b/spring-boot-rest-2/src/main/java/com/baeldung/unsupportedmediatype/User.java index f9c3d9c191..765149dad5 100644 --- a/spring-boot-rest-2/src/main/java/com/baeldung/unsupportedmediatype/User.java +++ b/spring-boot-rest-2/src/main/java/com/baeldung/unsupportedmediatype/User.java @@ -55,11 +55,11 @@ public class User implements Serializable { @Override public String toString() { return "User{" - + "id=" + id - + ", name='" + name + '\'' - + ", age=" + age - + ", address='" + address + '\'' - + '}'; + + "id=" + id + + ", name='" + name + '\'' + + ", age=" + age + + ", address='" + address + '\'' + + '}'; } diff --git a/spring-boot-rest-2/src/test/java/com/baeldung/unsupportedmediatype/ApplicationUnitTest.java b/spring-boot-rest-2/src/test/java/com/baeldung/unsupportedmediatype/ApplicationUnitTest.java index 95de780106..498ad9d537 100644 --- a/spring-boot-rest-2/src/test/java/com/baeldung/unsupportedmediatype/ApplicationUnitTest.java +++ b/spring-boot-rest-2/src/test/java/com/baeldung/unsupportedmediatype/ApplicationUnitTest.java @@ -20,39 +20,39 @@ public class ApplicationUnitTest { @Test public void JsonTestCase() throws Exception { mockMvc.perform(MockMvcRequestBuilders.post("/user/") - .contentType(MediaType.APPLICATION_JSON_VALUE) - .content( "{\n" - + " \"name\": \"Andy\",\n" - + " \"age\": 1,\n" - + " \"address\": \"Hello world\"\n" - + "}")) - .andExpect(status().isOk()); + .contentType(MediaType.APPLICATION_JSON_VALUE) + .content( "{\n" + + " \"name\": \"Andy\",\n" + + " \"age\": 1,\n" + + " \"address\": \"Hello world\"\n" + + "}")) + .andExpect(status().isOk()); } @Test public void JsonFailTestCase() throws Exception {// Because no content-type added mockMvc.perform(MockMvcRequestBuilders.post("/user/") - .content( "{\n" - + " \"name\": \"Andy\",\n" - + " \"age\": 1,\n" - + " \"address\": \"Hello world\"\n" - + "}")) - .andExpect(status().isUnsupportedMediaType()); + .content( "{\n" + + " \"name\": \"Andy\",\n" + + " \"age\": 1,\n" + + " \"address\": \"Hello world\"\n" + + "}")) + .andExpect(status().isUnsupportedMediaType()); } @Test public void XmlTestCase() throws Exception { mockMvc.perform(MockMvcRequestBuilders.post("/user/") - .contentType(MediaType.APPLICATION_XML_VALUE) - .content("Andy1
Hello world
")) - .andExpect(status().isOk()); + .contentType(MediaType.APPLICATION_XML_VALUE) + .content("Andy1
Hello world
")) + .andExpect(status().isOk()); } @Test public void StringFailTestCase() throws Exception { // Because content-type is not supported mockMvc.perform(MockMvcRequestBuilders.post("/user/") - .contentType(MediaType.TEXT_PLAIN_VALUE) - .content("Andy1
Hello world
")) - .andExpect(status().isUnsupportedMediaType()); + .contentType(MediaType.TEXT_PLAIN_VALUE) + .content("Andy1
Hello world
")) + .andExpect(status().isUnsupportedMediaType()); } }