Angularjs,Spring MVC和@ModelAttribute = null值

时间:2013-10-12 10:08:35

标签: spring angularjs spring-mvc modelattribute

我正在开发一个应用程序,其中前端使用Angularjs,后端使用Spring MVC公开REST服务。我试图在AngularJS中使用$ resource来保存一个新对象,但是当它调用我的Controller时为空(所有变量都为null)对象被传递。

这是我控制器中的方法:

@RequestMapping(value = "/api/instance", method = RequestMethod.POST)
    public @ResponseBody ActionResult createInstance(@ModelAttribute Instance instance) {
        logger.info(instance.toString());
        return ActionResult.SUCCESS;
    }

以下是Chrome报告发送的一些数据:

Request Method:POST    
Status Code:200 OK    
Request Headersview source    
Accept:application/json, text/plain, */*    
Accept-Encoding:gzip,deflate,sdch    
Accept-Language:sv-SE,sv;q=0.8,en-US;q=0.6,en;q=0.4    
Connection:keep-alive    
Content-Length:229    
Content-Type:application/json;charset=UTF-8
Request Payloadview parsed
{"id":"ffde5029-32b4-407e-b6ed-0192fcc994fe","count":0,"events":[],"status":"STOPPED","name":"name1","deviceProductInfo":{"manufacturer":"ACME","model":"Analyzer 2000"},"enabled":false,"type":"ARCH2000","timeForLastActivity":"-"}

当我在控制器中记录实例对象时,alls字段为空:

Instance{count=0, id='null', events=[], status='null', name='null', deviceProductInfo=null, enabled=false}

我错过了什么或为什么我的Instance对象没有填充JSON数据被发送?非常感谢!

编辑:这是我的实例级:

import com.fasterxml.jackson.annotation.JsonProperty;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;


@SuppressWarnings("UnusedDeclaration")
public class Instance {

    private int count;
    private String id;
    private List<Event> events = new ArrayList<Event>();
    private String status;
    private String name;
    private DeviceProductInfo deviceProductInfo;
    private boolean enabled;

    public Instance() {

    }

    public Instance(@JsonProperty("id") String id, String name, String status) {
        this.id = id;
        this.status = status;
        this.name = name;
    }

    public DeviceProductInfo getDeviceProductInfo() {
        return deviceProductInfo;
    }

    public void setDeviceProductInfo(DeviceProductInfo deviceProductInfo) {
        this.deviceProductInfo = deviceProductInfo;
    }

    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getType() {
        return "ARCH2000";
    }

    public List<Event> getEvents() {
        return events;
    }

    @Override
    public String toString() {
        return "Instance{" +
                "count=" + count +
                ", id='" + id + '\'' +
                ", events=" + events +
                ", status='" + status + '\'' +
                ", name='" + name + '\'' +
                ", deviceProductInfo=" + deviceProductInfo +
                ", enabled=" + enabled +
                '}';
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public String getTimeForLastActivity() {
        if (CollectionUtils.isEmpty(this.events)) {
            return "-";
        } else {


            DateTime dateForLastEvent = events.get(events.size() - 1).getOccured();
            DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm");

            return dateTimeFormatter.print(dateForLastEvent);
        }
    }
}

2 个答案:

答案 0 :(得分:1)

请尝试使用@RequestBody替换@ModelAttribute。它对我有用。

1. The user can directly input in the text box
2. Only number can be entered 
3. only the number position can be changed.

答案 1 :(得分:0)

尝试显式添加JSON作为要使用的类型:

@RequestMapping(value = "/api/instance", method = RequestMethod.POST, consumes="application/json")