javax.ws.rs.core响应与弹簧启动ANOTITATION相结合,返回JSON响应中的所有内容



我的代码以这种方式工作,但是在这种情况下,有什么方法可以将响应方法与春季启动ANOTITATION一起使用,而无需返回整个响应数据吗?

l猜测,带有邮政图的RestController包括响应返回所有内容。

@RestController
@RequestMapping("/v1")
public class Resource {
@PostMapping(value = "/post")
public Response post(@RequestBody final Data data) {
    Response response = null;
    try {
        validateData(data);
        LOG.info("SUCCESS");
        response = Response.status(Status.OK).entity("Success").build();
    } catch (Exception e) {
        LOG.error(e.getMessage(), e.getCause());
        response = Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build();
    }
    return response;
}

{
"context": {
    "headers": {},
    "entity": "Success",
    "entityType": "java.lang.String",
    "entityAnnotations": [],
    "entityStream": {
        "committed": false,
        "closed": false
    },
    "stringHeaders": {},
    "mediaType": null,
    "allowedMethods": [],
    "committed": false,
    "entityTag": null,
    "links": [],
    "acceptableMediaTypes": [
        {
            "type": "*",
            "subtype": "*",
            "parameters": {},
            "quality": 1000,
            "wildcardType": true,
            "wildcardSubtype": true
        }
    ],
    "acceptableLanguages": [
        "*"
    ],
    "entityClass": "java.lang.String",
    "requestCookies": {},
    "responseCookies": {},
    "lengthLong": -1,
    "lastModified": null,
    "date": null,
    "length": -1,
    "language": null,
    "location": null
},
"status": 200,
"stringHeaders": {},
"statusInfo": "OK",
"mediaType": null,
"metadata": {},
"allowedMethods": [],
"cookies": {},
"entityTag": null,
"links": [],
"lastModified": null,
"entity": "Success",
"date": null,
"length": -1,
"language": null,
"location": null,
"headers": {}
}

如果我使用具有相同代码的球衣阳极,则可以得到我所需的东西。通过我的数据在身体中的响应,也不能使用Swagger2,因为不支持球衣。
是否有某种方法可以将第一部分与Spring Boot Anotations一起使用,而无需返回响应方法中的所有内容,只是状态代码200或400?方法需要是响应而不是数据或列表,谢谢

@Component
@Path("/v1")
public class Resource {
    @POST
    @Path("/post")
    @Produces(MediaType.APPLICATION_JSON)
    public Response post(@RequestBody final Data data) {
        Response response = null;
        try {
            validateData(data);
            LOG.info("SUCCESS");
            response = Response.status(Status.OK).entity(new BasicResponse("0", "Success")).build();
        } catch (Exception e) {
            LOG.error(e.getMessage(), e.getCause());
            response = Response.status(Status.BAD_REQUEST).entity(new BasicResponse(Status.BAD_REQUEST.toString(), e.getMessage())).build();
        }
        return response;
    }
}

我认为您应该返回ResponseEntity而不是Response,Spring使用ResponseEntityResponse是JAX-RS类型,所以我怀疑它是否可以与Spring注释正常工作,在您的情况下:p> return ResponseEntity.ok("Success");

应该工作。

最新更新