Merge pull request #11327 from mabdullah12/feature/abdullah/baeldung/415_Unsupported_MediaType_for_POST
415 Unsupported Media Type
This commit is contained in:
commit
6f87f6edd5
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,66 @@
|
|||
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(){
|
||||
}
|
||||
|
||||
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 + '\''
|
||||
+ '}';
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -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<User> 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
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,58 @@
|
|||
package com.baeldung.unsupportedmediatype;
|
||||
|
||||
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;
|
||||
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 ApplicationUnitTest {
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@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());
|
||||
}
|
||||
|
||||
@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());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void XmlTestCase() throws Exception {
|
||||
mockMvc.perform(MockMvcRequestBuilders.post("/user/")
|
||||
.contentType(MediaType.APPLICATION_XML_VALUE)
|
||||
.content("<user><name>Andy</name><age>1</age><address>Hello world</address></user>"))
|
||||
.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("<user><name>Andy</name><age>1</age><address>Hello world</address></user>"))
|
||||
.andExpect(status().isUnsupportedMediaType());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue