Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs、Spring MVC和@ModelAttribute=null值_Spring_Angularjs_Spring Mvc_Modelattribute - Fatal编程技术网

Angularjs、Spring MVC和@ModelAttribute=null值

Angularjs、Spring MVC和@ModelAttribute=null值,spring,angularjs,spring-mvc,modelattribute,Spring,Angularjs,Spring Mvc,Modelattribute,我正在开发一个应用程序,其中前端使用Angularjs,后端使用SpringMVC公开REST服务。我试图在AngularJS中使用$resource来保存一个新对象,但当它调用我的控制器时,会传递一个空的(所有变量都为null)对象 这是我在控制器中的方法: @RequestMapping(value = "/api/instance", method = RequestMethod.POST) public @ResponseBody ActionResult createInsta

我正在开发一个应用程序,其中前端使用Angularjs,后端使用SpringMVC公开REST服务。我试图在AngularJS中使用$resource来保存一个新对象,但当它调用我的控制器时,会传递一个空的(所有变量都为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":"-"}
在控制器中记录实例对象时,所有字段均为空:

Instance{count=0, id='null', events=[], status='null', name='null', deviceProductInfo=null, enabled=false}
我是否遗漏了什么,或者为什么我的实例对象中并没有填充正在发送的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);
        }
    }
}
import com.fasterxml.jackson.annotation.JsonProperty;
导入org.joda.time.DateTime;
导入org.joda.time.format.DateTimeFormat;
导入org.joda.time.format.DateTimeFormatter;
导入org.springframework.util.CollectionUtils;
导入java.util.ArrayList;
导入java.util.List;
@抑制警告(“未使用的声明”)
公共类实例{
私人整数计数;
私有字符串id;
私有列表事件=新的ArrayList();
私有字符串状态;
私有字符串名称;
专用设备ProductInfo设备ProductInfo;
启用私有布尔值;
公共实例(){
}
公共实例(@JsonProperty(“id”)字符串id、字符串名称、字符串状态){
this.id=id;
这个状态=状态;
this.name=名称;
}
公共设备产品信息getDeviceProductInfo(){
返回deviceProductInfo;
}
公共无效设置DeviceProductInfo(DeviceProductInfo设备ProductInfo){
this.deviceProductInfo=deviceProductInfo;
}
公共布尔值isEnabled(){
返回启用;
}
已启用公共void集(已启用布尔值){
this.enabled=已启用;
}
公共字符串getStatus(){
返回状态;
}
公共无效设置状态(字符串状态){
这个状态=状态;
}
公共字符串getId(){
返回id;
}
公共无效集合id(字符串id){
this.id=id;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共字符串getType(){
返回“ARCH2000”;
}
公共列表getEvents(){
返回事件;
}
@凌驾
公共字符串toString(){
返回“实例{”+
“count=“+count+
“,id=”+id+“\”+
“,events=“+事件”+
,状态=“+”状态+“\”+
“,name=”“+name+”\“””+
“,deviceProductInfo=“+deviceProductInfo+
“,enabled=“+enabled”+
'}';
}
public int getCount(){
返回计数;
}
公共无效集合计数(整数计数){
this.count=计数;
}
公共字符串getTimeForLastActivity(){
if(CollectionUtils.isEmpty(this.events)){
返回“-”;
}否则{
DateTime dateForLastEvent=events.get(events.size()-1.getOccurd();
DateTimeFormatter DateTimeFormatter=DateTimeFormat.forPattern(“yyyy-MM-dd-HH:MM”);
返回日期TimeFormatter.print(dateForLastEvent);
}
}
}

尝试明确添加JSON作为要使用的类型:

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

尝试明确添加JSON作为要使用的类型:

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

请尝试用@RequestBody替换@ModelAttribute。这对我很管用

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

        System.out.println("");
    }.

请尝试用@RequestBody替换@ModelAttribute。这对我很管用

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

        System.out.println("");
    }.

请发布你的
实例
类。我用我的实例类更新了我的问题。不要使用
@modeldattribute
,而是使用
@RequestBody
。M.Deinum建议的解决方案有效。请发布你的
实例
类。我用我的实例类更新了我的问题。不要使用
@modeldattribute
,改为使用
@RequestBody
。M.Deinum建议的解决方案有效。感谢您的输入,但不幸的是,这没有帮助。我的控制器中的值仍然为空。感谢您的输入,但不幸的是,这没有帮助。这些值在我的控制器中仍然为空。