415 Unsupported Media Type

1. Basic user controller added

2. POST API explains the how to support differnet content-type formats
This commit is contained in:
Muhammad Abdullah Azam Khan 2021-10-14 01:15:13 +04:00
parent ec6f5d90cc
commit 1c38ab94a1
3 changed files with 103 additions and 0 deletions

View File

@ -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);
}
}

View File

@ -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 + '\'' +
'}';
}
}

View File

@ -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
}
}