我已定义以下异常映射器来处理自定义<mvx:MvxContentPage
xmlns:mvx="clr-namespace:MvvmCross.Forms.Views;assembly=MvvmCross.Forms"
x:TypeArguments="viewModels:CategoryListViewModel"
xmlns:viewModels=""
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="GSP.X.AccuStore.Forms.Views.Site.Profiles.CategoryListView">
<ContentPage.ToolbarItems>
<ToolbarItem Text="Areas" Command="{Binding Path=GoToAreasCommand}" />
</ContentPage.ToolbarItems>
</mvx:MvxContentPage>
HttpCustomException
当在代码中抛出package com.myapp.apis;
import com.myapp.apis.model.HttpCustomException;
import com.myapp.apis.model.HttpCustomExceptionStatusType;
import com.myapp.apis.model.HttpCustomNotAuthorizedException;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
@Provider
public class HttpCustomExceptionMapper implements ExceptionMapper <HttpCustomException> {
@Override
public Response toResponse (HttpCustomException e) {
Response response = Response.status (new HttpCustomExceptionStatusType (Response.Status.UNAUTHORIZED, e.getMessage ())).build ();
if (e instanceof HttpCustomNotAuthorizedException) {
response = Response.status (new HttpCustomExceptionStatusType (Response.Status.INTERNAL_SERVER_ERROR, e.getMessage ())).build ();
}
System.out.println ("HttpCustomExceptionMapper: " + response.getStatus () + ", " + response.getStatusInfo ().getReasonPhrase ());
return response;
}
}
时,我可以在catalina.out中看到HttpCustomNotAuthorizedException
方法末尾定义的日志消息。因此,在请求处理期间将调用HttpCustomExceptionMapper类。但是,在最终响应中,我在客户端看到的是,我只看到标准的toResponse
消息,而不是我在响应中设置的自定义消息。
为什么会这样?
答案 0 :(得分:1)
它显示标准响应,因为您没有为响应设置主体,而只是设置状态。 您必须像这样传递自定义响应:
Response response = Response.status (Response.Status.INTERNAL_SERVER_ERROR).entity(CustomResponse).build ();
我不能接受这种设计,但是会是这样的:
HttpCustomExceptionStatusType status = new HttpCustomExceptionStatusType (Response.Status.INTERNAL_SERVER_ERROR, e.getMessage ())
Response response = Response.status (status).entity(status).build ();
答案 1 :(得分:1)
您应该注意,当前版本的HTTP协议(HTTP / 2)完全不支持原因短语。它已从协议中删除。
因此,从Tomcat 9.0开始已删除对发送原因短语的支持。在Tomcat 7.0中,默认情况下已禁用8.5对发送自定义原因短语的支持(可以通过system property,org.apache.coyote.USE_CUSTOM_STATUS_MSG_IN_HEADER
启用)。在8.5中,您还应该在Connector上设置sendReasonPhrase="true"
。