简体   繁体   English

如何将 Java POJO / Json 发布到 Play Framework v2.2.1 Controller Z9D5D5D543DE68F05BBB5 方法

[英]How to POST a Java POJO / Json to a Play Framework v2.2.1 Controller Http POST Method

I am writing an API using Java play framework.我正在使用 Java 播放框架编写 API。 I would like to post a Json via a rest client like Postman, while testing the Play controller.我想通过 rest 客户端(如 Postman)发布 Json,同时测试播放 Z594C103F2C6EABEE4C3Z894C103F2C6EABEE4C3Z

Version of Java Play = 2.2.1 Java 版本播放= 2.2.1

Controller method: Controller 方法:

public static Result submitDetails(DetailsVO detailsVO) throws Exception

Routes config路线配置

POST    /submit                     controllers.application.ProcessDetails.submitDetails(detailsVO:DetailsVO)

Below is my POJO下面是我的 POJO

import java.io.Serializable;
import java.util.List;

public class DetailsVO implements Serializable {
    public DetailsVO(){}

    private String firstName;
    private String middleName;

    private List<Details> detailsList;

//Setter and getter for first name and last name

public List<Details> getDetailsList() {
        return detailsList;
    }

    public void setDetailsList(List<Details> detailsList) {
        this.detailsList = detailsList;
    }

}

After adding the routes entry, I am getting this error "Cannot resolve reference"添加路由条目后,我收到此错误“无法解析参考”

Please, could some advice on how to define a post method that takes a Java POJO in the routes?请就如何定义一个在路线中采用 Java POJO 的 post 方法提供一些建议吗?

I dont think it is possible to use your VO directly in play for java.我认为在 java 中直接使用您的 VO 是不可能的。 You need to use the play request type.您需要使用播放请求类型。 And then from the request you get the body as JSON.然后从请求中你得到的主体是 JSON。 You can use any JSON parser for this.您可以为此使用任何 JSON 解析器。 Play has one, but you can use whatever you like. Play 有一个,但你可以使用任何你喜欢的东西。 Your route would look like this:您的路线如下所示:

POST    /submit                     @controllers.HomeController.submitDetails(request: Request)

Remove the static from your controller function, and let it take the Http.Request instead of the VO.从您的 controller function 中删除 static,取而代之的是 Z9D4D43DE68F0B35555D5 的请求。

public Result submitDetails(Http.Request request) throws Exception {
    ObjectMapper objectMapper = new ObjectMapper();
    JsonNode json = request.body().asJson();
    DetailsVO dvo = objectMapper.convertValue(json, DetailsVO.class);
    return ok(json.toString());
}

For this example i used the Jackson JSON parser.对于这个例子,我使用了 Jackson JSON 解析器。 For this particular solution to work you also need your fields to be public, because the parser does not work for the private fields.要使此特定解决方案起作用,您还需要公开字段,因为解析器不适用于私有字段。 I would suggest using the play documentation on their website.我建议使用他们网站上的播放文档。 It can be found here: https://www.playframework.com/documentation/2.8.x/JavaHome I would suggest trying scala when you're trying out the play framework, it makes for much cleaner code with this framework.它可以在这里找到: https://www.playframework.com/documentation/2.8.x/JavaHome我建议尝试 scala 当你使用这个框架来尝试更简洁的代码时

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM