使用Spring Web Service发送图像时遇到问题.
我写了如下控制器
@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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
@ResponseBody 将响应转换为JSON.
我正在使用RestClient来测试Web服务.
但是当我点击http://localhost:8080/my-war-name/rest/imageURL时.
Header
Accept=image/jpg
Run Code Online (Sandbox Code Playgroud)
我在RestClient上面临以下错误
使用windows-1252编码将响应正文转换为字符串失败.响应机构未设置!
当我使用浏览器Chrome和Firefox时
标题没有添加,所以错误是预期的(请指导我这个)
HTTP Status 405 - Request method 'GET' not supported …
我是Spring和Portlet的新手.我想用jqgrid来显示一些列表.我试图在控制器中调用一个方法,该方法使用@RequestMapping进行注释,但该方法未被调用
我的控制器有以下方法
@Controller(value = "myController")
public class MyController {
@RequestMapping(value="/myURL",method=RequestMethod.GET)
public @ResponseBody MyDTO initItemSearchGrid(RenderResponse response, RenderRequest request){
MyDTO myDto=new MyDTO();
return myDto;
}
}
Run Code Online (Sandbox Code Playgroud)
我的JSP代码使用AJAX
var urlink="/myURL"; /* myURL is the exact String written in value Attribute of
resourceMapping in Controller*/
$.ajax({
url :urlink,
cache: false,
data:$('#myForm').formSerialize(),
dataType: "json",
type: "GET",
contentType: "application/json; charset=utf-8",
success: function(jsondata){
...
}
});
Run Code Online (Sandbox Code Playgroud)
当上面的AJAX代码正在执行时,我的方法不会被调用.