谷歌AppEngine:基本的Spring MVC 4 REST + JSON在dev中工作,但不在prod中

时间:2015-01-24 23:58:22

标签: java json rest google-app-engine spring-mvc

我有一个非常基本的Spring MVC 4 REST + JSON演示,我试图在Google AppEngine上运行,而且我无法通过" 406 Not Acceptable"来自生产代码的响应。相同的代码在AppEngine开发服务器上很愉快。

我的appEngine dev服务器响应此请求: http://localhost:8080/service/greeting/getGreeting

有了这个回复:

{"id":1,"content":"Hello, World!"}

但是,将相同的部署到appEngine生产会产生“不可接受的”#。

使用我的Chrome高级REST客户端查看详细信息,可以在本地获取此信息:

Status
200 OK Show explanation Loading time: 13
Request headers 
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36
Content-Type: text/plain; charset=utf-8 
Accept: */*
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8
Cookie: dev_appserver_login=test@example.com:false:18580476422013912411
Response headers 
Content-Type: application/json 
Server: Development/1.0 
Date: Sat, 24 Jan 2015 22:57:33 GMT 
Cache-Control: no-cache 
Expires: Fri, 01 Jan 1990 00:00:00 GMT 
Content-Length: 34 

但这就是生产:

Status
406 Not Acceptable Show explanation Loading time: 487
Request headers 
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36
Content-Type: text/plain; charset=utf-8 
Accept: */*
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8
Response headers 
Content-Type: text/html; charset=utf-8 
Content-Encoding: gzip 
Vary: Accept-Encoding 
Date: Sat, 24 Jan 2015 22:56:19 GMT 
Server: Google Frontend 
Cache-Control: private 
Content-Length: 170 
Alternate-Protocol: 80:quic,p=0.02

我可以看到来自prod的内容类型的明显区别:

Content-Type: text/html; charset=utf-8 
Content-Encoding: gzip 

..但如果这是问题,我不知道该怎么做。我的方法被注释为创建" application / json",但它几乎就像在prod中被忽略一样。 我已经确认所有的Jackson罐子都在最终的WAR文件中。

AppEngine日志中列出的实际错误是:

09:47:33.982 2015-01-25  406 244 B 8ms /service/greeting/getGreeting
124.169.143.81 - - [24/Jan/2015:14:47:33 -0800] "GET /service/greeting/getGreeting HTTP/1.1" 406 244 - "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36" "spring-mvc-test-zone.appspot.com" ms=8 cpu_ms=324 cpm_usd=0.000027 instance=00c61b117cb1cb2dbcd014386050a9f83a53 app_engine_release=1.9.17

任何提示?

我的pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>

    <groupId>com.bluemongo.springMvcTest</groupId>
    <artifactId>springMvcTest</artifactId>

    <properties>
        <appengine.app.version>4</appengine.app.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring.version>4.1.4.RELEASE</spring.version>
    </properties>

    <prerequisites>
        <maven>3.1.0</maven>
    </prerequisites>

    <dependencies>
        <!-- Compile/runtime dependencies -->
        <dependency>
            <groupId>com.google.appengine</groupId>
            <artifactId>appengine-api-1.0-sdk</artifactId>
            <version>1.9.17</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>com.googlecode.objectify</groupId>
            <artifactId>objectify</artifactId>
            <version>5.1.3</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.5.0</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.5.0</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.5.0</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-core-asl</artifactId>
            <version>1.9.13</version>
        </dependency>

        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.13</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.3.1</version>
        </dependency>



        <!-- Test Dependencies -->
        <dependency>
            <groupId>com.google.appengine</groupId>
            <artifactId>appengine-testing</artifactId>
            <version>1.9.10</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.google.appengine</groupId>
            <artifactId>appengine-api-stubs</artifactId>
            <version>1.9.10</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <!-- for hot reload of the web application-->
        <outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>versions-maven-plugin</artifactId>
                <version>2.1</version>
                <executions>
                    <execution>
                        <phase>compile</phase>
                        <goals>
                            <goal>display-dependency-updates</goal>
                            <goal>display-plugin-updates</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <version>3.1</version>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archiveClasses>true</archiveClasses>
                    <webResources>
                        <!-- in order to interpolate version from pom into appengine-web.xml -->
                        <resource>
                            <directory>${basedir}/src/main/webapp/WEB-INF</directory>
                            <filtering>true</filtering>
                            <targetPath>WEB-INF</targetPath>
                        </resource>
                    </webResources>
                </configuration>
            </plugin>

            <plugin>
                <groupId>com.google.appengine</groupId>
                <artifactId>appengine-maven-plugin</artifactId>
                <version>1.9.10</version>
                <configuration>
                    <enableJarClasses>false</enableJarClasses>
                    <!-- Comment in the below snippet to bind to all IPs instead of just localhost -->
                    <!-- address>0.0.0.0</address>
                    <port>8080</port -->
                    <!-- Comment in the below snippet to enable local debugging with a remove debugger
                         like those included with Eclipse or IntelliJ -->
                    <!-- jvmFlags>
                      <jvmFlag>-agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n</jvmFlag>
                    </jvmFlags -->
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

