1

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

这是我在控制器中的方法:

@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":"-"}

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

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);
        }
    }
}
4

2 回答 2

1

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

  @RequestMapping(value = "/", method = RequestMethod.POST)
  public @ResponseBody void add****(@RequestBody Good good) {

        System.out.println("");
    }.
于 2016-07-28T02:00:34.173 回答
0

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

@RequestMapping(value = "/api/instance", method = RequestMethod.POST, consumes="application/json")
于 2013-10-12T16:11:31.987 回答