简体   繁体   English

JAX-RS API故障响应到Java

[英]JAX-RS API Fault Response into java

I am accessing an API which is returning 404 error with a Fault message. 我正在访问一个API,该API返回带有错误消息的404错误。 I want to use the exact message in my java code. 我想在我的Java代码中使用确切的消息。 I am not sure how to access it. 我不确定如何访问它。

Normal response comes in element 正常反应进入要素

But this is coming in form of Fault and I want to access the message inside it. 但这是以故障的形式出现的,我想访问其中的消息。 Kindly suggest which class to use for it. 请建议使用哪个类。 Or any sample code snippents. 或任何示例代码片段。

Below is the sample responses in different format. 以下是不同格式的示例响应。

XML Response: XML回应:

<Fault xmlns="http://abc.cu.ms/api/resource">
   <Message>Service not found</Message>
</Fault>

JSON: JSON:

{"Message": "Service not found"}

RAW: 生的:

HTTP/1.1 404 Not Found
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
X-Powered-By: ASP.NET
Date: Tue, 13 Jan 2015 09:30:38 GMT
Content-Length: 37

{"Message":"Service not found"}

Whatever JAX-RS implementation you are using, it should have a client API. 无论您使用的是哪种JAX-RS实现,它都应该具有客户端API。 If you using a JAX-RS 2 implementation, there should be a standard Client API. 如果您使用JAX-RS 2实现,则应该有一个标准的Client API。 If you are using a JAX-RS 1 implementation, then the Client API will be specific to the implementation (and you will probably need to import a jar dependencies). 如果您使用的是JAX-RS 1实现,则Client API将特定于该实现(并且可能需要导入jar依赖项)。

For a JAX-RS 2 implementation, you could do something like this 对于JAX-RS 2实现,您可以执行以下操作

Client client = ClientBuilder.newBuilder();
WebTarget target = client.target(url);
Response response = target.request.accept(MediaType.APPLICATION_XML).get();
Fault fault = response.readEntity(Fault.class);
String message = fault.getMessage();

Your Fault class can simple look something like 您的Fault类可以简单地看起来像

@XmlRootElement(name = "Fault")
public class Fault {
    private String message;
    @XmlElement(name = "Message")
    public String getMessage() {
        return message;
    } 
    public void setMessage(String message) {
        this.message = message;
    }
}

For the namespace in the xml, you can simply add a package-info.java file the package the Fault class is in. It would look like 对于xml中的名称空间,您可以简单地添加Fault类所在的包中的package-info.java文件。

@XmlSchema(
    namespace = "http://www.example.org/package",
    elementFormDefault = XmlNsForm.QUALIFIED) 

package thepackage.of.your.fault.model;

import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;

You can read more about namespaces here . 您可以在此处阅读有关名称空间的更多信息。

For more information specific to your implementation, please provide more information as to what JAX-RS implementation you are using 有关特定于您的实现的更多信息,请提供有关您正在使用哪种JAX-RS实现的更多信息。

For JSON, you might need to add/register a provider, but again I would need to know what implementation you're working with to help with that. 对于JSON,您可能需要添加/注册一个提供程序,但是同样,我需要知道您正在使用哪种实现来提供帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM