我正在用SpringMVC,Gson制作一个简单的RESTful服务器,并将其部署在GAE中。如果我尝试按以下方式映射我的请求,则一切正常:
import java.util.Arrays;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ResponseBody;
import com.generic.server.model.Widget;
import com.generic.server.services.WidgetService;
import com.google.gson.Gson;
@Component
@Path("/widget")
public class WidgetRestService {
/**
* @return All the widgets info.
* @uri http://localhost:8888/rest/widget/
*/
@GET @Path("/") @Produces(MediaType.APPLICATION_JSON)
public @ResponseBody String getAll() {
Gson g = new Gson();
return g.toJson(Arrays.asList(new Widget("BuyerApp", "Buy something now!"),
new Widget("DogSwitcher", "Tired of your dog? Switch it right now!")));
}
}
这样可以打印出所需的结果。但是我想摆脱那个annoyinh
Gson
实例。所以我做了自己的自定义HttpMessageConverter
。@Component
public class GSONHttpMessageConverter extends AbstractHttpMessageConverter<Object> {
public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
private GsonBuilder gsonBuilder = new GsonBuilder()
.excludeFieldsWithoutExposeAnnotation()
.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
public GSONHttpMessageConverter() {
super(new MediaType("application", "json", DEFAULT_CHARSET));
}
@Override
protected boolean supports(Class<?> clazz) {
// should not be called, since we override canRead/Write instead
throw new UnsupportedOperationException();
}
@Override
public boolean canRead(Class<?> clazz, MediaType mediaType) {
return MediaType.APPLICATION_JSON.isCompatibleWith(mediaType);
}
public boolean canWrite(Class<?> clazz, MediaType mediaType) {
return MediaType.APPLICATION_JSON.isCompatibleWith(mediaType);
}
public void registerTypeAdapter(Type type, Object serializer) {
gsonBuilder.registerTypeAdapter(type, serializer);
}
@Override
protected Object readInternal(Class<? extends Object> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
try {
Gson gson = gsonBuilder.create();
return gson.fromJson(StringUtils.convertStreamToString(inputMessage.getBody()), clazz);
} catch (JsonParseException e) {
throw new HttpMessageNotReadableException("Could not read JSON: " + e.getMessage(), e);
}
}
@Override
protected void writeInternal(Object o, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
Type genericType = TypeToken.get(o.getClass()).getType();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputMessage.getBody(), DEFAULT_CHARSET));
try {
// See http://code.google.com/p/google-gson/issues/detail?id=199 for details on SQLTimestamp conversion
Gson gson = gsonBuilder.create();
writer.append(gson.toJson(o, genericType));
} finally {
writer.flush();
writer.close();
}
}
}
并将其添加到
applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.0.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<mvc:annotation-driven />
<!-- If you hit /home/ on the browser this will redirect you to the login
view -->
<mvc:view-controller path="/" view-name="login" />
<mvc:resources location="/resources/" mapping="/resources/**" />
<context:component-scan
base-package="com.generic.server.services,
com.generic.server.model,
com.generic.server.rest,
com.generic.server.ui.controller" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/client/</value>
</property>
<property name="suffix">
<value>.html</value>
</property>
</bean>
<mvc:default-servlet-handler />
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="com.generic.server.util.GSONHttpMessageConverter"/>
</list>
</property>
</bean>
</beans>
现在,我将更改映射到返回对象的请求的方式:
/**
* @return All the widgets info.
* @uri http://localhost:8888/rest/widget/
*/
@GET @Path("/") @Produces(MediaType.APPLICATION_JSON)
public @ResponseBody List<Widget> getAll() {
return Arrays.asList(new Widget("BuyerApp", "Buy something now!"),
new Widget("DogSwitcher", "Tired of your dog? Switch it right now!"));
}
但是,当我尝试访问localhost:8888 / rest / widget /时,服务器崩溃并显示以下消息:
javax.ws.rs.WebApplicationException
at com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:268)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1029)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:941)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:932)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:384)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:451)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:632)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)
at com.google.appengine.api.socket.dev.DevSocketFilter.doFilter(DevSocketFilter.java:74)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.tools.development.ResponseRewriterFilter.doFilter(ResponseRewriterFilter.java:123)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.tools.development.HeaderVerificationFilter.doFilter(HeaderVerificationFilter.java:34)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.api.blobstore.dev.ServeBlobFilter.doFilter(ServeBlobFilter.java:63)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:43)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.tools.development.StaticFileFilter.doFilter(StaticFileFilter.java:125)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.tools.development.DevAppServerModulesFilter.doDirectRequest(DevAppServerModulesFilter.java:368)
at com.google.appengine.tools.development.DevAppServerModulesFilter.doDirectModuleRequest(DevAppServerModulesFilter.java:351)
at com.google.appengine.tools.development.DevAppServerModulesFilter.doFilter(DevAppServerModulesFilter.java:116)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
at com.google.appengine.tools.development.DevAppEngineWebAppContext.handle(DevAppEngineWebAppContext.java:97)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at com.google.appengine.tools.development.JettyContainerService$ApiProxyHandler.handle(JettyContainerService.java:485)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:326)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:923)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:547)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:409)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)
我是GAE的新手,但是我在SO和其他页面中进行了搜索,我认为这是映射REST请求的方法。任何提示,不胜感激。
更新:
我还尝试使用RestTemplate中的
GsonHttpMessageConverter
类。引起我注意的另一件事是,如果删除AnnotationMethodHandlerAdapter
bean,则会得到相同的堆栈跟踪。 GsonHttpMessageConverter
未被使用。我还尝试实现自己的
WebMvcConfigurationSupport
并以编程方式在其中添加我的消息转换器,如下所示:@Configuration
public class WebConfig extends WebMvcConfigurationSupport {
@Bean
public RequestMappingHandlerAdapter requestMappingHandlerAdapter() {
RequestMappingHandlerAdapter handlerAdapter = super.requestMappingHandlerAdapter();
handlerAdapter.getMessageConverters().add(0, new GSONHttpMessageConverter());
return handlerAdapter;
}
但这既不能覆盖
requestMappingHandlerAdapter
也不能覆盖configureMessageConverters
方法。堆栈跟踪与未更改时一样。解:
我的应用程序上下文:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.0.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
<!-- If you hit /home/ on the browser this will redirect you to the login
view -->
<mvc:view-controller path="/" view-name="login" />
<mvc:resources location="/resources/" mapping="/resources/**" />
<context:component-scan
base-package="com.generic.server.services,
com.generic.server.model,
com.generic.server.rest,
com.generic.server.ui.controller" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/client/</value>
</property>
<property name="suffix">
<value>.html</value>
</property>
</bean>
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="com.generic.server.util.GsonHttpMessageConverter" />
</mvc:message-converters>
</mvc:annotation-driven>
</beans>
我的网络服务:
@Controller
@RequestMapping("/rest")
public class WidgetRestService {
/**
* @return All the widgets info.
* @uri http://localhost:8888/rest/widget/
*/
@RequestMapping(value="/widget", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON)
public @ResponseBody List<Widget> getAll() {
return Arrays.asList(new Widget("BuyerApp", "Buy something now!"), new Widget("DogSwitcher", "Tired of your dog? Switch it right now!"));
}
}
主要问题是我试图使用Jersey公开Web服务。取而代之的是,我进行了更改,现在使用SpringMVC注释。
最佳答案
您正在使用<mvc:annotation-driven />
,并且还注册了与AnnotationMethodHandlerAdapter
AnnotationMethodHandlerAdapter`注册的RequestMappingHandlerAdapter
冲突的<mvc:annotation-driven />' (So basically the
,它什么也不做,只会消耗服务器内存。
相反,您应该使用名称空间来注册您的转换器。
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="com.generic.server.util.GSONHttpMessageConverter"/>
</mvc:message-converters>
</mvc:annotation-driven>
接下来,您将在其余端点中混合使用JAX-WS和Spring MVC注释。假设您要使用Spring,请将其更改为以下内容
@Controller
@RequestMapping("/widget")
public class WidgetRestService {
/**
* @return All the widgets info.
* @uri http://localhost:8888/rest/widget/
*/
@RequestMapping(method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON)
public @ResponseBody String getAll() {
return Arrays.asList(new Widget("BuyerApp", "Buy something now!"), new Widget("DogSwitcher", "Tired of your dog? Switch it right now!"));
}
}
除了创建自己的转换器,您可能想签出spring-android项目并使用其GsonHttpMessageConverter。