我当前正在使用Java 7,Maven,Spring MVC和Eclipse上的Eclipselink JPA编写Web服务,以访问连接到内部网络的温度/湿度传感器的值。使用curl连接到传感器并检索值没有问题,但是当我尝试使用URLConnection通过java连接时,出现错误。
我的代码:
public SensorReading getReading() { SensorReading sr = new SensorReading(); try { String parameters = "a=" + new Date().getTime(); URL url = new URL(sensorProtocol + sensorAddress + "/readValues?" + parameters); URLConnection connection = (URLConnection) url.openConnection(); connection.setDoInput(true); connection.setDoOutput(true); connection.setUseCaches(false); BufferedReader reader = new BufferedReader(new InputStreamReader((InputStream) connection.getContent())); sr = parseSensorResponse(reader); return esr; } catch(IOException e) { e.printStackTrace(); return null; } }
错误:
java.net.UnknownServiceException: no content-type at java.net.URLConnection.getContentHandler(Unknown Source) at java.net.URLConnection.getContent(Unknown Source) at com.company.web.service.SensorReadingServiceImpl.getReading(SensorReadingServiceImpl.java:45) at com.company.web.service.SensorServiceTest.test(SensorServiceTest.java:20) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74) at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83) at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71) at org.junit.runners.ParentRunner.run(ParentRunner.java:309) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
不幸的是,我无法控制响应,因此也无法控制设置的标头。此请求返回的唯一标头是状态-200消息。我需要能够读取这些值,但是如果没有内容类型,我就很迷路。有没有更有效的方法来解决这个问题?还是我只是想办法解决?谢谢!
如果不需要依赖服务器返回的内容类型,则应使用getInputStream,该方法不需要设置内容类型。