Search in sources :

Example 1 with MultiResponse500

use of org.apache.servicecomb.demo.multiErrorCode.MultiResponse500 in project java-chassis by ServiceComb.

the class MultiErrorCodeService method errorCodeWithHeader.

@Path("/errorCodeWithHeader")
@POST
@ApiResponses({ @ApiResponse(code = 200, response = MultiResponse200.class, message = ""), @ApiResponse(code = 400, response = MultiResponse400.class, message = ""), @ApiResponse(code = 500, response = MultiResponse500.class, message = "") })
@ResponseHeaders({ @ResponseHeader(name = "x-code", response = String.class) })
public Response errorCodeWithHeader(MultiRequest request) {
    Response response = new Response();
    if (request.getCode() == 400) {
        MultiResponse400 r = new MultiResponse400();
        r.setCode(400);
        r.setMessage("bad request");
        response.setStatus(Status.BAD_REQUEST);
        // If got many types for different status code, we can only using InvocationException for failed error code like 400-500.
        // The result for Failed Family(e.g. 400-500), can not set return value as target type directly or will give exception.
        response.setResult(new InvocationException(Status.BAD_REQUEST, r));
        response.setHeader("x-code", "400");
    } else if (request.getCode() == 500) {
        MultiResponse500 r = new MultiResponse500();
        r.setCode(500);
        r.setMessage("internal error");
        response.setStatus(Status.INTERNAL_SERVER_ERROR);
        response.setResult(new InvocationException(Status.INTERNAL_SERVER_ERROR, r));
        response.setHeader("x-code", "500");
    } else {
        MultiResponse200 r = new MultiResponse200();
        r.setCode(200);
        r.setMessage("success result");
        response.setStatus(Status.OK);
        // If error code is OK family(like 200), we can use the target type.
        response.setResult(r);
        response.setHeader("x-code", "200");
    }
    return response;
}
Also used : ApiResponse(io.swagger.annotations.ApiResponse) Response(org.apache.servicecomb.swagger.invocation.Response) MultiResponse200(org.apache.servicecomb.demo.multiErrorCode.MultiResponse200) MultiResponse400(org.apache.servicecomb.demo.multiErrorCode.MultiResponse400) InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException) MultiResponse500(org.apache.servicecomb.demo.multiErrorCode.MultiResponse500) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) ResponseHeaders(org.apache.servicecomb.swagger.extend.annotations.ResponseHeaders) ApiResponses(io.swagger.annotations.ApiResponses)

Example 2 with MultiResponse500

use of org.apache.servicecomb.demo.multiErrorCode.MultiResponse500 in project java-chassis by ServiceComb.

the class MultiErrorCodeService method errorCode.

@Path("/errorCode")
@POST
@ApiResponses({ @ApiResponse(code = 200, response = MultiResponse200.class, message = ""), @ApiResponse(code = 400, response = MultiResponse400.class, message = ""), @ApiResponse(code = 500, response = MultiResponse500.class, message = "") })
public MultiResponse200 errorCode(MultiRequest request) {
    if (request.getCode() == 400) {
        MultiResponse400 r = new MultiResponse400();
        r.setCode(400);
        r.setMessage("bad request");
        throw new InvocationException(javax.ws.rs.core.Response.Status.BAD_REQUEST, r);
    } else if (request.getCode() == 500) {
        MultiResponse500 r = new MultiResponse500();
        r.setCode(500);
        r.setMessage("internal error");
        throw new InvocationException(javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR, r);
    } else {
        MultiResponse200 r = new MultiResponse200();
        r.setCode(200);
        r.setMessage("success result");
        return r;
    }
}
Also used : MultiResponse200(org.apache.servicecomb.demo.multiErrorCode.MultiResponse200) MultiResponse400(org.apache.servicecomb.demo.multiErrorCode.MultiResponse400) InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException) MultiResponse500(org.apache.servicecomb.demo.multiErrorCode.MultiResponse500) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) ApiResponses(io.swagger.annotations.ApiResponses)

Example 3 with MultiResponse500

use of org.apache.servicecomb.demo.multiErrorCode.MultiResponse500 in project java-chassis by ServiceComb.

the class MultiErrorCodeService method errorCodeWithHeaderJAXRS.

// using JAX-RS providers, users need to add dependencies for implementations, see pom for an example.
@Path("/errorCodeWithHeaderJAXRS")
@POST
@ApiResponses({ @ApiResponse(code = 200, response = MultiResponse200.class, message = ""), @ApiResponse(code = 400, response = MultiResponse400.class, message = ""), @ApiResponse(code = 500, response = MultiResponse500.class, message = "") })
@ResponseHeaders({ @ResponseHeader(name = "x-code", response = String.class) })
public javax.ws.rs.core.Response errorCodeWithHeaderJAXRS(MultiRequest request) {
    javax.ws.rs.core.Response response;
    if (request.getCode() == 400) {
        MultiResponse400 r = new MultiResponse400();
        r.setCode(request.getCode());
        r.setMessage(request.getMessage());
        // If got many types for different status code, we can only using InvocationException for failed error code like 400-500.
        // The result for Failed Family(e.g. 400-500), can not set return value as target type directly or will give exception.
        response = javax.ws.rs.core.Response.status(Status.BAD_REQUEST).entity(new InvocationException(Status.BAD_REQUEST, r)).header("x-code", "400").build();
    } else if (request.getCode() == 500) {
        MultiResponse500 r = new MultiResponse500();
        r.setCode(request.getCode());
        r.setMessage(request.getMessage());
        response = javax.ws.rs.core.Response.status(Status.INTERNAL_SERVER_ERROR).entity(new InvocationException(Status.INTERNAL_SERVER_ERROR, r)).header("x-code", "500").build();
    } else {
        MultiResponse200 r = new MultiResponse200();
        r.setCode(request.getCode());
        r.setMessage(request.getMessage());
        // If error code is OK family(like 200), we can use the target type.
        response = javax.ws.rs.core.Response.status(Status.OK).entity(r).header("x-code", "200").build();
    }
    return response;
}
Also used : MultiResponse200(org.apache.servicecomb.demo.multiErrorCode.MultiResponse200) MultiResponse400(org.apache.servicecomb.demo.multiErrorCode.MultiResponse400) InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException) MultiResponse500(org.apache.servicecomb.demo.multiErrorCode.MultiResponse500) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) ResponseHeaders(org.apache.servicecomb.swagger.extend.annotations.ResponseHeaders) ApiResponses(io.swagger.annotations.ApiResponses)

Example 4 with MultiResponse500

use of org.apache.servicecomb.demo.multiErrorCode.MultiResponse500 in project java-chassis by ServiceComb.

the class MultiErrorCodeServiceClient method testErrorCode.

private static void testErrorCode() {
    MultiRequest request = new MultiRequest();
    request.setCode(200);
    ResponseEntity<MultiResponse200> result = template.postForEntity(SERVER + "/MultiErrorCodeService/errorCode", request, MultiResponse200.class);
    TestMgr.check(result.getStatusCodeValue(), 200);
    TestMgr.check(result.getBody().getMessage(), "success result");
    request.setCode(400);
    MultiResponse400 t400 = null;
    try {
        template.postForEntity(SERVER + "/MultiErrorCodeService/errorCode", request, MultiResponse400.class);
    } catch (InvocationException e) {
        t400 = (MultiResponse400) e.getErrorData();
    }
    TestMgr.check(t400.getCode(), 400);
    TestMgr.check(t400.getMessage(), "bad request");
    request.setCode(500);
    MultiResponse500 t500 = null;
    try {
        template.postForEntity(SERVER + "/MultiErrorCodeService/errorCode", request, MultiResponse400.class);
    } catch (InvocationException e) {
        t500 = (MultiResponse500) e.getErrorData();
    }
    TestMgr.check(t500.getCode(), 500);
    TestMgr.check(t500.getMessage(), "internal error");
}
Also used : MultiRequest(org.apache.servicecomb.demo.multiErrorCode.MultiRequest) MultiResponse200(org.apache.servicecomb.demo.multiErrorCode.MultiResponse200) MultiResponse400(org.apache.servicecomb.demo.multiErrorCode.MultiResponse400) InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException) MultiResponse500(org.apache.servicecomb.demo.multiErrorCode.MultiResponse500)

