调用servlet方法时出错“java.io.IOException:服务器返回HTTP响应代码:406”

时间:2013-07-08 06:04:41

标签: java servlets spring-mvc

我正在研究客户端 - 服务器应用程序。我收到错误:“java.io.IOException:服务器返回HTTP响应代码:406”从servlet请求方法时,获取具有所选名称的对象。在我的服务器端,我在DataController中获得了以下代码:

 @Controller
    public class DataController {

         @RequestMapping(value = "/getHarnessType")
         @ResponseBody
         public HarnessType getHarnessType(
               @RequestParam("harnessTypeName") String harnessTypeName) {
             GeneratorService generatorService=new GeneratorServiceImpl();
             HarnessType harnessType=generatorService.getHarnessType(harnessTypeName);
             return harnessType;
        }
    }

我的客户端源代码的一部分,我调用方法getHarnessType:

String harnessTypeName=jTextField.getText();
url = new URL("http://localhost:8080/getHarnessType?harnessTypeName="+harnessTypeName);
urlConn = (HttpURLConnection) url.openConnection();
OutputStream baos = new ByteArrayOutputStream();                
IOUtils.copy(urlConn.getInputStream(), baos);
继续进行最后一行之后我已经提到了错误...有趣的是,当我将方法getHarnessType的返回类型更改为String时:

...
public String getHarnessType(
...
return harnessType.getName();

并且只将String值传递给客户端(不是整个对象),一切正常

完整堆栈跟踪:

java.io.IOException: Server returned HTTP response code: 406 for URL: http: //localhost:8080/getHarnessType?harnessTypeName=7
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1245)
at formular.Formular2.actionPerformed(Formular2.java:100)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6038)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3260)
at java.awt.Component.processEvent(Component.java:5803)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4410)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
at java.awt.Container.dispatchEventImpl(Container.java:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2429)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)

3 个答案:

答案 0 :(得分:1)

似乎无法正确解析返回的对象HarnessType。我不确定您是否正在使用正确的注释驱动程序配置。

您可以先修改代码,看它是否有效:

@Controller
public class DataController {

     @RequestMapping(value = "/getHarnessType", method = RequestMethod.GET)
     public @ResponseBody HarnessType getHarnessType(
           @RequestParam("harnessTypeName") String harnessTypeName) {
         GeneratorService generatorService=new GeneratorServiceImpl();
         HarnessType harnessType=generatorService.getHarnessType(harnessTypeName);
         return harnessType;
    }
}

答案 1 :(得分:0)

问题似乎与客户端有关,尝试添加接受标头,我猜你没有使用浏览器。如果Web服务器检测到它要返回的数据对客户端不可接受,则会返回包含406错误代码的标头。 如果问题仍未解决,请添加堆栈跟踪。

答案 2 :(得分:0)

尝试设置urlConn .setRequestProperty("Content-Type", "application/json"); 错误406表示客户端请求中存在错误。它无法接受回应。见here