相关疑难解决方法(0)

HttpURLConnection无效的HTTP方法:PATCH

当我尝试使用非标准的HTTP方法,如PATCH和URLConnection:

    HttpURLConnection conn = (HttpURLConnection) new URL("http://example.com").openConnection();
    conn.setRequestMethod("PATCH");
Run Code Online (Sandbox Code Playgroud)

我得到一个例外:

java.net.ProtocolException: Invalid HTTP method: PATCH
at java.net.HttpURLConnection.setRequestMethod(HttpURLConnection.java:440)
Run Code Online (Sandbox Code Playgroud)

使用像Jersey这样的更高级别的API会产生相同的错误.是否有解决方法来发出PATCH HTTP请求?

java httpurlconnection

64
推荐指数
6
解决办法
5万
查看次数

如何配置Spring和SLF4J以便我可以获取日志记录?

我有一个我想要登录的maven&spring应用程序.我很想使用SLF4J.

我想将所有配置文件放入包含log4j.xml的目录{classpath}/config中,然后使用spring bean将init放入.

例如

<bean id="log4jInitialization" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetClass" value="org.springframework.util.Log4jConfigurer"/>
    <property name="targetMethod" value="initLogging"/>
    <property name="arguments">
        <list>
            <value>classpath:config/log4j.xml</value>
        </list>
    </property>
</bean>
Run Code Online (Sandbox Code Playgroud)

但是我收到此警告并且没有记录.

log4j的:警告没有附加目的地可以发现记录器(org.springframework.context.support.ClassPathXmlApplicationContext).log4j:WARN请正确初始化log4j系统.log4j的:WARN见http://logging.apache.org/log4j/1.2/faq.html#noconfig获取更多信息.

我已经google了,找不到一个简单的例子来设置它.有任何想法吗?

java logging spring log4j slf4j

44
推荐指数
4
解决办法
8万
查看次数

如何在Spring RestTemplate中记录响应?

我正在使用RestTemplate来调用Web服务.

String userId = restTemplate.getForObject(createUserUrl, String.class);
Run Code Online (Sandbox Code Playgroud)

如果这不能返回用户ID我只是返回null但我不知道为什么.如何将实际的XML响应输出到日志?

java rest logging spring

14
推荐指数
2
解决办法
3万
查看次数

是否可以使用Guava链接异步调用?

我想链接异步休息服务调用,并在完成后进行单次回调.

是否可以用番石榴做到这一点?

java guava

14
推荐指数
2
解决办法
9186
查看次数

如何使用spring记录RestTemplate请求和响应?

我正在使用springRestTemplate来执行GET查询.

如何在每个请求上自动将任何请求和响应数据记录到日志文件中?

java logging spring spring-rest

10
推荐指数
1
解决办法
2万
查看次数

RestTemplate记录POST数据

我的resttemplate.exchange()在POST请求上失败,服务器返回500错误.

我尝试将根日志记录级别设置为DEBUG,但在返回500错误之前没有记录任何内容.为了确保我的日志配置是正确的,我在resttemplate调用之前添加了一行

HttpClient client = new DefaultHttpClient();
client.execute(new HttpGet("http://google.com"));
Run Code Online (Sandbox Code Playgroud)

在这种情况下,确实出现了很多日志消息.

那么如何让RestTemplate导出调试数据呢?

谢谢杨

spring log4j resttemplate

7
推荐指数
1
解决办法
8037
查看次数

调试RestTemplate发布请求

我想POST以一个REST-API使用RestTemplateSpring.这是我正在使用的代码:

//Code to Post data using Rest Template
List<UserVO> userList = getUsers();
RestRequestVO submitRequestData = new RestRequestVO();
submitRequestData.setAction("update");
submitRequestData.setType("user");
submitRequestData.setItems(items);
ResponseEntity<String> resposne = restTemplate.postForEntity(putUserUrl, submitRequestData, String.class);
String message = resposne.getBody();

//The structure of class
public class RestRequestVO {
private String action;
private String type;
private List<UserVO> items;

//Setters and Getters
}

//Expected JSON
{
"action"="update",
"type"="user",
"items"=[
    { //user1 }, {//user2} ....
]
}
Run Code Online (Sandbox Code Playgroud)

我需要正确调试它,看看按restTemplate.postForEntity(putUserUrl, submitRequestData, String.class);行发送到REST服务器的确切JSON是什么.

我在用Eclipse.我已经尝试逐行调试代码.我也尝试过设置日志级别 …

java rest spring json resttemplate

6
推荐指数
1
解决办法
2万
查看次数

如何在Spring Boot中使用Logback调试RestTemplate?

我一直在尝试找出我的RestTemplate设置中缺少的内容(尤其是OAuth2RestTemplate),因此我想查看调试日志。我找到了一种可能的解决方案,但没有RestTemplate显示获取操作。

到目前为止,我所做的是通过遵循此处显示的配置来替换Spring Boot应用程序中的默认Logback日志记录机制。当我在更改日志记录依赖关系后运行Spring Boot应用程序时,在控制台上看到的只是Spring徽标(ASCII),没有别的。我已经定义了一个log4j-spring.propertiessrc/main/resources与前面的链接中定义的属性。

我在这里还想念什么?我需要在调试模式下运行Spring Boot应用程序吗?如果是这样,我该怎么做?另外,如何使用默认的Logback机制进行设置?

java spring spring-mvc logback spring-boot

6
推荐指数
0
解决办法
3286
查看次数

当响应代码为401时,ClientHttpResponse.getBody()抛出ResourceAccessException

我正在尝试为每个请求记录请求 - 响应对.问题是,当响应代码是401,ClientHttpResponse.getBody()抛出ResourceAccessException,我无法读取响应正文.

这是RestTemplate配置

RestTemplate restTemplate = new RestTemplate();

// Added this requestFactory to make response object readable more than once.
ClientHttpRequestFactory requestFactory =
        new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory());
