Camel仅在浏览器中获得304响应,而不是邮递员

时间:2018-01-17 12:18:00

标签: java facebook-graph-api apache-camel

我有一个骆驼休息应用程序的问题。 该路由返回从PostMan调用时所期望的结果,但是从浏览器返回结果第一次,而第二次我得到304错误代码HttpOperationFailedException

我尝试删除/更改ETagExpiresLast-Modified等标题, Cache-ControlIf-Modified-Since,正如我在其他类似问题的答案中所读到的,但没有任何改变。

如何避免此错误?有人可以解释我为什么要打电话 http://localhost:8080/facebook/name仅来自浏览器,而不是来自PostMan? 如果我尝试直接在浏览器https://graph.facebook.com/v2.11/1234567?fields=name&access_token=ABCD1234MYTOKEN中调用,则没有问题。

以下是代码:

路由在TriggerRouteBuilderClass中定义:

package it.mycompany.facebook.service.camel.route;

import static javax.ws.rs.HttpMethod.GET;
import static org.apache.camel.Exchange.HTTP_METHOD;

import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.model.rest.RestBindingMode;
import org.springframework.stereotype.Component;

import it.skytv.facebook.client.types.FacebookApplication;

@Component
public class TriggerRouteBuilder extends RouteBuilder {

@Override
public void configure() {

    restConfiguration()
            .apiContextPath("/api-docs")
            .apiProperty("api.title", "Camel REST API")
            .apiProperty("api.version", "1.0")
            .apiProperty("cors", "true")
            .host("localhost")
            .port(8080)
            .apiContextRouteId("swagger-api-doc")
            .component("servlet")
            .bindingMode(RestBindingMode.json);

    rest("/facebook")
        .get("/name")
        .description("Gets the name of the Facebook application")
        .route().id("get-app-name-route")
        .removeHeaders("CamelHttp*")
        .setHeader(HTTP_METHOD, constant(GET))    
        .to("{{facebook.api.base.url}}/{{facebook.appID}}?fields=name&access_token={{facebook.permanentToken}})")
        .unmarshal("model-jackson-dataformat")
        .end()
        .endRest();

    }
}

配置为:

package it.mycompany.facebook.configuration;
import org.apache.camel.component.jackson.JacksonDataFormat;
import org.apache.camel.component.servlet.CamelHttpTransportServlet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScans;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScans({@ComponentScan("it.mycompany.facebook")})
public class FacebookConfig {

    private static Logger trace = LoggerFactory.getLogger(FacebookAsyncServerConfig.class);

    @Bean
    public ServletRegistrationBean servletRegistrationBean() {
        ServletRegistrationBean servlet = new ServletRegistrationBean(
            new CamelHttpTransportServlet(), "/*");
        servlet.setName("CamelServlet");
        return servlet;
    }

    @Bean(name="model-jackson-dataformat")
    public JacksonDataFormat jacksonDataFormat() {
        JacksonDataFormat jackson = new JacksonDataFormat();
        jackson.setEnableJaxbAnnotationModule(false);
        jackson.setPrettyPrint(true);
        return jackson;
    }
}

主要方法是:

package it.mycompany.facebook;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MyFacebookApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyFacebookApplication .class, args);
    }
}

路由中注入的属性在application.properties中定义,并且是:

facebook.api.version=v2.11
facebook.api.base.url=https://graph.facebook.com/${facebook.api.version}/
facebook.appID=1234567
facebook.permanentToken=ABCD1234MYTOKEN

更新:从浏览器获取的请求和响应的标头是:

案例确定:

General
Request URL:http://localhost:8080/facebook/name
Request Method:GET
Status Code:200 OK
Remote Address:[::1]:8080
Referrer Policy:no-referrer-when-downgrade  

Response Headers
accept:text/html,application/xhtml+xml,application/
xml;q=0.9,image/webp,image/apng,*/*;q=0.8
accept-encoding:gzip, deflate, br
accept-language:it-IT,it;q=0.9,en-US;q=0.8,en;q=0.7
Access-Control-Allow-Origin:*
Content-Type:application/json;charset=UTF-8
Date:Wed, 17 Jan 2018 15:49:13 GMT
ETag:"b2790db5428802fhf415v9735876g323403ua592"
Expires:Sat, 01 Jan 2000 00:00:00 GMT
facebook-api-version:v2.11
Server:Apache-Coyote/1.1
Strict-Transport-Security:max-age=15552000; preload
Transfer-Encoding:chunked
upgrade-insecure-requests:1
X-Application-Context:application:8080

Request Headers
Accept:text/html,application/xhtml+xml,
application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding:gzip, deflate, br
Accept-Language:it-IT,it;q=0.9,en-US;q=0.8,en;q=0.7
Cache-Control:max-age=0
Connection:keep-alive
Host:localhost:8080
Upgrade-Insecure-Requests:1
User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36

案例304:

General:
Request URL:http://localhost:8080/facebook/name
Request Method:GET
Status Code:500 Internal Server Error
Remote Address:[::1]:8080
Referrer Policy:no-referrer-when-downgrade

Response Headers
Connection:close
Content-Type:text/plain;charset=ISO-8859-1
Date:Wed, 17 Jan 2018 15:54:41 GMT
Server:Apache-Coyote/1.1
Transfer-Encoding:chunked
X-Application-Context:application:8080

Request Headers
Accept:text/html,application/xhtml+xml,
application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding:gzip, deflate, br
Accept-Language:it-IT,it;q=0.9,en-US;q=0.8,en;q=0.7
Cache-Control:max-age=0
Connection:keep-alive
Host:localhost:8080
If-None-Match:"b2790db5428802fhf415v9735876g323403ua592"
Upgrade-Insecure-Requests:1
User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36

非常感谢。

1 个答案:

答案 0 :(得分:0)

我解决了从我得到的反应中删除ETag。 感谢@CBroe的提示,我发现浏览器的第二个请求标题为“If-None-Match”,检查上一个响应中“ETag”的值。