I am facing problem while sending an Image using Spring Web Service.
I have written controller as below
@Controller
public class WebService {
@RequestMapping(value = "/image", headers = "Accept=image/jpeg, image/jpg, image/png, image/gif", method = RequestMethod.GET)
public @ResponseBody byte[] getImage() {
try {
InputStream inputStream = this.getClass().getResourceAsStream("myimage.jpg");
BufferedImage bufferedImage = ImageIO.read(inputStream);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ImageIO.write( bufferedImage , "jpg", byteArrayOutputStream);
return byteArrayOutputStream.toByteArray();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
@ResponseBody
converts response into JSON.
I am using RestClient to test Web Service.
But When I'm hitting with http://localhost:8080/my-war-name/rest/image
URL.
Header
Accept=image/jpg
I facing following error on RestClient
Response body conversion to string using windows-1252 encoding failed. Response body not set!
When i'm using browsers Chrome and Firefox
Headers are not added so error was expected (Please guide me on this)
HTTP Status 405 - Request method 'GET' not supported
type Status report
message Request method 'GET' not supported
description The specified HTTP method is not allowed for the requested resource (Request method 'GET' not supported).
I have also faced below error once
The resource identified by this request is only capable
of generating responses with characteristics not acceptable according to the request "accept" headers ()
I have followed
http://krams915.blogspot.com/2011/02/spring-3-rest-web-service-provider-and.html tutorial.
My requirment is to send image in byte format to Android Client.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…