java - 谷歌应用引擎 : basic Spring MVC 4 REST + JSON works in dev but not in prod

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

我有一个非常基本的 Spring MVC 4 REST + JSON 演示,我试图在 Google AppEngine 上运行它,但我无法通过生产中代码的“406 Not Acceptable ”响应。相同的代码在 AppEngine 开发服务器上运行良好。

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

对此回应:

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

但是将其部署到 appEngine 生产会导致“406 Not Acceptable ”。

使用我的 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: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5632332009372626253324203324093a39313f386b2233252216332e373b263a337835393b" rel="noreferrer noopener nofollow">[email protected]</a>: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

我可以看到的明显区别是产品的内容类型:

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

..但如果这就是问题所在,我不知道该怎么办。我的方法被注释为创建“application/json”,但这几乎就像在产品中被忽略一样。 我已经确认所有 Jackson jar 也都在生成的 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>

我的 Controller :

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;
    }
}

我的调度程序-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>

最佳答案

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

关于java - 谷歌应用引擎 : basic Spring MVC 4 REST + JSON works in dev but not in prod,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28131681/

相关文章:

java - 与线程模式混淆

java - 尝试运行 jar 时出现 NoClassDefFoundError

Node.js 中 Java 的 RSA/ECB/OAEPWithSHA-256AndMGF1Padding 等效项

java - RGB 颜色量化从 8 位到 6 位

java - (Arquillian REST 扩展)为什么 webtarget 在测试方法中为空?

c# - Rest API 设计(twitter)

c# - 使用 C# 仅获取 JSON 的一部分

java - 用java解析文件

javascript - 拼接 JSON 列表中的元素 - javascript

java - Unirest JSONElement NoSuchMethodError