Controller reception processing json, xml format data

 

1.RequestBody json received data format, and directly into the object.

Use lombok User.java dependencies

@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class User{
    private int id;
    private String name;      
}

UserController.java

@Controller
public class UserController {
    @GetMapping("/testJson")
    public String testJson(@RequestBody User user){
        System.out.println(user);
        return "user";
    }
}

Use Postman access

Print Results:

User(id=12, name=aaa)

 

2.RequestBody receiving xml format data, automatically converted received map

A new method in UserController.java

    @GetMapping("/testXml")
    public String testXml(@RequestBody Map<String,String> map){
        System.out.println(map);
        return "user";
    }

Pom.xml file in the new add-dependent:

<dependency>
    <groupId>com.fasterxml.jackson.jaxrs</groupId>
    <artifactId>jackson-jaxrs-xml-provider</artifactId>
    <version>2.4.3</version>
</dependency>

Use Postman test

Print Results:

{id=12, name=John}

 RequestBody can handle Json, xml parameters

 

Guess you like

Origin www.cnblogs.com/Yatces/p/11082712.html