Loading...
Loading...
Compare original and translation side by side
| Scenario | Pattern |
|---|---|
| API interface method | |
| Void-like operation | Return |
| Success result | Return value (DTO, primitive, etc.) |
| Failure result | Throw |
| DTO serialization | Use JAXB annotations ( |
| 场景 | 模式 |
|---|---|
| API接口方法 | |
| 类void操作 | 返回 |
| 成功结果 | 返回值(DTO、基本类型等) |
| 失败结果 | 抛出 |
| DTO序列化 | 使用JAXB注解( |
throws XmlRpcExceptionpublic interface SomeApi {
SomeDto someAction(SomeParameterDto request) throws XmlRpcException;
}<?xml version="1.0" encoding="UTF-8"?>
<methodResponse xmlns:ex="http://ws.apache.org/xmlrpc/namespaces/extensions">
<fault>
<value>
<struct>
<member>
<name>faultCode</name>
<value><i4>1</i4></value>
</member>
<member>
<name>faultString</name>
<value>failed to execute api</value>
</member>
</struct>
</value>
</fault>
</methodResponse>enabledForExceptionthrows XmlRpcExceptionpublic interface SomeApi {
SomeDto someAction(SomeParameterDto request) throws XmlRpcException;
}<?xml version="1.0" encoding="UTF-8"?>
<methodResponse xmlns:ex="http://ws.apache.org/xmlrpc/namespaces/extensions">
<fault>
<value>
<struct>
<member>
<name>faultCode</name>
<value><i4>1</i4></value>
</member>
<member>
<name>faultString</name>
<value>failed to execute api</value>
</member>
</struct>
</value>
</fault>
</methodResponse>enabledForExceptionvoidintpublic interface ServiceControlApi {
int start(String name) throws XmlRpcException;
}0XmlRpcExceptionvoidvoidvoidintvoidintpublic interface ServiceControlApi {
int start(String name) throws XmlRpcException;
}0XmlRpcExceptionvoidvoidvoidint| Outcome | How to Signal |
|---|---|
| Operation succeeded | Return value |
| Query found nothing | Return empty/false (this is success) |
| Operation failed | Throw |
public interface PublicIpApi {
boolean isExists(InetAddress address) throws XmlRpcException;
}truefalseXmlRpcExceptionpublic interface ServiceControlApi {
int start(String name) throws XmlRpcException;
}00XmlRpcException-1| 结果 | 传递方式 |
|---|---|
| 操作成功 | 返回值 |
| 查询无结果 | 返回空值/false(属于成功情况) |
| 操作失败 | 抛出 |
public interface PublicIpApi {
boolean isExists(InetAddress address) throws XmlRpcException;
}truefalseXmlRpcExceptionpublic interface ServiceControlApi {
int start(String name) throws XmlRpcException;
}00XmlRpcException-1// GOOD: JAXB DTO
@XmlRootElement
public class ComplexJaxbDto implements Element {
@XmlAttribute
private String type;
@XmlAttribute
private String number;
private List<NestedDto> nestedDtos;
private ComplexJaxbDto() { }
public static final class NestedDto {
private String value;
private NestedDto() { }
}
}// BAD: Serializable DTO (Java-only)
public class SerializableDto implements Serializable {
private String value;
public SerializableDto(String value) {
this.value = value;
}
}// 推荐:JAXB DTO
@XmlRootElement
public class ComplexJaxbDto implements Element {
@XmlAttribute
private String type;
@XmlAttribute
private String number;
private List<NestedDto> nestedDtos;
private ComplexJaxbDto() { }
public static final class NestedDto {
private String value;
private NestedDto() { }
}
}// 不推荐:Serializable DTO(仅支持Java)
public class SerializableDto implements Serializable {
private String value;
public SerializableDto(String value) {
this.value = value;
}
}<ex:serializable>rO0ABXNyACx0aWwueG1scnBj...</ex:serializable><ex:jaxb>
<complexJaxbDto number="number" type="type">
<nestedDtos>
<value>test</value>
</nestedDtos>
<nestedDtos>
<value>test2</value>
</nestedDtos>
</complexJaxbDto>
</ex:jaxb><ex:serializable>rO0ABXNyACx0aWwueG1scnBj...</ex:serializable><ex:jaxb>
<complexJaxbDto number="number" type="type">
<nestedDtos>
<value>test</value>
</nestedDtos>
<nestedDtos>
<value>test2</value>
</nestedDtos>
</complexJaxbDto>
</ex:jaxb>| Mistake | Problem | Fix |
|---|---|---|
Missing | Client can't receive errors | Add to all API methods |
Using | XML-RPC library doesn't support it | Use |
Returning | Meaningless, creates confusion | Always return |
| Error codes in DTO fields | Request appears successful | Throw |
Using | Java-only, unreadable | Use JAXB annotations |
Using | Java-only | Avoid, use standard faults |
| 错误 | 问题 | 修复方案 |
|---|---|---|
未声明 | 客户端无法接收错误信息 | 为所有API方法添加该声明 |
使用 | XML-RPC库不支持 | 使用 |
返回 | 无实际意义,造成混淆 | 始终返回 |
| DTO字段中包含错误码 | 请求看起来执行成功 | 抛出 |
使用 | 仅支持Java,输出不可读 | 使用JAXB注解 |
使用 | 仅支持Java | 避免使用,使用标准错误机制 |
start()0start()XmlRpcExceptionstart()0start()XmlRpcExceptionthrows XmlRpcExceptionvoidint00throws XmlRpcExceptionvoidint00