简体   繁体   English

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

[英]Getting error “java.io.IOException: Server returned HTTP response code: 406”, when calling servlet method

I am working on client-server application. 我正在处理客户端服务器应用程序。 I am getting error: "java.io.IOException: Server returned HTTP response code: 406" when requesting method from servlet, to get object with selected name. 当从Servlet请求方法以获取具有选定名称的对象时,出现错误:“ java.io.IOException:服务器返回了HTTP响应代码:406”。 On my server side I got following code in my DataController: 在服务器端,我的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;
        }
    }

part of source code of my client where I am calling method getHarnessType: 我在其中调用方法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);

after proceeding last line I am getting already mentioned error... interesting is, that when i change return type of method getHarnessType to String like: 在执行最后一行之后,我已经提到了错误...有趣的是,当我将方法getHarnessType的返回类型更改为String时,例如:

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

and pass only String value to client (not whole object), everything works fine 并仅将String值传递给客户端(而不是整个对象),一切正常

full stack trace: 全栈跟踪:

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)

It seems like the returned object HarnessType cannot be parsed correctly. 似乎返回的对象HarnessType无法正确解析。 I am not sure if you are using the right configuration of annotation driver. 我不确定您是否使用了正确的注释驱动程序配置。

You can modify your code as following first to see if it works: 您可以按照以下步骤修改代码,以查看其是否有效:

@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;
    }
}

Problem seems to be with the client, try adding accept headers, I guess you are not using browser. 问题似乎与客户端有关,请尝试添加accept标头,我想您未使用浏览器。 If the Web server detects that the data it wants to return is not acceptable to the client, it returns a header containing the 406 error code. 如果Web服务器检测到其要返回的数据对于客户端不可接受,则它将返回包含406错误代码的标头。 If the problem is still not solved, please add the stack trace. 如果问题仍未解决,请添加堆栈跟踪。

try setting urlConn .setRequestProperty("Content-Type", "application/json"); 尝试设置urlConn .setRequestProperty("Content-Type", "application/json"); Error 406 means something wrong in your client side request. 错误406表示您的客户端请求有问题。 It is not capable of accepting the response. 它不能接受响应。 See here 这里

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.io.IOException:服务器返回 HTTP 响应代码:406 为 ZE6B3091A8D2C4D552A4 - java.io.IOException: Server returned HTTP response code: 406 for URL 获取错误java.io.IOException:服务器返回HTTP响应代码:401 for - getting error java.io.IOException: Server returned HTTP response code: 401 for java.io.IOException:服务器返回HTTP响应代码:502 - java.io.IOException: Server returned HTTP response code: 502 java.io.IOException:服务器返回HTTP响应代码:500 - java.io.IOException: Server returned HTTP response code: 500 在Java代码中获取“ java.io.IOException:服务器返回的HTTP响应代码:URL的403:” - Getting “java.io.IOException: Server returned HTTP response code: 403 for URL: ” in java code 获取 java.io.IOException:服务器返回 HTTP 响应代码:URL 的 400:使用返回 400 状态代码的 url 时 - Getting java.io.IOException: Server returned HTTP response code: 400 for URL: when using a url which return 400 status code 错误打开连接 java.io.IOException:服务器返回 HTTP 响应代码:URL 501 - Error opening connection java.io.IOException: Server returned HTTP response code: 501 for URL 错误:java.io.IOException:服务器返回 HTTP 响应代码:URL 403 - Error : java.io.IOException: Server returned HTTP response code: 403 for URL java.io.IOException:服务器返回HTTP响应代码:503的URL:错误 - java.io.IOException: Server returned HTTP response code: 503 for URL: Error URLConnection错误 - java.io.IOException:服务器返回HTTP响应代码:URL为400 - URLConnection Error - java.io.IOException: Server returned HTTP response code: 400 for URL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM