Java 谷歌AppEngine:BasicSpringMVC4rest&x2B;JSON在dev中工作,但在prod中不工作
我有一个非常基本的SpringMVC4REST+JSON演示,我正试图在GoogleAppengine上运行,但我无法通过生产代码中的“406不可接受”响应。同样的代码在AppEngine dev服务器上运行良好 我的appEngine开发服务器响应此请求: 有了这一答复:Java 谷歌AppEngine:BasicSpringMVC4rest&x2B;JSON在dev中工作,但在prod中不工作,java,json,rest,google-app-engine,spring-mvc,Java,Json,Rest,Google App Engine,Spring Mvc,我有一个非常基本的SpringMVC4REST+JSON演示,我正试图在GoogleAppengine上运行,但我无法通过生产代码中的“406不可接受”响应。同样的代码在AppEngine dev服务器上运行良好 我的appEngine开发服务器响应此请求: 有了这一答复: {"id":1,"content":"Hello, World!"} 但将其部署到appEngine生产中会导致“406不可接受” 使用我的Chrome Advanced REST客户端查看详细信息时,我在本地看到: S
{"id":1,"content":"Hello, World!"}
但将其部署到appEngine生产中会导致“406不可接受”
使用我的Chrome Advanced 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
但在prod上:
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中这几乎被忽略了。
我已经确认,所有的杰克逊罐子也在最终的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
有什么提示吗
My 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.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;
}
}
my 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>
<!– JSON View –>
<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 Advanced REST客户端,请确保将Accept头设置为Accept=application/json,而不是Accept=*/*。Hi@BrianReindel,感谢您的响应。Accept头在Chrome REST客户机中似乎不可配置,而且不管怎样,具有相同Accept头的开发输出在同一客户机上工作正常。这排除了吗?还引用“仅当接受请求标头与其中一个值匹配时,才会匹配请求。此外,使用PRODUCTS条件可确保用于生成响应的实际内容类型符合PRODUCTS条件中指定的媒体类型。”我能想到的另一件事是您的本地环境和prod环境之间有什么不同?这可能是两件不同的事情。您的servlet容器(Tomcat、JBoss、Websphere等)是否已经在类路径上全局部署了库,这些库会导致与已部署工件中的库发生冲突?另外,Apache是介于两者之间,还是其他一些web服务器以某种方式操纵请求或响应中的头?谢谢@BrianReindel,我实际上使用的是appEngine SDK附带的默认appEngine开发服务器(我相信它基于Jetty)。我肯定我在某处犯了一些n00b错误,但我就是看不出来:)你成功地解决了这个问题吗?我也陷入了类似的困境:)嗨@StasS,我最终做的是使用Google计算引擎而不是AppEngine构建自己的环境。同样的代码在GCE中运行良好。我认为这是AppEngine的一些局限性,我没有发现这些局限性。
<?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>
<!– JSON View –>
<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>