$ curl -i -X POST -H "Content-Type:application/json" -H "Accept:application/json" http://localhost:8080/myapp/login -d '{"username":"someuser","password":"0","deviceId":"5","deviceTime":"2014-12-12 11:55:05.987"}'
返回HttpStatus 415
服务器端日志显示
的异常TRACE HandlerMethod - 解析参数错误[0] [type = com.npt.ws.model.MobileUserLoginRequest]
org.springframework.web.HttpMediaTypeNotSupportedException:内容类型' application / json'不支持
包含
的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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.1.xsd">
<!-- Activates mapping of @Controller -->
<context:component-scan base-package="com.npt.ws.web">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- Activates @Autowired for Controllers -->
<context:annotation-config proxy-target-class="true"/>
<!--Jackson - objectMapper format date setting -->
<bean id="objectMapper"
class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean"
p:indentOutput="true" p:simpleDateFormat="yyyy-MM-dd HH:mm:ss.SSS">
</bean>
<!--Jackson - registerModule JodaModule -->
<bean
class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"
p:targetObject-ref="objectMapper" p:targetMethod="registerModule">
<property name="arguments">
<list>
<bean class="com.fasterxml.jackson.datatype.joda.JodaModule" />
</list>
</property>
</bean>
<!-- Spring MVC: setting objectMapper in converter MappingJackson2HttpMessageConverter-->
<mvc:annotation-driven>
<mvc:message-converters>
<bean
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper" ref="objectMapper" />
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
</beans>
LoginRequest类
import java.sql.Timestamp;
import org.codehaus.jackson.map.annotate.JsonDeserialize;
import org.codehaus.jackson.map.annotate.JsonSerialize;
import org.joda.time.DateTime;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.npt.ws.utils.CustomDateDeSerializer;
import com.npt.ws.utils.CustomDateSerializer;
public class MobileUserLoginRequest {
private String username;
private String password;
private String deviceId;
private String deviceName;
private String devicePhoneNum;
private String serviceCarrierId;
private String deviceType;
private String deviceOS;
private String browserName;
private String browserVersion;
private String deviceModel;
private String appVersion;
private DateTime deviceTime;
private Double lattitude;
private Double longitude;
private String devicePowerStatus;
private Integer deviceBatteryRemaining;
private Integer appRAMUsed;
private Integer deviceAvailableRAM;
private Integer deviceSDAvailableSpace;
private String ipAddress;
private String locationAPI;
private String locationAPIAccuracy;
public MobileUserLoginRequest() {
super();
}
public MobileUserLoginRequest(String username, String password,
String deviceId) {
super();
this.username = username;
this.password = password;
this.deviceId = deviceId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getDeviceId() {
return deviceId;
}
public void setDeviceId(String deviceId) {
this.deviceId = deviceId;
}
public String getDeviceName() {
return deviceName;
}
public void setDeviceName(String deviceName) {
this.deviceName = deviceName;
}
public String getDevicePhoneNum() {
return devicePhoneNum;
}
public void setDevicePhoneNum(String devicePhoneNum) {
this.devicePhoneNum = devicePhoneNum;
}
public String getServiceCarrierId() {
return serviceCarrierId;
}
public void setServiceCarrierId(String serviceCarrierId) {
this.serviceCarrierId = serviceCarrierId;
}
public String getDeviceType() {
return deviceType;
}
public void setDeviceType(String deviceType) {
this.deviceType = deviceType;
}
public String getDeviceOS() {
return deviceOS;
}
public void setDeviceOS(String deviceOS) {
this.deviceOS = deviceOS;
}
public String getBrowserName() {
return browserName;
}
public void setBrowserName(String browserName) {
this.browserName = browserName;
}
public String getBrowserVersion() {
return browserVersion;
}
public void setBrowserVersion(String browserVersion) {
this.browserVersion = browserVersion;
}
public String getDeviceModel() {
return deviceModel;
}
public void setDeviceModel(String deviceModel) {
this.deviceModel = deviceModel;
}
public String getAppVersion() {
return appVersion;
}
public void setAppVersion(String appVersion) {
this.appVersion = appVersion;
}
//@JsonSerialize(using = CustomDateSerializer.class)
public DateTime getDeviceTime() {
return deviceTime;
}
//@JsonDeserialize(using = CustomDateDeSerializer.class)
public void setDeviceTime(DateTime deviceTime) {
this.deviceTime = deviceTime;
}
public void setDeviceTime(Timestamp deviceTime) {
this.deviceTime = new DateTime(deviceTime.getTime());
}
public Double getLattitude() {
return lattitude;
}
public void setLattitude(Double lattitude) {
this.lattitude = lattitude;
}
public Double getLongitude() {
return longitude;
}
public void setLongitude(Double longitude) {
this.longitude = longitude;
}
public String getDevicePowerStatus() {
return devicePowerStatus;
}
public void setDevicePowerStatus(String devicePowerStatus) {
this.devicePowerStatus = devicePowerStatus;
}
public Integer getDeviceBatteryRemaining() {
return deviceBatteryRemaining;
}
public void setDeviceBatteryRemaining(Integer deviceBatteryRemaining) {
this.deviceBatteryRemaining = deviceBatteryRemaining;
}
public Integer getAppRAMUsed() {
return appRAMUsed;
}
public void setAppRAMUsed(Integer appRAMUsed) {
this.appRAMUsed = appRAMUsed;
}
public Integer getDeviceAvailableRAM() {
return deviceAvailableRAM;
}
public void setDeviceAvailableRAM(Integer deviceAvailableRAM) {
this.deviceAvailableRAM = deviceAvailableRAM;
}
public Integer getDeviceSDAvailableSpace() {
return deviceSDAvailableSpace;
}
public void setDeviceSDAvailableSpace(Integer deviceSDAvailableSpace) {
this.deviceSDAvailableSpace = deviceSDAvailableSpace;
}
public String getIpAddress() {
return ipAddress;
}
public void setIpAddress(String ipAddress) {
this.ipAddress = ipAddress;
}
public String getLocationAPI() {
return locationAPI;
}
public void setLocationAPI(String locationAPI) {
this.locationAPI = locationAPI;
}
public String getLocationAPIAccuracy() {
return locationAPIAccuracy;
}
public void setLocationAPIAccuracy(String locationAPIAccuracy) {
this.locationAPIAccuracy = locationAPIAccuracy;
}
}
包含
的控制器@RequestMapping(value="/login", method=RequestMethod.POST,
consumes="application/json", produces="application/json"
)
public @ResponseBody MobileUserLoginResponse authenticateUser(@RequestBody final MobileUserLoginRequest loginRequest,HttpServletRequest req, HttpServletResponse res, HttpSession session) {
try {
MobileUserLoginResponse loginResponse = mobileAppLoginManager.authenticateUser(loginRequest);
return loginResponse;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
答案 0 :(得分:0)
在声明消耗和生成时使用枚举,MediaType.APPLICATION_JSON_VALUE。