Java REST客户端-javax.ws.rs.ProcessingException:javax / xml / bind / annotation / XmlTransient

问题描述

我正在使用Java 1.7,并试图创建一个客户端来查询REST api。

但是,执行客户端时出现以下错误。我在REST服务中设置了一个断点,但从未实现过。

错误

javax.ws.rs.ProcessingException: javax/xml/bind/annotation/XmlTransient

问题

有什么想法可以解决客户问题吗?

代码

pom.xml

    <!-- JAX-RS 2.x Client RI -->
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-client</artifactId>
    </dependency>

Client.java

import javax.ws.rs.BadRequestException;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

private static final String approvalSubmitUrl = "http://localhost:8081/rest/approvals-status/{evaluationRequestId}";

public Object getStatus(String evaluationRequestId,EvaluationProvidednotification providednotification) {
    logger.info(approvalSubmitUrl);
    EvaluationProvidednotificationDTO evaluationProvidednotificationDTO = new EvaluationProvidednotificationDTO();
    map(providednotification,evaluationProvidednotificationDTO);
    try {
        logger.info("Sending request to get status from " + approvalSubmitUrl + " (nexct-approval-service)");
        Client client = ClientBuilder.newClient();
        WebTarget target = client.target(approvalSubmitUrl);
        Response response = target.queryParam("evaluationRequestId",evaluationRequestId)
                .request(MediaType.APPLICATION_JSON_TYPE).post(Entity.json(evaluationProvidednotificationDTO));
        StatusResponse statusResponse = response.readEntity(StatusResponse.class);
        logger.info("Successfully got the status: " + statusResponse.status );
    }
    catch (BadRequestException refused) {
        logger.error("Request to: " + approvalSubmitUrl + " REFUSED: " + refused.getMessage());
    }
    catch (Exception e) {
        logger.error("Failed to get status from " + approvalSubmitUrl,e);
    }
    return null;
}

该服务仅供参考,因为我确定错误是客户端错误造成的。

Service.java (带有Java 14的Springboot)

@PostMapping(value = "/rest/approvals-status/{evaluationRequestId}",consumes=MediaType.APPLICATION_JSON_VALUE,produces={MediaType.APPLICATION_JSON_VALUE,MediaType.APPLICATION_XML_VALUE})
public @ResponseBody ResponseEntity<StatusDTO> getStatus(@PathVariable String evaluationRequestId,@RequestBody EvaluationProvidednotificationDTO providednotificationDTO) {
    StatusDTO status = new StatusDTO();
    status.setEvaluationRequestId(evaluationRequestId);
    if (providednotificationDTO.outcome == EvaluationProvidednotificationDTO.Evaluation.Approved) {
        if (pendingApprovals(evaluationRequestId)) {
            status.setStatus(StatusDTO.Status.Wait);
        } else {
            status.setStatus(StatusDTO.Status.Proceed);
        }
    } else {
        status.setStatus(StatusDTO.Status.Wait);
    }
    return ResponseEntity.ok(status);
}

这是要发布的对象:

EvaluationProvidednotificationDTO.java

public class EvaluationProvidednotificationDTO {

    public enum Evaluation {
        Approved,Rejected;
    }

    //any contraints on the request for example cost constraints
    public List<Serializable> constraints;

    //the outcome of the evaluation
    public Evaluation outcome;

    //any messages that the evaluator wants to send to the requestor
    public String messagetoRequestor;

    public List<Serializable> getConstraints() {
        return constraints;
    }

    public void setConstraints(List<Serializable> constraints) {
        this.constraints = constraints;
    }

    public Evaluation getoutcome() {
        return outcome;
    }

    public void setoutcome(Evaluation outcome) {
        this.outcome = outcome;
    }

    public String getMessagetoRequestor() {
        return messagetoRequestor;
    }

    public void setMessagetoRequestor(String messagetoRequestor) {
        this.messagetoRequestor = messagetoRequestor;
    }
}

解决方法

我设法通过将POJO转换为JSON来解决此问题:

        Invocation.Builder builder = target.request();
        Gson gson = new Gson();
        String json = gson.toJson(evaluationProvidedNotificationDTO);
        Entity entity = Entity.entity(json,MediaType.APPLICATION_JSON_TYPE);
        Response response = builder.post(entity);
        StatusResponse statusResponse = response.readEntity(StatusResponse.class);