Spring 带有协议缓冲区的 MVC 4 REST 不工作
Spring MVC 4 REST with protocol buffers not working
我正在尝试使用 Spring MVC 4 的 Rest 模板来支持 google 协议缓冲区作为消息格式。我在 Spring 框架博客上关注这个 post
spring-mvc-google-protocol-buffers
我查看了源代码,试图在我的环境中实现它。
我有两个问题 - 当我将 Java.version 转换为 1.6 时我无法编译它并且我无法让它作为 webapp 工作(不知道是什么
将是转换后的 war 文件的上下文根)
-详情-
我需要使此代码作为 Web 应用程序工作并部署在 java6 容器上(weblogic 10.3.6 -servlet 2.5 兼容)
所以我更改了代码库中的 java 8 功能,使其与 Java 6 兼容。
唯一的问题是当我更改 pom.xml 的以下部分时
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>demo.DemoApplication</start-class>
<java.version>1.8</java.version>
</properties>
将 java.version 值更改为 1.6,然后尝试执行 mvn clean install ,DemoApplicationTests class 无法编译并出现此错误。
-google-protocol-buffers-master\src\test\java\demo\DemoApplicationTests.java:28: cannot find symbol
[ERROR] symbol : constructor RestTemplate(java.util.List<org.springframework.http.converter.protobuf.ProtobufHttpMessageConverter>) is not defined
[ERROR] location: class org.springframework.web.client.RestTemplate
下面的 link 显示 Spring 代码库通常没有任何 Java 8 特定的源代码,所以不确定为什么这段代码只能在 Java 8 中编译
https://spring.io/blog/2015/04/03/how-spring-achieves-compatibility-with-java-6-7-and-8
-------------------------------------------- ----------------------------------
下面的 link 显示了如何将 spring 引导应用程序转换为 WAR 应用程序。
我确实将 pom.xml 打包选项更改为 war。
代码由 mvn clean install 毫无问题地构建,并生成 .war 文件。
但是没有 web.xml - 所以我不知道部署的网络应用程序的上下文根是什么。
我要么在 weblogic 10.3.6 上部署了 webapp(与 java 6 兼容)
它部署得很好。
但是当我 运行 DemoApplicationTests(我已经更改为直接指向 URL
使用此调用(通过单击已部署的 Web 应用程序从 WebLogic 控制台获取上下文根)
ResponseEntity<CustomerProtos.Customer> customer = restTemplate.getForEntity(
"http://127.0.0.1:7001/demo-0.0.1-SNAPSHOT/customers/2", CustomerProtos.Customer.class);
我一直收到 404 未找到错误。
我已将更改后的代码放在这里。
https://github.com/robinbajaj123/spring-and-google-protocol-buffers
我们将不胜感激您的反馈。
您需要将 Spring 启动应用程序转换为有效的 Servlet 应用程序。如果您使用的是 Servlet 3 或更高版本,并且从 start.spring.io you'd get a ServletIntializer which is a Java class that is the programmatic equivalent of web.xml
. Since you're using 2.5, not 3.0, you need an explicit web.xml
. You might check out this sample on how to get a Boot app hoisted up in a Servlet 2.5 environment, though using Servlet 2.5 is not recommended! 中选择了基于 .war
的部署。值得一提的是,2009 年引入了 Servlet 3.0 支持。
最后,此代码使用 Java 8 个 lambda。您需要将 lambda 替换为 Java 6 等价代码。我看到的一个例子是:
@Bean
CustomerRepository customerRepository() {
...
@Bean
定义中的最后一行 returns lambda:customers::get
。将其替换为:
final Map<Integer, CustomerProtos.Customer> customers =
new ConcurrentHashMap<Integer, CustomerProtos.Customer>();
return new CustomerRepository() {
public CustomerProtos.Customer findById(int id) {
return customers.get( id) ;
}
};
同样,用老式的 for-in 循环替换 List
中的 forEach
方法:
for (CustomerProtos.Customer c : Arrays.asList( ... )) {
customers.put(c.getId(), c);
}
我正在尝试使用 Spring MVC 4 的 Rest 模板来支持 google 协议缓冲区作为消息格式。我在 Spring 框架博客上关注这个 post spring-mvc-google-protocol-buffers 我查看了源代码,试图在我的环境中实现它。
我有两个问题 - 当我将 Java.version 转换为 1.6 时我无法编译它并且我无法让它作为 webapp 工作(不知道是什么 将是转换后的 war 文件的上下文根)
-详情- 我需要使此代码作为 Web 应用程序工作并部署在 java6 容器上(weblogic 10.3.6 -servlet 2.5 兼容)
所以我更改了代码库中的 java 8 功能,使其与 Java 6 兼容。 唯一的问题是当我更改 pom.xml 的以下部分时
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>demo.DemoApplication</start-class>
<java.version>1.8</java.version>
</properties>
将 java.version 值更改为 1.6,然后尝试执行 mvn clean install ,DemoApplicationTests class 无法编译并出现此错误。
-google-protocol-buffers-master\src\test\java\demo\DemoApplicationTests.java:28: cannot find symbol
[ERROR] symbol : constructor RestTemplate(java.util.List<org.springframework.http.converter.protobuf.ProtobufHttpMessageConverter>) is not defined
[ERROR] location: class org.springframework.web.client.RestTemplate
下面的 link 显示 Spring 代码库通常没有任何 Java 8 特定的源代码,所以不确定为什么这段代码只能在 Java 8 中编译 https://spring.io/blog/2015/04/03/how-spring-achieves-compatibility-with-java-6-7-and-8
-------------------------------------------- ----------------------------------
下面的 link 显示了如何将 spring 引导应用程序转换为 WAR 应用程序。 我确实将 pom.xml 打包选项更改为 war。 代码由 mvn clean install 毫无问题地构建,并生成 .war 文件。 但是没有 web.xml - 所以我不知道部署的网络应用程序的上下文根是什么。 我要么在 weblogic 10.3.6 上部署了 webapp(与 java 6 兼容) 它部署得很好。
但是当我 运行 DemoApplicationTests(我已经更改为直接指向 URL 使用此调用(通过单击已部署的 Web 应用程序从 WebLogic 控制台获取上下文根)
ResponseEntity<CustomerProtos.Customer> customer = restTemplate.getForEntity(
"http://127.0.0.1:7001/demo-0.0.1-SNAPSHOT/customers/2", CustomerProtos.Customer.class);
我一直收到 404 未找到错误。
我已将更改后的代码放在这里。 https://github.com/robinbajaj123/spring-and-google-protocol-buffers
我们将不胜感激您的反馈。
您需要将 Spring 启动应用程序转换为有效的 Servlet 应用程序。如果您使用的是 Servlet 3 或更高版本,并且从 start.spring.io you'd get a ServletIntializer which is a Java class that is the programmatic equivalent of web.xml
. Since you're using 2.5, not 3.0, you need an explicit web.xml
. You might check out this sample on how to get a Boot app hoisted up in a Servlet 2.5 environment, though using Servlet 2.5 is not recommended! 中选择了基于 .war
的部署。值得一提的是,2009 年引入了 Servlet 3.0 支持。
最后,此代码使用 Java 8 个 lambda。您需要将 lambda 替换为 Java 6 等价代码。我看到的一个例子是:
@Bean
CustomerRepository customerRepository() {
...
@Bean
定义中的最后一行 returns lambda:customers::get
。将其替换为:
final Map<Integer, CustomerProtos.Customer> customers =
new ConcurrentHashMap<Integer, CustomerProtos.Customer>();
return new CustomerRepository() {
public CustomerProtos.Customer findById(int id) {
return customers.get( id) ;
}
};
同样,用老式的 for-in 循环替换 List
中的 forEach
方法:
for (CustomerProtos.Customer c : Arrays.asList( ... )) {
customers.put(c.getId(), c);
}