如何调试夸库斯/小黑麦客户端请求

我有一个看起来像这样的请求:


@Path("/v1")

@RegisterRestClient

@Produces("application/json")

public interface VaultClient {

    @POST

    @Path("/auth/jwt/login")

    @Consumes("application/json")

    String getVaultToken(LoginPayload loginPayload);

}

登录支付加载它只是一个简单的POJO:


public class LoginPayload {

    private String jwt;

    final private String role = "some-service";


    public void setJwt(String _jwt) {

        this.jwt = _jwt;

    }

}

当我尝试通过服务调用此终结点时:


public String getServiceJwt() {

    String loginJwt = getLoginJwt();

    LoginPayload loginPayload = new LoginPayload();

    loginPayload.setJwt(loginJwt);

    try {

        System.out.println(loginPayload.toString());

        String tokenResponse = vaultClient.getVaultToken(loginPayload);

        System.out.println("##################");

        System.out.println(tokenResponse);

    } catch (Exception e) {

        System.out.println(e);

    }

    return vaultJwt;

}

我得到一个400:


javax.ws.rs.WebApplicationException: Unknown error, status code 400

java.lang.RuntimeException: method call not supported

但是,我不知道如何对此进行故障排除。我可以通过PostMan /失眠执行相同的请求,它返回的响应很好。有没有办法让我更好地内省出的响应是什么样子的?也许它没有正确地将POJO序列化为JSON?我无从得知。


更新 我在此请求的另一端抛出了一个节点服务器,并注销了正文。它是空的。因此,有些东西不是序列化POJO并将其与POST请求一起发送。不过,这不是一个很好的调试故事。有没有办法在不记录此请求的另一端的情况下获得此内容?


另外,为什么POJO不序列化?它非常密切地遵循所有文档。


慕虎7371278
浏览 51回答 1
1回答

心有法竹

