我正在写一个Restful Webservice Impl,我通过注释@Produces("application/json")
来消费和生成JSON格式的响应。我也在制作JSON响应。这里使用具有错误代码和错误消息的类来处理异常。当我获得异常时,它不会以application/json
格式生成。我使用ExceptionMapper
来查找解决方案,但它是`text / plain格式。
段
public Class Confiuration{
@Path("getData")
@Consumes("application/json")
@Produces("application/json")
public JSONGetDataResponseVo getData(GetDataRequestVo datarequestVO)
throws FaultResponse {
JSONGetDataResponseVo response=new JSONGetDataResponseVo ();
DataServiceValidator.validateGetConfigurationAndDataRequest(datarequestVO);
....
....
}catch(ApplicationException applicationException){
throw new FaultResponse(applicationException,locale);
}
}
FaultResponseMapper
@Provider
public class FaultResponseMapper implements ExceptionMapper<FaultResponse> {
@Context
private HttpHeaders headers;
public Response toResponse(FaultResponse faultResponse) {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity(faultResponse).type(MediaType.APPLICATION_JSON).build();
}
}
应用程序异常
public abstract class ApplicationException extends Exception{
private java.lang.String errorCode;
public ApplicationException(String errorCode, String message) {
super(message);
this.errorCode = errorCode;
}
public ApplicationException(String message) {
super(message);
}
public java.lang.String getErrorCode() {
return this.errorCode;
}
public abstract String getLocaleMessage(Locale locale);
}
FaultResponse
public class FaultResponse extends WebApplicationException {
private String errorCode;
private String errorMessage;
private String localErrorMessage;
public FaultResponse(String errorCode, String errorMessage,
String localErrorMessage) {
this.errorCode = errorCode;
this.errorMessage = errorMessage;
this.localErrorMessage = localErrorMessage;
}
public FaultResponse(ApplicationException applicationException,
Locale locale) {
this.errorCode = applicationException.getErrorCode();
this.errorMessage = applicationException.getMessage();
if (locale != null
&& applicationException.getLocaleMessage(locale) != null) {
this.localErrorMessage = applicationException
.getLocaleMessage(locale);
} else {
this.localErrorMessage = applicationException.getMessage();
}
}
}
所以我在这里如何以JSON格式生成我的faultResponse。
答案 0 :(得分:1)
这与您将异常作为响应返回这一事实有关。我会
ApplicationException
制作例外映射器。 FaultResponse
不扩展和异常。只需在映射器中创建它。throws ApplicationException
。你不需要抓住它并重新抛出。 我做了这些修改,但效果很好。
为FaultResponse
添加了getter(编组所需)并删除了异常扩展
public class FaultResponse {
...
public String getErrorCode() { return errorCode; }
public String getErrorMessage() { return errorMessage; }
public String getLocalErrorMessage() { return localErrorMessage; }
...
}
为测试和ApplicationException
实施
public class ApplicationExceptionImpl extends ApplicationException {
public ApplicationExceptionImpl(){
this("400", "Bad Request");
}
public ApplicationExceptionImpl(String errorCode, String message) {
super(errorCode, message);
}
@Override
public String getLocaleMessage(Locale locale) {
return "Bad Request";
}
}
public class FaultService {
public void doSomething() throws ApplicationException {
throw new ApplicationExceptionImpl();
}
}
资源类
@Path("fault")
public class FaultResource {
FaultService service = new FaultService();
@GET
public Response getException() throws ApplicationException {
service.doSomething();
return Response.ok("Cool").build();
}
}
ExceptionMapper
@Provider
public class ApplicationExceptionMapper implements ExceptionMapper<ApplicationException> {
@Override
public Response toResponse(ApplicationException exception) {
FaultResponse response = new FaultResponse(exception, Locale.ENGLISH);
return Response.status(Response.Status.BAD_REQUEST)
.entity(response).type(MediaType.APPLICATION_JSON).build();
}
}
ApplicationException类保持不变
curl -v http://localhost:8080/api/fault
{"errorCode":"400","errorMessage":"Bad Request","localErrorMessage":"Bad Request"}
如果在此之后您还没有看到JSON,那么您可能没有配置提供程序。如果是这种情况,请显示您的应用程序配置以及项目依赖项。