@RequestBody使用字符串,但不使用pojo模型Rest Web服务
如果我使用字符串作为参数,但它不适用于模型类,那么为什么下面的服务可以完美地工作。我根本没有得到错误,但如果我使用模型而不是字符串,它总是接收null。注意:我在mvc dispatcher servelet和Java库中有mvc:annotation驱动。我的猜测是,在我将应用程序从Spring3更改为Spring4之前,库运行正常,所以我认为库有一些问题。拜托,能帮我查一下POM里还有什么需要的吗@RequestBody使用字符串,但不使用pojo模型Rest Web服务,rest,spring-mvc,jackson,Rest,Spring Mvc,Jackson,如果我使用字符串作为参数,但它不适用于模型类,那么为什么下面的服务可以完美地工作。我根本没有得到错误,但如果我使用模型而不是字符串,它总是接收null。注意:我在mvc dispatcher servelet和Java库中有mvc:annotation驱动。我的猜测是,在我将应用程序从Spring3更改为Spring4之前,库运行正常,所以我认为库有一些问题。拜托,能帮我查一下POM里还有什么需要的吗 @Controller @RequestMapping("/log") public clas
@Controller
@RequestMapping("/log")
public class Lo_Controller {
@RequestMapping(value="display/last2", method=RequestMethod.POST, headers = "Accept=application/json")
@ResponseStatus(HttpStatus.CREATED)
@ResponseBody
//after taking out @ModelAtribute I don't reach this method when debugging
public String getTest(@RequestBody Mas60010b mas60010b) { //if I change Mas60010b mas60010b to String strTest ...
Lo_Mas60010 lo_Mas60010 = new Lo_Mas60010();
System.out.println(mas60010b.getSubCd());
//System.out.println(strTest); //... it will work
return "returnTestPost";
}
型号:
public class Mas60010b {
private String subCd;
private String firstDT;
private String currDT;
public String getSubCd() {
return subCd;
}
public void setSubCd(String subCd) {
this.subCd = subCd;
}
public String getFirstDT() {
return firstDT;
}
public void setFirstDT(String firstDT) {
this.firstDT = firstDT;
}
public String getCurrDT() {
return currDT;
}
public void setCurrDT(String currDT) {
this.currDT = currDT;
}
客户测试人员
public class Main {
public static void main(String[] args) throws ClientProtocolException, IOException {
HttpClient client2 = new DefaultHttpClient();
HttpPost post2 = new HttpPost("http://localhost:8080/MHE2/log/display/last2");
Map<String, String> map2 = new HashMap<String, String>();
map2.put("subCd", "A");
map2.put("firstDT", "2014-09-29 00:00:00.0");
map2.put("currDT", "2014-09-30 16:45:33.379731");
ObjectMapper mapper2 = new ObjectMapper();
String strJson2 = mapper2.writeValueAsString(map2);
StringEntity input2 = new StringEntity(strJson2);
input2.setContentType("application/json");
post2.setEntity(input2);
//Without @ModelAtribute I get [WARNING ] SRVE8094W: WARNING: Cannot set header. Response already committed.
HttpResponse response2 = client2.execute(post2);
BufferedReader rd2 = new BufferedReader(new InputStreamReader(response2.getEntity().getContent()));
String line2 = "";
while ((line2 = rd2.readLine()) != null) {
System.out.println(line2);
}
}
}
…[警告]正在跳过[C:\Users\e04947.m2\repository\org\springframework\spring expression\4.1.2.RELEASE\spring-expression-4.1.2.RELEASE.jar],因为它不表示目录
[WARNING ] SRVE8094W: WARNING: Cannot set header. Response already committed.
[12/3/14 8:55:38:812 CST] 0000001a ramework.core.io.support.PathMatchingResourcePatternResolver W Cannot search for matching files underneath URL [bundleresource://96.fwk327208746/] because it does not correspond to a directory in the file system
java.io.FileNotFoundException: URL [bundleresource://96.fwk327208746/] cannot be resolved to absolute file path because it does not reside in the file system: bundleresource://96.fwk327208746/
at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:212)
at org.springframework.core.io.AbstractFileResolvingResource.getFile(AbstractFileResolvingResource.java:52)
at org.springframework.core.io.UrlResource.getFile(UrlResource.java:212)
正如@Bohuslav在其评论中所说,
@RequestBody
和@modeldattribute
不得用于同一方法参数。请删除@modeldattribute
,然后重试。之后,您的模型参数将正确填充。我认为您不应该同时使用@RequestBody
和@modeldattribute
。试着使用just RequestBody看看这是否有什么不同。首先,感谢您的评论。我尝试删除@modeldattribute,但现在从客户端调用时得到“[WARNING]SRVE8094W:WARNING:无法设置头。响应已提交”。我添加了我的有效pom。我尝试从pom中取出jackson库并将其粘贴到web inf/lib中,然后手动添加它们,而不是使用maven,但我仍然面临着这个警告。我尝试了它,因为我可以看到问题只发生在向模型发送贴图时。pom可能有问题,因为在我将此项目转换为maven之前,此web服务工作正常。我获取了您的代码,创建了一个,并在我的机器上进行了尝试。这个项目运作得很好。只需将其下载到您的计算机上,然后以mvn clean tomcat7:run
的形式运行即可。然后,使用一个AJAX客户端,如用于Chrome的Postman扩展,向http://localhost:8080/log
。您应该可以看到提交的信息。我对POM-1做了一些小改动<代码>编译不是必需的。2.Servlet API必须提供作用域。您可以生成一个WAR并部署到其他应用服务器,然后再试一次。非常感谢。因为您告诉我我的代码是正确的,并且您在Tomcat中运行了它,所以我开始关注Websphere Liberty概要文件配置,并通过添加json特性更改了server.xml。我不知道其中包含的每一件事情,但肯定是与此功能配置相关的东西。对于未来的读者,这是我的server.xml jsp-2.2 localConnector-1.0 jdbc-4.0 jaxrs-1.1 json-1.0的结尾…外加额外的数据源configsGreat!WebSphere要正确启动和运行总是一件痛苦的事情。
[WARNING ] Cannot search for matching files underneath URL [bundleresource://96.fwk327208746/] because it does not correspond to a directory in the file system
URL [bundleresource://96.fwk327208746/] cannot be resolved to absolute file path because it does not reside in the file system: bundleresource://96.fwk327208746/
[WARNING ] Cannot search for matching files underneath URL [bundleresource://95.fwk327208746/] because it does not correspond to a directory in the file system
URL [bundleresource://95.fwk327208746/] cannot be resolved to absolute file path because it does not reside in the file system: bundleresource://95.fwk327208746/
[WARNING ] Skipping [C:\STS\ws\MHE_original\WebContent\WEB-INF\lib\db2java.jar] because it does not denote a directory
[WARNING ] Skipping [C:\STS\ws\MHE_original\WebContent\WEB-INF\lib\jackson-core-asl-1.9.12.jar] because it does not denote a directory
[WARNING ] SRVE8094W: WARNING: Cannot set header. Response already committed.
[12/3/14 8:55:38:812 CST] 0000001a ramework.core.io.support.PathMatchingResourcePatternResolver W Cannot search for matching files underneath URL [bundleresource://96.fwk327208746/] because it does not correspond to a directory in the file system
java.io.FileNotFoundException: URL [bundleresource://96.fwk327208746/] cannot be resolved to absolute file path because it does not reside in the file system: bundleresource://96.fwk327208746/
at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:212)
at org.springframework.core.io.AbstractFileResolvingResource.getFile(AbstractFileResolvingResource.java:52)
at org.springframework.core.io.UrlResource.getFile(UrlResource.java:212)
[12/3/14 8:55:39:470 CST] 0000001a b.servlet.mvc.method.annotation.RequestMappingHandlerMapping I Mapped "{[/log/display/last2],methods=[POST],params=[],headers=[],consumes=[],produces=[application/json],custom=[]}" onto public java.lang.String com.mastercard.mhe.common.controller.Lo_Controller.getTest(com.mastercard.mhe.log.domain2.Mas60010b)
[12/3/14 8:55:39:782 CST] 0000001a b.servlet.mvc.method.annotation.RequestMappingHandlerAdapter I Looking for @ControllerAdvice: WebApplicationContext for namespace 'mvc-dispatcher-servlet': startup date [Wed Dec 03 08:55:37 CST 2014]; parent: Root WebApplicationContext
[12/3/14 8:55:40:063 CST] 0000001a b.servlet.mvc.method.annotation.RequestMappingHandlerAdapter I Looking for @ControllerAdvice: WebApplicationContext for namespace 'mvc-dispatcher-servlet': startup date [Wed Dec 03 08:55:37 CST 2014]; parent: Root WebApplicationContext
[12/3/14 8:55:40:267 CST] 0000001a .springframework.web.servlet.handler.SimpleUrlHandlerMapping I Mapped URL path [/**] onto handler 'org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0'
[12/3/14 8:55:40:313 CST] 0000001a org.springframework.web.servlet.DispatcherServlet I FrameworkServlet 'mvc-dispatcher': initialization completed in 2548 ms
[12/3/14 8:55:40:313 CST] 0000001a com.ibm.ws.webcontainer.servlet I SRVE0242I: [MHE_original] [/MHE2] [mvc-dispatcher]: Initialization successful.
[12/3/14 8:55:40:470 CST] 00000022 SystemOut O null
[12/3/14 8:56:29:485 CST] 0000001a com.ibm.ws.webcontainer.srt W SRVE8094W: WARNING: Cannot set header. Response already committed.