Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Response.ok和元数据_Java_Spring_Restful Architecture - Fatal编程技术网

Java Response.ok和元数据

Java Response.ok和元数据,java,spring,restful-architecture,Java,Spring,Restful Architecture,在我的RESTful服务项目中,我有一个添加记录的方法(新人): 在控制器中: @PostMapping(value = "/addPerson") public Response dodajOsobe(InputStream inputStream) { return osMgr.dodajOsobe(inputStream); } 在职: public Response dodajOsobe(InputStream inputStream) { Response resp

在我的RESTful服务项目中,我有一个添加记录的方法(新人):

在控制器中:

@PostMapping(value = "/addPerson")
public Response dodajOsobe(InputStream inputStream) {
    return osMgr.dodajOsobe(inputStream);
}
在职:

public Response dodajOsobe(InputStream inputStream) {

    Response response = null;
    try {
        String input = misc.InputStreamToString(inputStream);
        Integer id = dodajOsobe(input);
        response = Response.ok("New person added").build();
    } catch (Exception e) {
        response = Response.serverError().entity("Nie udało się dodać osoby. Przyczyna: " + e.getLocalizedMessage()).build();
    }
    return response;
}
如果一切正常,则返回状态为OK的对象“响应”:

response = Response.ok("New person added").build();
在浏览器中执行后,结果如下所示:

{
    "statusType": "OK",
    "entity": "New person added",
    "entityType": "java.lang.String",
    "metadata": {},
    "status": 200
}
{
    "statusType": "OK",
    "entity": "New person added",
    "entityType": "java.lang.String",
    "metadata": {"id":112233},
    "status": 200
}
我想返回此人的身份证。我知道我可以在“实体”中执行此操作,但我希望有可重新编辑的信息。 有字段“metadata”,我不会把记录的id放在那里,如下所示:

{
    "statusType": "OK",
    "entity": "New person added",
    "entityType": "java.lang.String",
    "metadata": {},
    "status": 200
}
{
    "statusType": "OK",
    "entity": "New person added",
    "entityType": "java.lang.String",
    "metadata": {"id":112233},
    "status": 200
}

如何做到这一点???

您可以创建自定义响应类,例如

class OkResponse {
  private EntityId id; //or something else
  private String message;
}

并将其实例作为响应返回。

我最后添加了header方法:

        response = Response.ok("New record added")
                .header("rezerwacjaId", rezId)
                .build();
得到了JSON:

{
  "entity": "New record added",
  "entityType": "java.lang.String",
  "metadata": {
    "rezerwacjaId": [
      77754
    ]
  },
  "status": 200,
  "statusType": "OK"
}

你的问题令人困惑。你到底想干什么?还请添加实体的代码和整个rest控制器方法。是否可以使用此对象执行此操作?是的,请参见以下问题: