当前位置: 首页 > 知识库问答 >
问题:

Spring托架模板401错误响应

司徒焕
2023-03-14

因此,在我的客户端代码中,我有:

HttpHeaders headers = new HttpHeaders();
headers.set(HttpHeaders.AUTHORIZATION, "myToken");
HttpEntity entity = new HttpEntity(null, headers);

restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
restTemplate.getMessageConverters().add(new StringHttpMessageConverter());

response = restTemplate.exchange("http://localhost:8080/documents", HttpMethod.GET, entity, Document[].class);

一切正常。之后我要测试错误。因此,我删除了授权头。

当我用像postman这样的工具进行测试时,我会收到401响应。但是对于我的rest模板,我只收到一个IllegalArgumentException。

我也测试了ResponseErrorHandler。

public class MyErrorHandler implements ResponseErrorHandler {

    @Override
    public boolean hasError(ClientHttpResponse clientHttpResponse) throws IOException {
        return false; //i've also tried return true
    }

    @Override
    public void handleError(ClientHttpResponse clientHttpResponse) throws IOException {
        String theString = IOUtils.toString(clientHttpResponse.getBody());
        FunctionalTestException exception = new FunctionalTestException();
        Map<String, Object> properties = new HashMap<String, Object>();
        properties.put("code", clientHttpResponse.getStatusCode().toString());
        properties.put("body", theString);
        properties.put("header", clientHttpResponse.getHeaders());
        exception.setProperties(properties);
        throw exception;
    }
}
restTemplate.setErrorHandler(new MyErrorHandler());

所以我的问题是如何使用rest模板找到我的401错误响应。

这里有个例外:

java.lang.IllegalArgumentException: invalid start or end

和堆栈跟踪:

sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1455)
sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441)
sun.net.www.protocol.http.HttpURLConnection.getHeaderField(HttpURLConnection.java:2979)
java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:489)
org.springframework.http.client.SimpleBufferingClientHttpRequest.executeInternal(SimpleBufferingClientHttpRequest.java:84)
org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48)
org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53)
org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:619)
org.springframework.web.client.RestTemplate.execute(RestTemplate.java:580)
org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:498)
org.boite.dq.steps.UnauthorizedUser.callListCategories(UnauthorizedUser.java:61)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
org.jbehave.core.steps.StepCreator$ParametrisedStep.perform(StepCreator.java:733)
org.jbehave.core.embedder.PerformableTree$FineSoFar.run(PerformableTree.java:346)
org.jbehave.core.embedder.PerformableTree$PerformableSteps.perform(PerformableTree.java:1088)
org.jbehave.core.embedder.PerformableTree$AbstractPerformableScenario.performRestartableSteps(PerformableTree.java:953)
org.jbehave.core.embedder.PerformableTree$NormalPerformableScenario.perform(PerformableTree.java:992)
org.jbehave.core.embedder.PerformableTree$PerformableScenario.perform(PerformableTree.java:902)
org.jbehave.core.embedder.PerformableTree$PerformableStory.performScenarios(PerformableTree.java:825)
org.jbehave.core.embedder.PerformableTree$PerformableStory.perform(PerformableTree.java:798)
org.jbehave.core.embedder.PerformableTree.performCancellable(PerformableTree.java:422)
org.jbehave.core.embedder.PerformableTree.perform(PerformableTree.java:393)
org.jbehave.core.embedder.StoryManager$EnqueuedStory.call(StoryManager.java:292)
org.jbehave.core.embedder.StoryManager$EnqueuedStory.call(StoryManager.java:266)
java.util.concurrent.FutureTask.run(FutureTask.java:266)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
java.lang.Thread.run(Thread.java:745)

共有1个答案

阎星河
2023-03-14

崩溃发生在HTTPURLConnection::GetHeAderField中,因此我怀疑您的一个响应头格式不正确(不是HTTPURLConnection所期望的)。通常,401响应附带一个www-authenticate响应头,将代理指向服务支持的身份验证方法。我怀疑是这个头导致了崩溃。

Jersey的问题跟踪器中的一个bug报告显示,httpurlconnectionwww-authentication格式设置了一些约束。在此特定情况下,导致类似崩溃的值是oauth_problem=token_rejected。建议的解决办法是:

 类似资料:
  • 我正在SpringBoot应用程序中使用SpringREST模板。 即使我正在传递凭据,我也总是收到未经授权的错误。 我可以通过ChromeREST Web Service Client访问此服务。 在SpringBoot中是否有访问REST模板的简化方法。 下面是到目前为止完成的导致401错误的代码片段 RestClientConfig类 错误:

  • 当我点击登录按钮并通过数据库成功登录后,它被重定向到hello.ftl页面。但是ftl页面显示此错误 FreeMarker模板错误(调试模式;在生产中使用RETHROW!):以下内容的计算结果为null或missing:==>var[在模板“hello.ftl”第8行,第32列]----提示:如果已知失败的表达式在法律上引用了有时为null或missing的内容,可以指定默认值,如myoption

  • 相当新的Spring开发者.. 过去几天我一直在使用Spring,并设法使用JPA和Spring Rest创建了一个简单的CRUD API。现在,我希望能够灵活地改变返回的JSON的组成方式。 例如,我有以下简单实体: GET请求返回以下JSON: 现在我想删除部分并添加其他内容。 这在Spring可能吗? 课程: FaqsCategory(实体) FaqsCategoryRepository

  • 我在ARM模板中定义了以下模式: 部署ARM模板时,架构未正确导入,我在Azure门户的OpenAPI规范视图中看到以下错误: x-ms-export-notes:-

  • 我有下面的代码,我希望TestEnableIf与不同的专门化有不同的打印功能,但它没有按计划工作,错误如下。 我不明白的是,sfinae应该意味着专门化失败不是错误,那么编译器为什么要抱怨失败?

  • 如果我调用tomcat服务器的默认url,我将看到一个错误404。程序运行时没有任何问题,但restcontroller可能已禁用(我不知道,通常不知道)。日志中没有错误。起始页是html格式。我设置了默认路径(见下文)。 我正在使用Spring Boot和tomcat7。 请求的资源(/dashboard/)不可用。 Spring Boot路径src\main\resources\templat