restTemplate.setRequestFactory(requestFactory);
restTemplate.getInterceptors().add(new RequestLoggingInterceptor(vCloudRequest.getAction(),httpHeaders));
    restTemplate.setErrorHandler(new RequestErrorHandler());

return restTemplate;
Run Code Online (Sandbox Code Playgroud)

下面的拦截器的最后一行抛出以下异常.

我该如何解决这个问题?

org.springframework.web.client.ResourceAccessException:对" https://example.com/api/sessions "的POST请求发出I/O错误:服务器返回HTTP响应代码:401为URL:https://example.com/ api/sessions ; 嵌套异常是java.io.IOException:服务器返回HTTP响应代码:401为URL:https://example.com.11/api/sessions

这是拦截器的相关部分.

@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
    ClientHttpResponse response = execution.execute(request, body);

    String requestString = new String(body);

    // Calling this method because when we …
Run Code Online (Sandbox Code Playgroud)

java spring resttemplate

6
推荐指数
1
解决办法
4404
查看次数

为什么我的自定义ClientHttpRequestInterceptor响应为空

我已经为自定义日志记录拦截器执行了以下操作

public class HttpLoggingInterceptor implements ClientHttpRequestInterceptor {
    private final static Logger log = LoggerFactory.getLogger(HttpLoggingInterceptor.class);

    @Override
    public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
        logRequest(request, body);
        ClientHttpResponse response = execution.execute(request, body);
        logResponse(response);
        return response;
    }

    private void logRequest(HttpRequest request, byte[] body) throws IOException {
        log.info("Request URI : {}, Method : {}, Headers : {}, Request body : {}", request.getURI(), request.getMethod(), request.getHeaders(), new String(body, "UTF-8"));

    }

    private void logResponse(ClientHttpResponse response) throws IOException {
        log.info("Response Status code : {}, Status …
Run Code Online (Sandbox Code Playgroud)

spring interceptor resttemplate

5
推荐指数
1
解决办法
1478
查看次数

Eclipse - 声纳 S2629 可能误报新字符串

我正在使用最新的 Eclipse 和 Sonar 插件

回答日志记录时,有以下行:

log.debug("Request body: {}", new String(body, "UTF-8"));
Run Code Online (Sandbox Code Playgroud)

只有在调试级别时才应该创建字符串:

/**
 * Log a message at the DEBUG level according to the specified format
 * and argument.
 * <p/>
 * <p>This form avoids superfluous object creation when the logger
 * is disabled for the DEBUG level. </p>
 *
 * @param format the format string
 * @param arg    the argument
 */
public void debug(String format, Object arg);
Run Code Online (Sandbox Code Playgroud)

但声纳将其标记为squid:S2629

“前提条件”和日志参数不应该需要求值 (squid:S2629)

并给出串联的例子

logger.log(Level.DEBUG, "出现问题:" + …

java eclipse sonarqube sonarlint-eclipse

4
推荐指数
1
解决办法
2509
查看次数

如何获取 HandlerMethod 与过滤器中的 HttpServletRequest 匹配

我正在编写一个简单的代理应用程序,并且希望映射的 url 将由我的控制器处理,但其他 url(包括错误)可以转发到另一个不同的地址。因此,如果找不到资源,我会使用Filter而不是HandlerInterceptorAdapter无法调用,因为某些“资源路径处理程序”会处理它。

期待

http://localhost:8090/upload.html> Filter> > http://localhost:8092/upload.html
http://localhost:8090/files/upload>Controllerhttp://localhost:8092/files/upload

不是

http://localhost:8090/upload.html> Filter> > http://localhost:8092/upload.html
http://localhost:8090/files/upload>Controllerhttp://localhost:8092/files/upload

或者

http://localhost:8090/upload.html>>Interceptorhttp://localhost:8090/error找到 http://localhost:8090/files/upload>>Filterhttp://localhost:8092/files/upload

演示

Filter我在我的子类中设置了一个WebMvcConfigurerAdapter.

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {

    @Bean
    private javax.servlet.Filter proxyFilter() {
        return new OncePerRequestFilter() {
            @Override
            protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
                System.out.println("[doFilterInternal]isCommitted=" + response.isCommitted() …
Run Code Online (Sandbox Code Playgroud)

spring-boot

2
推荐指数
1
解决办法
3856
查看次数