繁体   English   中英

Spring Boot自动JSON到控制器对象

[英]Spring Boot Automatic JSON to Object at Controller

我有SpringBoot应用程序与该依赖项:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jersey</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

我在我的控制器上有一个方法如下:

@RequestMapping(value = "/liamo", method = RequestMethod.POST)
@ResponseBody
public XResponse liamo(XRequest xRequest) {
    ...
    return something;
}

我通过AJAX从我的HTML发送一个JSON对象,其中包含一些XRequest类型对象的字段(它是一个没有任何注释的普通POJO)。 但是我的JSON没有在我的控制器方法中构造成对象,并且它的字段为空。

我错过了我的控制器的自动反序列化?

Spring启动时带有弹出启动功能,它将处理Java对象的非编组JSON请求主体

您可以使用@RequestBody Spring MVC注释来反序列化/取消编组JSON字符串到Java对象...例如。

@RestController
public class CustomerController {
    //@Autowired CustomerService customerService;

    @RequestMapping(path="/customers", method= RequestMethod.POST)
    @ResponseStatus(HttpStatus.CREATED)
    public Customer postCustomer(@RequestBody Customer customer){
        //return customerService.createCustomer(customer);
    }
}

使用@JsonProperty使用相应的json字段名称注释实体成员元素。

public class Customer {
    @JsonProperty("customer_id")
    private long customerId;
    @JsonProperty("first_name")
    private String firstName;
    @JsonProperty("last_name")
    private String lastName;
    @JsonProperty("town")
    private String town;
}

默认情况下,SpringBoot具有此功能。 您只需在控制器方法的参数声明中使用@RequestBody注释,但与@ so-random-dude答案相反,您不必使用@JsonProperty注释字段,这不是必需的。

您只需为自定义XML对象类提供getter和setter。 为简单起见,我在下面发布一个例子。

例:

控制器方法声明: -

@PostMapping("/create")
    public ResponseEntity<ApplicationResponse> createNewPost(@RequestBody CreatePostRequestDto createPostRequest){
        //do stuff
        return response;
    }

您的自定义XML对象类: -

public class CreatePostRequestDto {
    String postPath;

    String postTitle;

    public String getPostPath() {
        return postPath;
    }

    public void setPostPath(String postPath) {
        this.postPath = postPath;
    }

    public String getPostTitle() {
        return postTitle;
    }

    public void setPostTitle(String postTitle) {
        this.postTitle = postTitle;
    }
}

列表<object> null 在 Spring 启动 Z594C103F2C6E04C3D8AB059F031E0C1 中反序列化 json 时<div id="text_translate"><p>我在 Spring 引导应用程序中遇到了 controller 问题。 当我在 controller(URI/调用)上进行调用时,object 列表始终是 null。</p><pre> { "code": "TEST", "name": "TEST NAME", "groupe": "A1", "list": [ {"type":"web", "link":"https://google.com/"}, {"type":"web", "link":"https://google2.com/"} ] }</pre><pre class="lang-java prettyprint-override"> @PostMapping(value="/call") public ResponseEntity&lt;Void&gt; ajouterEnvironnement(@RequestBody First first) { first.getCode() // value: "TEST" first.getList() // value: null }</pre><pre class="lang-java prettyprint-override"> public class First { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; private String code; private String name; private String groupe; @OneToMany(mappedBy = "first", cascade = CascadeType.ALL, fetch = FetchType.EAGER) private List&lt;Second&gt; list; }</pre><pre class="lang-java prettyprint-override"> public class Second { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; private String type; private String link; @ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name = "first_id", nullable = false) private First first; }</pre></div></object>

[英]List<object> null when deserializing json in Spring Boot controller

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从spring-boot rest控制器返回JSON对象 列表<object> null 在 Spring 启动 Z594C103F2C6E04C3D8AB059F031E0C1 中反序列化 json 时<div id="text_translate"><p>我在 Spring 引导应用程序中遇到了 controller 问题。 当我在 controller(URI/调用)上进行调用时,object 列表始终是 null。</p><pre> { "code": "TEST", "name": "TEST NAME", "groupe": "A1", "list": [ {"type":"web", "link":"https://google.com/"}, {"type":"web", "link":"https://google2.com/"} ] }</pre><pre class="lang-java prettyprint-override"> @PostMapping(value="/call") public ResponseEntity&lt;Void&gt; ajouterEnvironnement(@RequestBody First first) { first.getCode() // value: "TEST" first.getList() // value: null }</pre><pre class="lang-java prettyprint-override"> public class First { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; private String code; private String name; private String groupe; @OneToMany(mappedBy = "first", cascade = CascadeType.ALL, fetch = FetchType.EAGER) private List&lt;Second&gt; list; }</pre><pre class="lang-java prettyprint-override"> public class Second { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; private String type; private String link; @ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name = "first_id", nullable = false) private First first; }</pre></div></object> 在Spring Boot Rest Controller中解析Json数组init单个对象 Object 渲染到 Json - Spring 引导 Spring Boot-奇怪的JSON控制器响应 Spring 引导 controller - 上传多部分和 JSON 到 DTO 将JSON对象发送到Spring控制器 Spring Boot 控制器对象中的 NullPointerException 错误 通过自定义 object 进入 Spring 引导 Controller 不同类中的控制器之间的Spring Boot对象
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM