DP_*_*DP_ 4 java rest web-services restlet
我有一个Restlet ServerResource,它应该使用参数处理GET请求user.如果user等于某个值,则应返回一些图像,否则发送错误响应(404或403),表示不允许发送者获取图像.
import org.restlet.data.MediaType;
import org.restlet.representation.ObjectRepresentation;
import org.restlet.representation.Representation;
import org.restlet.resource.Get;
import org.restlet.resource.ResourceException;
import org.restlet.resource.ServerResource;
public class GetMap extends ServerResource {
@Get
public Representation getImage() {
final String user = getQuery().getValues("user");
if (user.equals("me")) {
//Read map from file and return it
byte[] data = readImage();
final ObjectRepresentation<byte[]> or=new ObjectRepresentation<byte[]>(data, MediaType.IMAGE_PNG) {
@Override
public void write(OutputStream os) throws IOException {
super.write(os);
os.write(this.getObject());
}
};
return or;
}
return null; // Here I want to send an error response
}
[...]
}
Run Code Online (Sandbox Code Playgroud)
如何在getImage方法中发送标准化的错误响应(而不是return null)?
| 归档时间: |
|
| 查看次数: |
1245 次 |
| 最近记录: |