java.lang.IllegalArgumentException:使用RestTemplate时[0]没有匹配常量

时间:2013-07-18 23:15:00

标签: java illegalargumentexception resttemplate

我正在运行一个程序,它会针对url生成一些负载,并使用RestTemplate进一步使用相同的URL。

我正在点击的网址会返回一个简单的字符串 Hello World 作为回复。

以下是将针对网址生成负载的程序。

public class ShellbareboneLnP {

    public static void main(String[] args) {

        try {

            // create thread pool with given size
            ExecutorService service = Executors.newFixedThreadPool(threads);

            // queue some tasks
            long startTime = System.currentTimeMillis();
            long endTime = startTime + (durationOfRun * 60 * 1000);

            for (int i = 0; i < threads; i++) {
                service.submit(new ShellBareboneTask(endTime));
            }

            service.shutdown();
            service.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
        } catch (InterruptedException e) {
            LOG.warn("Threw a Interrupted Exception in" + CNAME + ".ShellbareboneLnP: boss told me to stop...Not my fault!!");
        } catch (Exception e) {
            LOG.warn("Threw a Exception in" + CNAME + e);
        } finally {
        }
    }
}


class ShellBareboneTask implements Runnable {

    private static final Logger LOG = Logger.getLogger(ShellBareboneTask.class.getName());
    public static ConcurrentHashMap<String, AtomicInteger> exceptionMap = new ConcurrentHashMap<String, AtomicInteger>();
    private final long endTime;


    /**
     * Constructor to pass the values
     * 
     * @param durationOfRun
     */
    public ShellBareboneTask(long endTime) {
        this.endTime = endTime;
    }


    @Override
    public void run() {

        try {

            RestTemplate restTemplate = new RestTemplate();

            while (System.currentTimeMillis() <= endTime) {

                PerformanceTimer timer = PerformanceTimer.getInstance();
                restTemplate.getForObject("some_url", String.class);
                timer.getDuration();

            }
        } catch (Exception e) {
            handleException(e.getCause() != null ? e.getCause().toString() : e.toString());
            e.printStackTrace();
        }

    }
}

每次,程序运行一段时间后,我都会收到以下异常。我不确定导致这些例外的原因。谁能在这帮助我?

java.lang.IllegalArgumentException: No matching constant for [0]
    at org.springframework.http.HttpStatus.valueOf(HttpStatus.java:426)
    at org.springframework.http.client.AbstractClientHttpResponse.getStatusCode(AbstractClientHttpResponse.java:32)
    at org.springframework.web.client.DefaultResponseErrorHandler.hasError(DefaultResponseErrorHandler.java:46)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:439)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:401)
    at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:199)
    at com.hostdomain.market.shell.barebone.lnp.ShellBareboneTask.run(ShellbareboneLnP.java:271)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:452)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:314)
    at java.util.concurrent.FutureTask.run(FutureTask.java:149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:897)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:919)
    at java.lang.Thread.run(Thread.java:736)
16:04:20,889 ERROR ShellBareboneTask:300 - java.lang.IllegalArgumentException: No matching constant for [0]
java.lang.IllegalArgumentException: No matching constant for [0]
    at org.springframework.http.HttpStatus.valueOf(HttpStatus.java:426)
    at org.springframework.http.client.AbstractClientHttpResponse.getStatusCode(AbstractClientHttpResponse.java:32)
    at org.springframework.web.client.DefaultResponseErrorHandler.hasError(DefaultResponseErrorHandler.java:46)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:439)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:401)
    at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:199)
    at com.hostdomain.market.shell.barebone.lnp.ShellBareboneTask.run(ShellbareboneLnP.java:271)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:452)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:314)
    at java.util.concurrent.FutureTask.run(FutureTask.java:149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:897)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:919)
    at java.lang.Thread.run(Thread.java:736)
16:04:22,248 ERROR ShellBareboneTask:300 - java.net.SocketException: Unexpected end of file from server
org.springframework.web.client.ResourceAccessException: I/O error: Unexpected end of file from server; nested exception is java.net.SocketException: Unexpected end of file from server
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:453)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:401)
    at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:199)
    at com.hostdomain.market.shell.barebone.lnp.ShellBareboneTask.run(ShellbareboneLnP.java:271)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:452)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:314)
    at java.util.concurrent.FutureTask.run(FutureTask.java:149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:897)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:919)
    at java.lang.Thread.run(Thread.java:736)
Caused by: java.net.SocketException: Unexpected end of file from server
    at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:782)
    at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:645)
    at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:779)
    at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:645)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1205)
    at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:390)
    at org.springframework.http.client.SimpleClientHttpResponse.getRawStatusCode(SimpleClientHttpResponse.java:47)
    at org.springframework.http.client.AbstractClientHttpResponse.getStatusCode(AbstractClientHttpResponse.java:32)
    at org.springframework.web.client.DefaultResponseErrorHandler.hasError(DefaultResponseErrorHandler.java:46)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:439)
    ... 9 more