我使用过滤器和拦截器作为异常处理程序来解决此问题:用于打印日志的过滤器:import lombok.extern.java.Log;import org.glassfish.jersey.message.MessageUtils;import javax.ws.rs.WebApplicationException;import javax.ws.rs.client.ClientRequestContext;import javax.ws.rs.client.ClientRequestFilter;import javax.ws.rs.client.ClientResponseContext;import javax.ws.rs.client.ClientResponseFilter;import javax.ws.rs.container.ContainerRequestContext;import javax.ws.rs.container.ContainerRequestFilter;import javax.ws.rs.container.ContainerResponseContext;import javax.ws.rs.container.ContainerResponseFilter;import javax.ws.rs.core.MultivaluedMap;import javax.ws.rs.ext.WriterInterceptor;import javax.ws.rs.ext.WriterInterceptorContext;import java.io.BufferedInputStream;import java.io.ByteArrayOutputStream;import java.io.FilterOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.URI;import java.nio.charset.Charset;import java.util.Comparator;import java.util.List;import java.util.Map;import java.util.Set;import java.util.TreeSet;import java.util.concurrent.atomic.AtomicLong;/**&nbsp;* Based on org.glassfish.jersey.filter.LoggingFilter&nbsp;*/@Logpublic class LoggingFilter implements ContainerRequestFilter, ClientRequestFilter, ContainerResponseFilter,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ClientResponseFilter, WriterInterceptor {&nbsp; &nbsp; private static final String NOTIFICATION_PREFIX = "* ";&nbsp; &nbsp; private static final String REQUEST_PREFIX = "> ";&nbsp; &nbsp; private static final String RESPONSE_PREFIX = "< ";&nbsp; &nbsp; private static final String ENTITY_LOGGER_PROPERTY = LoggingFilter.class.getName() + ".entityLogger";&nbsp; &nbsp; private static final String LOGGING_ID_PROPERTY = LoggingFilter.class.getName() + ".id";&nbsp; &nbsp; private static final Comparator<Map.Entry<String, List<String>>> COMPARATOR = (o1, o2) -> o1.getKey().compareToIgnoreCase(o2.getKey());&nbsp; &nbsp; private static final int DEFAULT_MAX_ENTITY_SIZE = 8 * 1024;&nbsp; &nbsp; private final AtomicLong _id = new AtomicLong(0);&nbsp; &nbsp; private final int maxEntitySize;&nbsp; &nbsp; public LoggingFilter() {&nbsp; &nbsp; &nbsp; &nbsp; this.maxEntitySize = LoggingFilter.DEFAULT_MAX_ENTITY_SIZE;&nbsp; &nbsp; }&nbsp; &nbsp; private void log(final StringBuilder b) {&nbsp; &nbsp; &nbsp; &nbsp; LoggingFilter.log.info(b.toString());&nbsp; &nbsp; }&nbsp; &nbsp; private StringBuilder prefixId(final StringBuilder b, final long id) {&nbsp; &nbsp; &nbsp; &nbsp; b.append(id).append(" ");&nbsp; &nbsp; &nbsp; &nbsp; return b;&nbsp; &nbsp; }&nbsp; &nbsp; private void printRequestLine(final StringBuilder b, final String note, final long id, final String method, final URI uri) {&nbsp; &nbsp; &nbsp; &nbsp; this.prefixId(b, id).append(LoggingFilter.NOTIFICATION_PREFIX)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .append(note)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .append(" on thread ").append(Thread.currentThread().getName())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .append("\n");&nbsp; &nbsp; &nbsp; &nbsp; this.prefixId(b, id).append(LoggingFilter.REQUEST_PREFIX).append(method).append(" ")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .append(uri.toASCIIString()).append("\n");&nbsp; &nbsp; }&nbsp; &nbsp; private void printResponseLine(final StringBuilder b, final String note, final long id, final int status) {&nbsp; &nbsp; &nbsp; &nbsp; this.prefixId(b, id).append(LoggingFilter.NOTIFICATION_PREFIX)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .append(note)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .append(" on thread ").append(Thread.currentThread().getName()).append("\n");&nbsp; &nbsp; &nbsp; &nbsp; this.prefixId(b, id).append(LoggingFilter.RESPONSE_PREFIX)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .append(status)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .append("\n");&nbsp; &nbsp; }&nbsp; &nbsp; private void printPrefixedHeaders(final StringBuilder b,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final long id,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final String prefix,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final MultivaluedMap<String, String> headers) {&nbsp; &nbsp; &nbsp; &nbsp; for (final Map.Entry<String, List<String>> headerEntry : this.getSortedHeaders(headers.entrySet())) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final List<?> val = headerEntry.getValue();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final String header = headerEntry.getKey();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(val.size() == 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.prefixId(b, id).append(prefix).append(header).append(": ").append(val.get(0)).append("\n");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final StringBuilder sb = new StringBuilder();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; boolean add = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (final Object s : val) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(add) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sb.append(',');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; add = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sb.append(s);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.prefixId(b, id).append(prefix).append(header).append(": ").append(sb.toString()).append("\n");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; private Set<Map.Entry<String, List<String>>> getSortedHeaders(final Set<Map.Entry<String, List<String>>> headers) {&nbsp; &nbsp; &nbsp; &nbsp; final TreeSet<Map.Entry<String, List<String>>> sortedHeaders = new TreeSet<>(LoggingFilter.COMPARATOR);&nbsp; &nbsp; &nbsp; &nbsp; sortedHeaders.addAll(headers);&nbsp; &nbsp; &nbsp; &nbsp; return sortedHeaders;&nbsp; &nbsp; }&nbsp; &nbsp; private InputStream logInboundEntity(final StringBuilder b, InputStream stream, final Charset charset) throws IOException {&nbsp; &nbsp; &nbsp; &nbsp; if(!stream.markSupported()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stream = new BufferedInputStream(stream);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; stream.mark(this.maxEntitySize + 1);&nbsp; &nbsp; &nbsp; &nbsp; final byte[] entity = new byte[this.maxEntitySize + 1];&nbsp; &nbsp; &nbsp; &nbsp; final int entitySize = stream.read(entity);&nbsp; &nbsp; &nbsp; &nbsp; b.append(new String(entity, 0, Math.min(entitySize, this.maxEntitySize), charset));&nbsp; &nbsp; &nbsp; &nbsp; if(entitySize > this.maxEntitySize) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; b.append("...more...");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; b.append('\n');&nbsp; &nbsp; &nbsp; &nbsp; stream.reset();&nbsp; &nbsp; &nbsp; &nbsp; return stream;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void filter(final ClientRequestContext context) throws IOException {&nbsp; &nbsp; &nbsp; &nbsp; final long id = this._id.incrementAndGet();&nbsp; &nbsp; &nbsp; &nbsp; context.setProperty(LoggingFilter.LOGGING_ID_PROPERTY, id);&nbsp; &nbsp; &nbsp; &nbsp; final StringBuilder b = new StringBuilder();&nbsp; &nbsp; &nbsp; &nbsp; this.printRequestLine(b, "Sending client request", id, context.getMethod(), context.getUri());&nbsp; &nbsp; &nbsp; &nbsp; this.printPrefixedHeaders(b, id, LoggingFilter.REQUEST_PREFIX, context.getStringHeaders());&nbsp; &nbsp; &nbsp; &nbsp; if(context.hasEntity()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final OutputStream stream = new LoggingFilter.LoggingStream(b, context.getEntityStream());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; context.setEntityStream(stream);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; context.setProperty(LoggingFilter.ENTITY_LOGGER_PROPERTY, stream);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // not calling log(b) here - it will be called by the interceptor&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.log(b);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void filter(final ClientRequestContext requestContext, final ClientResponseContext responseContext)&nbsp; &nbsp; throws IOException {&nbsp; &nbsp; &nbsp; &nbsp; final Object requestId = requestContext.getProperty(LoggingFilter.LOGGING_ID_PROPERTY);&nbsp; &nbsp; &nbsp; &nbsp; final long id = requestId != null ? (Long) requestId : this._id.incrementAndGet();&nbsp; &nbsp; &nbsp; &nbsp; final StringBuilder b = new StringBuilder();&nbsp; &nbsp; &nbsp; &nbsp; this.printResponseLine(b, "Client response received", id, responseContext.getStatus());&nbsp; &nbsp; &nbsp; &nbsp; this.printPrefixedHeaders(b, id, LoggingFilter.RESPONSE_PREFIX, responseContext.getHeaders());&nbsp; &nbsp; &nbsp; &nbsp; if(responseContext.hasEntity()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; responseContext.setEntityStream(this.logInboundEntity(b, responseContext.getEntityStream(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MessageUtils.getCharset(responseContext.getMediaType())));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; this.log(b);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void filter(final ContainerRequestContext context) throws IOException {&nbsp; &nbsp; &nbsp; &nbsp; final long id = this._id.incrementAndGet();&nbsp; &nbsp; &nbsp; &nbsp; context.setProperty(LoggingFilter.LOGGING_ID_PROPERTY, id);&nbsp; &nbsp; &nbsp; &nbsp; final StringBuilder b = new StringBuilder();&nbsp; &nbsp; &nbsp; &nbsp; this.printRequestLine(b, "Server has received a request", id, context.getMethod(), context.getUriInfo().getRequestUri());&nbsp; &nbsp; &nbsp; &nbsp; this.printPrefixedHeaders(b, id, LoggingFilter.REQUEST_PREFIX, context.getHeaders());&nbsp; &nbsp; &nbsp; &nbsp; if(context.hasEntity()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; context.setEntityStream(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.logInboundEntity(b, context.getEntityStream(), MessageUtils.getCharset(context.getMediaType())));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; this.log(b);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void filter(final ContainerRequestContext requestContext, final ContainerResponseContext responseContext)&nbsp; &nbsp; throws IOException {&nbsp; &nbsp; &nbsp; &nbsp; final Object requestId = requestContext.getProperty(LoggingFilter.LOGGING_ID_PROPERTY);&nbsp; &nbsp; &nbsp; &nbsp; final long id = requestId != null ? (Long) requestId : this._id.incrementAndGet();&nbsp; &nbsp; &nbsp; &nbsp; final StringBuilder b = new StringBuilder();&nbsp; &nbsp; &nbsp; &nbsp; this.printResponseLine(b, "Server responded with a response", id, responseContext.getStatus());&nbsp; &nbsp; &nbsp; &nbsp; this.printPrefixedHeaders(b, id, LoggingFilter.RESPONSE_PREFIX, responseContext.getStringHeaders());&nbsp; &nbsp; &nbsp; &nbsp; if(responseContext.hasEntity()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final OutputStream stream = new LoggingFilter.LoggingStream(b, responseContext.getEntityStream());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; responseContext.setEntityStream(stream);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; requestContext.setProperty(LoggingFilter.ENTITY_LOGGER_PROPERTY, stream);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // not calling log(b) here - it will be called by the interceptor&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.log(b);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void aroundWriteTo(final WriterInterceptorContext writerInterceptorContext)&nbsp; &nbsp; throws IOException, WebApplicationException {&nbsp; &nbsp; &nbsp; &nbsp; final LoggingFilter.LoggingStream stream = (LoggingFilter.LoggingStream) writerInterceptorContext.getProperty(LoggingFilter.ENTITY_LOGGER_PROPERTY);&nbsp; &nbsp; &nbsp; &nbsp; writerInterceptorContext.proceed();&nbsp; &nbsp; &nbsp; &nbsp; if(stream != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.log(stream.getStringBuilder(MessageUtils.getCharset(writerInterceptorContext.getMediaType())));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; private class LoggingStream extends FilterOutputStream {&nbsp; &nbsp; &nbsp; &nbsp; private final StringBuilder b;&nbsp; &nbsp; &nbsp; &nbsp; private final ByteArrayOutputStream baos = new ByteArrayOutputStream();&nbsp; &nbsp; &nbsp; &nbsp; LoggingStream(final StringBuilder b, final OutputStream inner) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super(inner);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.b = b;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; StringBuilder getStringBuilder(final Charset charset) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // write entity to the builder&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final byte[] entity = this.baos.toByteArray();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.b.append(new String(entity, 0, Math.min(entity.length, LoggingFilter.this.maxEntitySize), charset));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(entity.length > LoggingFilter.this.maxEntitySize) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.b.append("...more...");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.b.append('\n');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return this.b;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void write(final int i) throws IOException {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(this.baos.size() <= LoggingFilter.this.maxEntitySize) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.baos.write(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.out.write(i);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}在其余客户端界面中使用筛选器:@Path("/")@Produces(MediaType.APPLICATION_JSON)@Consumes(MediaType.APPLICATION_JSON)@RegisterRestClient@RegisterProvider(LoggingFilter.class)public interface Api {&nbsp; &nbsp; @GET&nbsp; &nbsp; @Path("/foo/bar")&nbsp; &nbsp; FooBar getFoorBar();现在,请求和响应负载将打印在日志中。之后,用于处理异常的侦听器:限定 符:@InterceptorBinding@Target({TYPE, METHOD})@Retention(RUNTIME)public @interface ExceptionHandler {}拦截 器:@Interceptor@ExceptionHandlerpublic class ExceptionHandlerInterceptor&nbsp; {&nbsp; &nbsp; @AroundInvoke&nbsp; &nbsp; public Object processRequest(final InvocationContext invocationContext) {&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return invocationContext.proceed();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; catch (final WebApplicationException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final int status = e.getResponse().getStatus();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final String errorJson = e.getResponse().readEntity(String.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final Jsonb jsonb = JsonbBuilder.create();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //"ErrorMessageDTO" is waited when a error occurs&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ErrorMessage errorMessage = jsonb.fromJson(errorJson, ErrorMessage.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //isValid method verifies if the conversion was successful&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(errorMessage.isValid()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Response&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .status(status)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .entity(errorMessage)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .build();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; errorMessage = ErrorMessage&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .builder()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .statusCode(status)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .statusMessage(e.getMessage())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .success(false)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .build();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Response&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .status(status)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .entity(errorMessage)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .build();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; catch (final Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Response&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .status(Status.INTERNAL_SERVER_ERROR)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .entity(ErrorMessage&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .builder()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .statusCode(Status.INTERNAL_SERVER_ERROR.getStatusCode())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .statusMessage(e.getMessage())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .success(false)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .build())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .build();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}使用拦截器:@Path("/resource")@Produces(MediaType.APPLICATION_JSON)@Consumes(MediaType.APPLICATION_JSON)@ExceptionHandler@Traced@Logpublic class ResourceEndpoint {&nbsp; &nbsp; @Inject&nbsp; &nbsp; @RestClient&nbsp; &nbsp; Api api;&nbsp; &nbsp; @GET&nbsp; &nbsp; @Path("/latest")&nbsp; &nbsp; public Response getFooBarLatest() {&nbsp; &nbsp; &nbsp; &nbsp; return Response.ok(this.api.getFoorBar()).build();&nbsp; &nbsp; }错误消息豆:@RegisterForReflection@Getter@Setter@AllArgsConstructor@NoArgsConstructor@Data@Builderpublic class ErrorMessage&nbsp; {&nbsp; &nbsp; @JsonbProperty("status_message")&nbsp; &nbsp; private String statusMessage;&nbsp; &nbsp; @JsonbProperty("status_code")&nbsp; &nbsp; private Integer statusCode;&nbsp; &nbsp; @JsonbProperty("success")&nbsp; &nbsp; private boolean success = true;&nbsp; &nbsp; @JsonbTransient&nbsp; &nbsp; public boolean isValid() {&nbsp; &nbsp; &nbsp; &nbsp; return this.statusMessage != null && !this.statusMessage.isEmpty() && this.statusCode != null;&nbsp; &nbsp; }}PS:使用龙目岛!
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java