好的首先是代码(很简单):
@Controller
@RequestMapping("/")
public class HelloController {
private final static Logger logger = Logger.getLogger(HelloController.class);
@RequestMapping(method = RequestMethod.GET)
public String printWelcome(ModelMap model) {
logger.info("ELO ELO");
model.addAttribute("message", "Hello world!");
RestTemplate restTemplate = new RestTemplate();
String url = "http://192.168.0.200:8000/GPIO/11/function/in";
//String url = "http://192.168.0.200:8000/GPIO/11/function";
//restTemplate.getForObject(url, String.class);
String test = "";
restTemplate.postForObject(url, null, String.class);
logger.info(test);
return "hello";
}
下一个我不是疯子的例子是postman(chrome)的回复:
最后完整的错误日志:
输入例外报告
消息请求处理失败;嵌套异常是 java.lang.IllegalArgumentException:“None”不包含'/'
description服务器遇到阻止它的内部错误 完成此请求。
例外
org.springframework.web.util.NestedServletException:Request 处理失败;嵌套异常是 java.lang.IllegalArgumentException:“None”不包含'/' org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:927) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:811) javax.servlet.http.HttpServlet.service(HttpServlet.java:618) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:796) javax.servlet.http.HttpServlet.service(HttpServlet.java:725) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 根本原因
java.lang.IllegalArgumentException:“None”不包含'/' org.springframework.http.MediaType.parseMediaType(MediaType.java:697) org.springframework.http.HttpHeaders.getContentType(HttpHeaders.java:305) org.springframework.web.client.HttpMessageConverterExtractor.getContentType(HttpMessageConverterExtractor.java:113) org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:84) org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:492) org.springframework.web.client.RestTemplate.execute(RestTemplate.java:447) org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:295) pl.piquarium.mvc.HelloController.printWelcome(HelloController.java:35) sun.reflect.NativeMethodAccessorImpl.invoke0(原生方法) sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) java.lang.reflect.Method.invoke(Method.java:606) org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176) org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:439) org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:427) org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925) org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856) org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:915) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:811) javax.servlet.http.HttpServlet.service(HttpServlet.java:618) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:796) javax.servlet.http.HttpServlet.service(HttpServlet.java:725) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) note注意Apache中提供了根本原因的完整堆栈跟踪 Tomcat / 8。0。3日志。
请求标题:
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip,deflate,sdch
Accept-Language:pl-PL,pl;q=0.8,en-US;q=0.6,en;q=0.4
Cache-Control:max-age=0
Connection:keep-alive
Cookie:__utma=212787668.2094541430.1400264829.1400264829.1400268775.2; __utmz=212787668.1400264829.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)
Host:192.168.0.200:8000
User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.137 Safari/537.36
响应标题:
Cache-Control:no-cache
Content-Length:2
Content-Type:None
Date:Fri, 16 May 2014 22:37:16 GMT
Server:WebIOPi/0.7.0/Python3.2
答案 0 :(得分:0)
您是否尝试过如下设置标题,
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);//or any other required
HttpEntity request = new HttpEntity(null, headers);
RestTemplate restTemplate = new RestTemplate();
String url = "http://192.168.0.200:8000/GPIO/11/function/in";
String response = restTemplate.postForObject(url,request,String.class);
答案 1 :(得分:0)
问题是服务器返回了无效的内容类型None
,而不是类似text/plain
的内容,而Spring REST正在窒息。您需要为None
类型添加自定义消息转换器,而不是使用类型化查询并自行解析响应对象,或让Pi人员修复其损坏的Web服务器。