我的控制器:

package com.bluemongo.springMvcTest.controller;

import com.bluemongo.springMvcTest.model.Greeting;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import java.util.concurrent.atomic.AtomicLong;

@RestController
@RequestMapping("/service/greeting")
public class RestGreetingController {
    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

@RequestMapping( value = "/getGreeting", method = RequestMethod.GET, headers="Accept=application/json", produces = {"application/json"})
@ResponseStatus(HttpStatus.OK)

    public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
        return new Greeting(counter.incrementAndGet(), String.format(template, name));
        //return "Sending a String was successful";
    }
}

我的模特:

package com.bluemongo.springMvcTest.model;

import java.io.Serializable;

public class Greeting implements Serializable{


    private static final long serialVersionUID = -1576547040362820422L;
    private final long id;
    private final String content;

    public Greeting(long id, String content) {
        this.id = id;
        this.content = content;
    }

    public long getId() {
        return id;
    }

    public String getContent() {
        return content;
    }

    public static long getSerialVersionUID() {
        return serialVersionUID;
    }
}

我的dispatcher-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans
       xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd

        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.0.xsd

        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

    <bean class="com.bluemongo.springMvcTest.controller.HelloWorldController"></bean>
    <bean class="com.bluemongo.springMvcTest.controller.AnotherController"></bean>
    <bean class="com.bluemongo.springMvcTest.controller.RestGreetingController"></bean>

<!--    <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="order" value="1" />
    <property name="ignoreAcceptHeader" value="true" />
    <property name="mediaTypes">
        <map>
            <entry key="json" value="application/json"/>
        </map>
    </property>
        <property name="defaultViews">
        <list>
        &lt;!&ndash; JSON View &ndash;&gt;
        <bean
                class="org.springframework.web.servlet.view.json.MappingJackson2JsonView">
        </bean>
        </list>
        </property>
    </bean>-->
    <mvc:annotation-driven/>
    <!--  <mvc:annotation-driven content-negotiation-manager="contentManager"/>
       <bean id="contentManager"
              class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
            <property name="favorPathExtension" value="true"/>
            <property name="ignoreAcceptHeader" value="true" />
            <property name="defaultContentType" value="application/json" />
            <property name="useJaf" value="false"/>
            <property name="mediaTypes">
                <map>
                    <entry key="json" value="application/json" />
                    <entry key="html" value="text/html" />
                    <entry key="xml" value="application/xml" />
                </map>
            </property>
        </bean>-->

    <bean id="jspViewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
        <property name="order"  value="3"/>
    </bean>

    <!-- Configure to plugin JSON as request and response in method handler -->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="jsonMessageConverter"/>
            </list>
        </property>
    </bean>

    <!-- Configure bean to convert JSON to POJO and vice versa -->
    <bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
    </bean>
</beans>

1 个答案:

答案 0 :(得分:2)

您应该在客户端中明确定义您只接受application / json。因此,对于Chrome Advanced REST Client,请务必将Accept标头设置为Accept = application / json,而不是Accept = * / *。