16:04:24,955 ERROR ShellBareboneTask:300 - java.net.SocketException: Connection reset
16:04:24,955 ERROR ShellBareboneTask:300 - java.net.SocketException: Connection reset
16:04:24,955 ERROR ShellBareboneTask:300 - java.net.SocketException: Unexpected end of file from server
16:04:24,955 ERROR ShellBareboneTask:300 - java.net.SocketException: Connection reset
org.springframework.web.client.ResourceAccessException: I/O error: Connection reset; nested exception is java.net.SocketException: Connection reset
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:453)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:401)
    at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:199)
    at com.hostdomain.market.shell.barebone.lnp.ShellBareboneTask.run(ShellbareboneLnP.java:271)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:452)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:314)
    at java.util.concurrent.FutureTask.run(FutureTask.java:149)
org.springframework.web.client.ResourceAccessException: I/O error: Connection reset; nested exception is java.net.SocketException: Connection reset at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:897)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:919)
    at java.lang.Thread.run(Thread.java:736)
Caused by: java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(SocketInputStream.java:179)
    at java.io.BufferedInputStream.fill(BufferedInputStream.java:229)
    at java.io.BufferedInputStream.read1(BufferedInputStream.java:269)
    at java.io.BufferedInputStream.read(BufferedInputStream.java:328)
    at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:700)
    at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:645)
    at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:779)
    at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:645)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1205)
    at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:390)
    at org.springframework.http.client.SimpleClientHttpResponse.getRawStatusCode(SimpleClientHttpResponse.java:47)
    at org.springframework.http.client.AbstractClientHttpResponse.getStatusCode(AbstractClientHttpResponse.java:32)
org.springframework.web.client.ResourceAccessException: I/O error: Connection reset; nested exception is java.net.SocketException: Connection reset
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:453)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:401)
    at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:199)
    at com.hostdomain.market.shell.barebone.lnp.ShellBareboneTask.run(ShellbareboneLnP.java:271)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:452)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:314)
    at java.util.concurrent.FutureTask.run(FutureTask.java:149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:897)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:919)
    at java.lang.Thread.run(Thread.java:736)
Caused by: java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(SocketInputStream.java:179)
    at java.io.BufferedInputStream.fill(BufferedInputStream.java:229)
    at java.io.BufferedInputStream.read1(BufferedInputStream.java:269)
    at java.io.BufferedInputStream.read(BufferedInputStream.java:328)
    at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:700)
    at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:645)
    at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:779)
    at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:645)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1205)
    at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:390)
    at org.springframework.http.client.SimpleClientHttpResponse.getRawStatusCode(SimpleClientHttpResponse.java:47)
    at org.springframework.http.client.AbstractClientHttpResponse.getStatusCode(AbstractClientHttpResponse.java:32)
    at org.springframework.web.client.DefaultResponseErrorHandler.hasError(DefaultResponseErrorHandler.java:46)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:439)
    ... 9 more

    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:453)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:401)
    at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:199)
    at com.hostdomain.market.shell.barebone.lnp.ShellBareboneTask.run(ShellbareboneLnP.java:271)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:452)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:314)
    at java.util.concurrent.FutureTask.run(FutureTask.java:149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:897)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:919)
    at java.lang.Thread.run(Thread.java:736)
Caused by: java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(SocketInputStream.java:179)
    at java.io.BufferedInputStream.fill(BufferedInputStream.java:229)
    at java.io.BufferedInputStream.read1(BufferedInputStream.java:269)
    at java.io.BufferedInputStream.read(BufferedInputStream.java:328)
    at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:700)
    at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:645)
    at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:779)
    at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:645)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1205)
    at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:390)
    at org.springframework.http.client.SimpleClientHttpResponse.getRawStatusCode(SimpleClientHttpResponse.java:47)
    at org.springframework.http.client.AbstractClientHttpResponse.getStatusCode(AbstractClientHttpResponse.java:32)
    at org.springframework.web.client.DefaultResponseErrorHandler.hasError(DefaultResponseErrorHandler.java:46)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:439)
    ... 9 more
org.springframework.web.client.ResourceAccessException: I/O error: Unexpected end of file from server; nested exception is java.net.SocketException: Unexpected end of file from server
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:453)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:401)
    at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:199)
    at com.hostdomain.market.shell.barebone.lnp.ShellBareboneTask.run(ShellbareboneLnP.java:271)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:452)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:314)
    at java.util.concurrent.FutureTask.run(FutureTask.java:149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:897)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:919)
    at java.lang.Thread.run(Thread.java:736)
