jsonrpc4j
jsonrpc4j copied to clipboard
JsonRpcRestClient class can't correctly process exception transition
JsonRpcRestClient class can't correctly process new mode of checked exception transition because HTTP status code used for that differs from HTTP_OK.
Can you provide more detail on this one? Which line of code is causing that?
com.googlecode.jsonrpc4j.JsonRpcServer#126 - uses DefaultHttpStatusCodeProvider.INSTANCE to translate to HTTP response code; In cause of exception response code is 500. Previous versions of jsonrpc4j (for example 1.2.0), server always returned HTTP 200.
@im-scooter Hi, I'm facing the same issue with exception response change to HTTP 500 from 200 after upgrading to jsonrpc4j 1.5.2.
It breaks my UI because it only checks for the "error" json object in the 200 OK response.
Do you have a solution? I am thinking to fork the jsonrpc4j 1.5.2 and manually change the HTTP 200 return code for jsonrpc error -32001 in class DefaultHttpStatusCodeProvider::getHttpStatusCode for error. Any suggestion?
@davidyyuan Hi. You can just write your own version of com.googlecode.jsonrpc4j.spring.rest.JsonRpcResponseErrorHandler where you can mark HTTP 500 code as OK. Just use this implementation in associated RestTemplate.
@im-scooter
thanks. I took a different approach. What I did is to implement my own HttpStatusCodeProvider and register it to the JsonServiceExporter instead of relying on the DefaultHttpStatusCodeProvider. Here is the code provider class:
` public class MyHttpStatusCodeProvider implements HttpStatusCodeProvider {
private static final Logger logger = LoggerFactory.getLogger(MyHttpStatusCodeProvider.class);
@Override
public int getHttpStatusCode(int resultCode) {
logger.debug("resultCode={}", resultCode);
if (resultCode == 0 || resultCode == -32001) {
return HttpStatus.OK.value();
} else if (resultCode == -32600) {
return HttpStatus.BAD_REQUEST.value();
} else if (resultCode == -32601) {
return HttpStatus.NOT_FOUND.value();
} else {
return HttpStatus.INTERNAL_SERVER_ERROR.value();
}
}
@Override
public Integer getJsonRpcCode(int httpStatusCode) {
logger.debug("httpStatusCode={}", httpStatusCode);
if (httpStatusCode == HttpStatus.OK.value()) {
return 0;
} else if (httpStatusCode == HttpStatus.INTERNAL_SERVER_ERROR.value()) {
return -32603;
} else if (httpStatusCode == HttpStatus.NOT_FOUND.value()) {
return -32601;
} else if (httpStatusCode == HttpStatus.BAD_REQUEST.value()) {
return -32700;
} else {
return null;
}
}
}` And wire it to a JsonRpcExporter in the Java Config:
` @Autowired MyService myService;
@Bean(name = "/myService") public JsonServiceExporter jsonServiceExporterAclService() { JsonServiceExporter exporter = new JsonServiceExporter(); exporter.setService(myService); exporter.setServiceInterface(AclService.class); exporter.setHttpStatusCodeProvider(httpStatusCodeProvider()); return exporter; }`
This is how I fixed this running without spring (this falls back on the default behavior except for on ERROR_NOT_HANDLED aka -32001):
` public class MyHttpStatusCodeProvider implements HttpStatusCodeProvider { private static final HttpStatusCodeProvider DEFAULT = DefaultHttpStatusCodeProvider.INSTANCE;
@Override
public int getHttpStatusCode(final int i) {
if (i == ErrorResolver.JsonError.ERROR_NOT_HANDLED.code) {
return HttpURLConnection.HTTP_OK;
}
return DEFAULT.getHttpStatusCode(i);
}
@Override
public Integer getJsonRpcCode(final int i) {
return DEFAULT.getJsonRpcCode(i);
}
}
`
BTW: After spending 5 minutes scratching my head, I can say with confidence that the code formatting in this editor is thoroughly broken.