如果我使用String作为参数但是它不适用于模型类,为什么下面的服务可以正常工作。我根本没有得到错误,但如果我使用模型而不是String,它总是会收到null。注意:我确实在mvc-dispatcher-servelet和alls Jackson库中有mvc:annotation-driven。我的猜测是我对库有些错误,因为它在将应用程序从Spring 3更改为Spring 4之前运行正常。请问,有人可以检查一下我是否需要在POM中使用其他东西?
@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);
}
}
}
有效的Pom:
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>MHE2</groupId>
<artifactId>MHE2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>MHE Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<org.aspectj-version>1.7.4</org.aspectj-version>
<spring.version>4.1.2.RELEASE</spring.version>
<java-version>1.6</java-version>
<jackson.databind-version>2.2.3</jackson.databind-version>
<org.slf4j-version>1.7.5</org.slf4j-version>
</properties>
<dependencies>
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.3</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.3.3</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.1.2.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.2.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.2.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.2.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.2.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.1.2.RELEASE</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.1.2.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.1.2.RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
<repositories>
<repository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>Central Repository</name>
<url>http://repo.maven.apache.org/maven2</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<releases>
<updatePolicy>never</updatePolicy>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>Central Repository</name>
<url>http://repo.maven.apache.org/maven2</url>
</pluginRepository>
</pluginRepositories>
<build>
<sourceDirectory>C:\STS\ws\MHE_original\src</sourceDirectory>
<scriptSourceDirectory>C:\STS\ws\MHE_original\src\main\scripts</scriptSourceDirectory>
<testSourceDirectory>C:\STS\ws\MHE_original\src\test\java</testSourceDirectory>
<outputDirectory>C:\STS\ws\MHE_original\WebContent\WEB-INF\classes</outputDirectory>
<testOutputDirectory>C:\STS\ws\MHE_original\target\test-classes</testOutputDirectory>
<resources>
<resource>
<directory>C:\STS\ws\MHE_original\src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<testResources>
<testResource>
<directory>C:\STS\ws\MHE_original\src\test\resources</directory>
</testResource>
</testResources>
<directory>C:\STS\ws\MHE_original\target</directory>
<finalName>MHE2-0.0.1-SNAPSHOT</finalName>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.3</version>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-5</version>
</plugin>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
</plugin>
<plugin>
<artifactId>maven-release-plugin</artifactId>
<version>2.3.2</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<executions>
<execution>
<id>default-testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</execution>
<execution>
<id>default-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</execution>
</executions>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>default-war</id>
<phase>package</phase>
<goals>
<goal>war</goal>
</goals>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
</configuration>
</execution>
</executions>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
</configuration>
</plugin>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>default-clean</id>
<phase>clean</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>default-install</id>
<phase>install</phase>
<goals>
<goal>install</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>default-resources</id>
<phase>process-resources</phase>
<goals>
<goal>resources</goal>
</goals>
</execution>
<execution>
<id>default-testResources</id>
<phase>process-test-resources</phase>
<goals>
<goal>testResources</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<executions>
<execution>
<id>default-test</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>default-deploy</id>
<phase>deploy</phase>
<goals>
<goal>deploy</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.3</version>
<executions>
<execution>
<id>default-site</id>
<phase>site</phase>
<goals>
<goal>site</goal>
</goals>
<configuration>
<outputDirectory>C:\STS\ws\MHE_original\target\site</outputDirectory>
<reportPlugins>
<reportPlugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
</reportPlugin>
</reportPlugins>
</configuration>
</execution>
<execution>
<id>default-deploy</id>
<phase>site-deploy</phase>
<goals>
<goal>deploy</goal>
</goals>
<configuration>
<outputDirectory>C:\STS\ws\MHE_original\target\site</outputDirectory>
<reportPlugins>
<reportPlugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
</reportPlugin>
</reportPlugins>
</configuration>
</execution>
</executions>
<configuration>
<outputDirectory>C:\STS\ws\MHE_original\target\site</outputDirectory>
<reportPlugins>
<reportPlugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
</reportPlugin>
</reportPlugins>
</configuration>
</plugin>
</plugins>
</build>
<reporting>
<outputDirectory>C:\STS\ws\MHE_original\target\site</outputDirectory>
</reporting>
</project>
从WebSphere 8.5 Liberty概要文件中记录
[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
...
[警告]跳过[C:\ Users \ e049447.m2 \ repository \ org \ springframework \ spring-expression \ 4.1.2.RELEASE \ spring-expression-4.1.2.RELEASE.jar]因为它不表示目录< / p>
[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.
答案 0 :(得分:0)
就像@Bohuslav在评论中所说,@RequestBody
和@ModelAttribute
不得在同一方法参数上使用。删除@ModelAttribute
,然后重试。之后,您的模型参数将正确填充。