Caused by: java.net.SocketException: Unexpected end of file from server
    at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:782)
    at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:645)
    at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:779)
    at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:645)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1205)
    at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:390)
    at org.springframework.http.client.SimpleClientHttpResponse.getRawStatusCode(SimpleClientHttpResponse.java:47)
    at org.springframework.http.client.AbstractClientHttpResponse.getStatusCode(AbstractClientHttpResponse.java:32)
    at org.springframework.web.client.DefaultResponseErrorHandler.hasError(DefaultResponseErrorHandler.java:46)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:439)
    ... 9 more
    at org.springframework.web.client.DefaultResponseErrorHandler.hasError(DefaultResponseErrorHandler.java:46)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:439)
    ... 9 more
16:04:27,617 ERROR ShellBareboneTask:300 - java.net.SocketException: Unexpected end of file from server
16:04:27,618 ERROR ShellBareboneTask:300 - java.net.SocketException: Unexpected end of file from server
org.springframework.web.client.ResourceAccessException: I/O error: Unexpected end of file from server; nested exception is java.net.SocketException: Unexpected end of file from server
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:453)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:401)
    at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:199)
    at com.hostdomain.market.shell.barebone.lnp.ShellBareboneTask.run(ShellbareboneLnP.java:271)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:452)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:314)
    at java.util.concurrent.FutureTask.run(FutureTask.java:149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:897)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:919)
    at java.lang.Thread.run(Thread.java:736)
Caused by: java.net.SocketException: Unexpected end of file from server
    at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:782)
    at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:645)
    at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:779)
    at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:645)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1205)
    at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:390)
    at org.springframework.http.client.SimpleClientHttpResponse.getRawStatusCode(SimpleClientHttpResponse.java:47)
    at org.springframework.http.client.AbstractClientHttpResponse.getStatusCode(AbstractClientHttpResponse.java:32)
    at org.springframework.web.client.DefaultResponseErrorHandler.hasError(DefaultResponseErrorHandler.java:46)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:439)
    ... 9 more
org.springframework.web.client.ResourceAccessException: I/O error: Unexpected end of file from server; nested exception is java.net.SocketException: Unexpected end of file from server
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:453)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:401)
    at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:199)
    at com.hostdomain.market.shell.barebone.lnp.ShellBareboneTask.run(ShellbareboneLnP.java:271)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:452)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:314)
    at java.util.concurrent.FutureTask.run(FutureTask.java:149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:897)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:919)
    at java.lang.Thread.run(Thread.java:736)
Caused by: java.net.SocketException: Unexpected end of file from server
    at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:782)
    at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:645)
    at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:779)
    at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:645)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1205)
    at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:390)
    at org.springframework.http.client.SimpleClientHttpResponse.getRawStatusCode(SimpleClientHttpResponse.java:47)
    at org.springframework.http.client.AbstractClientHttpResponse.getStatusCode(AbstractClientHttpResponse.java:32)
    at org.springframework.web.client.DefaultResponseErrorHandler.hasError(DefaultResponseErrorHandler.java:46)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:439)
    ... 9 more
16:04:39,469 ERROR ShellBareboneTask:300 - java.net.SocketException: Unexpected end of file from server
org.springframework.web.client.ResourceAccessException: I/O error: Unexpected end of file from server; nested exception is java.net.SocketException: Unexpected end of file from server
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:453)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:401)
    at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:199)
    at com.hostdomain.market.shell.barebone.lnp.ShellBareboneTask.run(ShellbareboneLnP.java:271)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:452)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:314)
    at java.util.concurrent.FutureTask.run(FutureTask.java:149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:897)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:919)
    at java.lang.Thread.run(Thread.java:736)
Caused by: java.net.SocketException: Unexpected end of file from server
    at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:782)
    at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:645)
    at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:779)
    at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:645)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1205)
    at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:390)
    at org.springframework.http.client.SimpleClientHttpResponse.getRawStatusCode(SimpleClientHttpResponse.java:47)
    at org.springframework.http.client.AbstractClientHttpResponse.getStatusCode(AbstractClientHttpResponse.java:32)
    at org.springframework.web.client.DefaultResponseErrorHandler.hasError(DefaultResponseErrorHandler.java:46)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:439)
    ... 9 more

3 个答案:

答案 0 :(得分:3)

  

java.lang.IllegalArgumentException:[0]没有匹配的常量       在org.springframework.http.HttpStatus.valueOf(HttpStatus.java:426)

异常消息表明您的服务器端代码生成了状态代码为0的响应,该响应无效。

你可以发布你的服务器端代码吗?

答案 1 :(得分:1)

我在使用Spring版本3时遇到了同样的错误,升级到4.2.2后它得到了解决。

答案 2 :(得分:-1)

我最近遇到了类似的问题。

我通过将 ClientHttpRequest 更改为 HttpComponentsClient 找到了解决方案。

这就是我所做的:

restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());

希望有所帮助