Example 5 with MultiResponse500

use of org.apache.servicecomb.demo.multiErrorCode.MultiResponse500 in project java-chassis by ServiceComb.

the class MultiErrorCodeServiceClient method testErrorCodeWithHeaderJAXRS.

private static void testErrorCodeWithHeaderJAXRS() {
    MultiRequest request = new MultiRequest();
    request.setCode(200);
    request.setMessage("success result");
    ResponseEntity<MultiResponse200> result = template.postForEntity(SERVER + "/MultiErrorCodeService/errorCodeWithHeaderJAXRS", request, MultiResponse200.class);
    TestMgr.check(result.getStatusCodeValue(), 200);
    TestMgr.check(result.getBody().getMessage(), "success result");
    TestMgr.check(result.getBody().getCode(), 200);
    TestMgr.check(result.getHeaders().getFirst("x-code"), 200);
    request.setCode(400);
    request.setMessage("bad request");
    MultiResponse400 t400 = null;
    try {
        template.postForEntity(SERVER + "/MultiErrorCodeService/errorCodeWithHeaderJAXRS", request, MultiResponse400.class);
    } catch (InvocationException e) {
        t400 = (MultiResponse400) e.getErrorData();
        TestMgr.check(e.getStatus().getStatusCode(), Status.BAD_REQUEST.getStatusCode());
    }
    TestMgr.check(t400.getCode(), 400);
    TestMgr.check(t400.getMessage(), "bad request");
    request.setCode(500);
    request.setMessage("internal error");
    MultiResponse500 t500 = null;
    try {
        template.postForEntity(SERVER + "/MultiErrorCodeService/errorCodeWithHeaderJAXRS", request, MultiResponse400.class);
    } catch (InvocationException e) {
        t500 = (MultiResponse500) e.getErrorData();
        TestMgr.check(e.getStatus().getStatusCode(), Status.INTERNAL_SERVER_ERROR.getStatusCode());
    }
    TestMgr.check(t500.getCode(), 500);
    TestMgr.check(t500.getMessage(), "internal error");
}
Also used : MultiRequest(org.apache.servicecomb.demo.multiErrorCode.MultiRequest) MultiResponse200(org.apache.servicecomb.demo.multiErrorCode.MultiResponse200) MultiResponse400(org.apache.servicecomb.demo.multiErrorCode.MultiResponse400) InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException) MultiResponse500(org.apache.servicecomb.demo.multiErrorCode.MultiResponse500)

Aggregations

MultiResponse200 (org.apache.servicecomb.demo.multiErrorCode.MultiResponse200)6 MultiResponse400 (org.apache.servicecomb.demo.multiErrorCode.MultiResponse400)6 MultiResponse500 (org.apache.servicecomb.demo.multiErrorCode.MultiResponse500)6 InvocationException (org.apache.servicecomb.swagger.invocation.exception.InvocationException)6 ApiResponses (io.swagger.annotations.ApiResponses)3 POST (javax.ws.rs.POST)3 Path (javax.ws.rs.Path)3 MultiRequest (org.apache.servicecomb.demo.multiErrorCode.MultiRequest)3 ResponseHeaders (org.apache.servicecomb.swagger.extend.annotations.ResponseHeaders)2 ApiResponse (io.swagger.annotations.ApiResponse)1 Response (org.apache.servicecomb.swagger.invocation.Response)1