调用Rest WebService POST方法时出现HTTP 400错误请求

时间:2016-11-12 18:42:21

标签: json spring spring-mvc bad-request

请找到相关的类和JSON请求,我无法理解我的请求有什么问题,我得到400 BAD请求。请求可能甚至没有到达服务器。我没有找到丢失的线索。感谢您对此的帮助

Json请求有效负载

{
  "campaignID": 1,
  "campaignDescription": "A Campaign #1",
  "campaignOption": "C",
  "countryID": "CI",
  "startDateTime": "02/04/2010",
  "endDateTime": "02/04/2017",
  "channelID": 3,
  "lastUpdateBy": "bbblsdcdo",
  "lastUpdateTime": "02/04/2017",
  "timestamp": "2016-11-10 23:26:10.285",
  "campaignIdCRM": null,
  "classificationCPC": null,
  "formCode": null
}

广告系列模型类

package com.srisris.listload.domain;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.annotations.Type;
import org.joda.time.LocalDate;
import org.springframework.format.annotation.DateTimeFormat;

@Entity
@Table(name = "Campaigns")
public class Campaigns {
    @Id
    @Column(name = "CampaignID", nullable = false)
    private Integer campaignID;

    @Column(name = "CampaignDesc", nullable = false)
    private String campaignDescription;

    @Column(name = "CampaignOption", nullable = false)
    private String campaignOption;

    @Column(name = "CountryID", nullable = false)
    private String countryID;

    @DateTimeFormat(pattern = "dd/MM/yyyy")
    @Column(name = "StartDateTime", nullable = false)
    @Type(type = "org.jadira.usertype.dateandtime.joda.PersistentLocalDate")
    private LocalDate startDateTime;
    // private Date startDateTime;

    @DateTimeFormat(pattern = "dd/MM/yyyy")
    @Column(name = "EndDateTime", nullable = false)
    @Type(type = "org.jadira.usertype.dateandtime.joda.PersistentLocalDate")
    private Date endDateTime;

    @Column(name = "ChannelID", nullable = false)
    private Integer channelID;

    @Column(name = "LastUpdateBy", nullable = false)
    private String lastUpdateBy;

    @DateTimeFormat(pattern = "dd/MM/yyyy")
    @Column(name = "LastUpdateTime", nullable = false)
    @Type(type = "org.jadira.usertype.dateandtime.joda.PersistentLocalDate")
    private Date lastUpdateTime;

    private String timestamp;

    @Column(name = "CampaignIdCRM", nullable = true)
    private String campaignIdCRM;

    @Column(name = "ClassificationCPC", nullable = true)
    private String classificationCPC;

    @Column(name = "FormCode", nullable = true)
    private String formCode;

    public Campaigns() {

    }

    public Campaigns(Integer campaignID, String campaignDescription, String campaignOption, String countryID,
            LocalDate startDateTime, Date endDateTime, Integer channelID, String lastUpdateBy, Date lastUpdateTime,
            String timestamp, String campaignIDCRM, String classificationCPC, String formCode) {
        super();
        this.campaignID = campaignID;
        this.campaignDescription = campaignDescription;
        this.campaignOption = campaignOption;
        this.countryID = countryID;
        this.startDateTime = startDateTime;
        this.endDateTime = endDateTime;
        this.channelID = channelID;
        this.lastUpdateBy = lastUpdateBy;
        this.lastUpdateTime = lastUpdateTime;
        this.timestamp = timestamp;
        this.campaignIdCRM = campaignIDCRM;
        this.classificationCPC = classificationCPC;
        this.formCode = formCode;
    }

    public Integer getCampaignID() {
        return campaignID;
    }

    public void setCampaignID(Integer campaignID) {
        this.campaignID = campaignID;
    }

    public String getCampaignDescription() {
        return campaignDescription;
    }

    public void setCampaignDescription(String campaignDescription) {
        this.campaignDescription = campaignDescription;
    }

    public String getCampaignOption() {
        return campaignOption;
    }

    public void setCampaignOption(String campaignOption) {
        this.campaignOption = campaignOption;
    }

    public String getCountryID() {
        return countryID;
    }

    public void setCountryID(String countryID) {
        this.countryID = countryID;
    }

    public LocalDate getStartDateTime() {
        return startDateTime;
    }

    public void setStartDateTime(LocalDate startDateTime) {
        this.startDateTime = startDateTime;
    }

    public Date getEndDateTime() {
        return endDateTime;
    }

    public void setEndDateTime(Date endDateTime) {
        this.endDateTime = endDateTime;
    }

    public Integer getChannelID() {
        return channelID;
    }

    public void setChannelID(Integer channelID) {
        this.channelID = channelID;
    }

    public String getLastUpdateBy() {
        return lastUpdateBy;
    }

    public void setLastUpdateBy(String lastUpdateBy) {
        this.lastUpdateBy = lastUpdateBy;
    }

    public Date getLastUpdateTime() {
        return lastUpdateTime;
    }

    public void setLastUpdateTime(Date lastUpdateTime) {
        this.lastUpdateTime = lastUpdateTime;
    }

    public String getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(String timestamp) {
        this.timestamp = timestamp;
    }

    public String getCampaignIDCRM() {
        return campaignIdCRM;
    }

    public void setCampaignIDCRM(String campaignIDCRM) {
        this.campaignIdCRM = campaignIDCRM;
    }

    public String getClassificationCPC() {
        return classificationCPC;
    }

    public void setClassificationCPC(String classificationCPC) {
        this.classificationCPC = classificationCPC;
    }

    public String getFormCode() {
        return formCode;
    }

    public void setFormCode(String formCode) {
        this.formCode = formCode;
    }

}

CampaignController类

    package com.srisris.listload.controller;

    import java.util.List;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.http.HttpHeaders;
    import org.springframework.http.HttpStatus;
    import org.springframework.http.MediaType;
    import org.springframework.http.ResponseEntity;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.util.UriComponentsBuilder;

    import com.srisris.listload.domain.Campaigns;
    import com.srisris.listload.service.CampaignService;

    @RestController
    public class CampaignController {

        @Autowired
        CampaignService campaignService;

        @RequestMapping(value = "/campaign/", method = RequestMethod.GET)
        public ResponseEntity<List<Campaigns>> listAllCampaigns() {
            List<Campaigns> campaigns = campaignService.findAllCampaigns();
            if(campaigns.isEmpty()){
                return new ResponseEntity<List<Campaigns>>(HttpStatus.NO_CONTENT);//You many decide to return HttpStatus.NOT_FOUND
            }
            return new ResponseEntity<List<Campaigns>>(campaigns, HttpStatus.OK);
        }

        @RequestMapping(value = "/campaign/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
        public ResponseEntity<Campaigns> getCampaign(@PathVariable("id") Integer id) {
             Campaigns campaign = campaignService.findById(id);
                if (campaign == null) {
                    System.out.println("User with id " + id + " not found");
                    return new ResponseEntity<Campaigns>(HttpStatus.NOT_FOUND);
                }
                return new ResponseEntity<Campaigns>(campaign, HttpStatus.OK);
        }

        @RequestMapping(value = "/campaign/", method = RequestMethod.POST)
        public ResponseEntity<Void> createCampaign(@RequestBody Campaigns campaign, UriComponentsBuilder ucBuilder) {
             if (campaignService.isCampaignExist(campaign)) {
                    System.out.println("A Campaign with ID " + campaign.getCampaignID() + " already exist");
                    return new ResponseEntity<Void>(HttpStatus.CONFLICT);
                }

             campaignService.saveCampaign(campaign);

                HttpHeaders headers = new HttpHeaders();
                headers.setLocation(ucBuilder.path("/campaign/{id}").buildAndExpand(campaign.getCampaignID()).toUri());
                return new ResponseEntity<Void>(headers, HttpStatus.CREATED);
        }

        @RequestMapping(value = "/campaign/{id}", method = RequestMethod.PUT)
        public ResponseEntity<Campaigns> updateCampaign(@PathVariable("id") long id, @RequestBody Campaigns campaign) {
            return null;

        }

        @RequestMapping(value = "/campaign/{id}", method = RequestMethod.DELETE)
        public ResponseEntity<Campaigns> deleteCampaign(@PathVariable("id") long id) {
            return null;
        }

    }


Campaign Service Implementation Class


package com.srisris.listload.service;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.srisris.listload.dao.CampaignDao;
import com.srisris.listload.domain.Campaigns;

@Service("campaignService")
@Transactional
public class CampaignServiceImpl implements CampaignService {

    @Autowired
    private CampaignDao campaignDao;

    private static List<Campaigns> campaigns = new ArrayList<Campaigns>();

//  static {
//      campaigns = populateDummyCampaigns();
//  }

    @Override
    public List<Campaigns> findAllCampaigns() {
        return campaignDao.listAllCampaigns();
    }

    @Override
    public Campaigns findById(Integer id) {

        return campaignDao.getCampaign(id);
    }

    @Override
    public void saveCampaign(Campaigns campaign) {
        campaignDao.createCampaign(campaign);

    }

    @Override
    public void updateCampaign(Campaigns campaign) {
        int index = campaigns.indexOf(campaign);
        campaigns.set(index, campaign);

    }

    @Override
    public void deleteCampaign(Integer campaignID) {
        for (Iterator<Campaigns> iterator = campaigns.iterator(); iterator.hasNext();) {
            Campaigns campaign = iterator.next();
            if (campaign.getCampaignID() == campaignID) {
                iterator.remove();
            }
        }

    }


    public Boolean isCampaignExist(Campaigns campaign) {
        return findById(campaign.getCampaignID())!=null;
    }

}

1 个答案:

答案 0 :(得分:0)

似乎问题在于有效负载json到控制器中的POJO广告系列的映射。我使用String转换了所有Date和LocalDate,然后尝试映射到POJO,如下所示。

我稍微修改了您的广告系列 POJO:

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

import org.springframework.format.annotation.DateTimeFormat;

@Entity
@Table(name = "Campaigns")
public class Campaigns {
    @Id
    @Column(name = "CampaignID", nullable = false)
    private Integer campaignID;

    @Column(name = "CampaignDesc", nullable = false)
    private String campaignDescription;

    @Column(name = "CampaignOption", nullable = false)
    private String campaignOption;

    @Column(name = "CountryID", nullable = false)
    private String countryID;

    @DateTimeFormat(pattern = "dd/MM/yyyy")
    @Column(name = "StartDateTime", nullable = false)
    private String startDateTime;

    @DateTimeFormat(pattern = "dd/MM/yyyy")
    @Column(name = "EndDateTime", nullable = false)
    private String endDateTime;

    @Column(name = "ChannelID", nullable = false)
    private Integer channelID;

    @Column(name = "LastUpdateBy", nullable = false)
    private String lastUpdateBy;

    @DateTimeFormat(pattern = "dd/MM/yyyy")
    @Column(name = "LastUpdateTime", nullable = false)
    private String lastUpdateTime;

    private String timestamp;

    @Column(name = "CampaignIdCRM", nullable = true)
    private String campaignIdCRM;

    @Column(name = "ClassificationCPC", nullable = true)
    private String classificationCPC;

    @Column(name = "FormCode", nullable = true)
    private String formCode;

    public Campaigns() {

    }

    public Campaigns(Integer campaignID, String campaignDescription, String campaignOption, String countryID, String startDateTime, String endDateTime,
            Integer channelID, String lastUpdateBy, String lastUpdateTime, String timestamp, String campaignIDCRM, String classificationCPC, String formCode) {
        super();
        this.campaignID = campaignID;
        this.campaignDescription = campaignDescription;
        this.campaignOption = campaignOption;
        this.countryID = countryID;
        this.startDateTime = startDateTime;
        this.endDateTime = endDateTime;
        this.channelID = channelID;
        this.lastUpdateBy = lastUpdateBy;
        this.lastUpdateTime = lastUpdateTime;
        this.timestamp = timestamp;
        this.campaignIdCRM = campaignIDCRM;
        this.classificationCPC = classificationCPC;
        this.formCode = formCode;
    }

    public Integer getCampaignID() {
        return campaignID;
    }

    public void setCampaignID(Integer campaignID) {
        this.campaignID = campaignID;
    }

    public String getCampaignDescription() {
        return campaignDescription;
    }

    public void setCampaignDescription(String campaignDescription) {
        this.campaignDescription = campaignDescription;
    }

    public String getCampaignOption() {
        return campaignOption;
    }

    public void setCampaignOption(String campaignOption) {
        this.campaignOption = campaignOption;
    }

    public String getCountryID() {
        return countryID;
    }

    public void setCountryID(String countryID) {
        this.countryID = countryID;
    }

    public String getStartDateTime() {
        return startDateTime;
    }

    public void setStartDateTime(String startDateTime) {
        this.startDateTime = startDateTime;
    }

    public String getEndDateTime() {
        return endDateTime;
    }

    public void setEndDateTime(String endDateTime) {
        this.endDateTime = endDateTime;
    }

    public Integer getChannelID() {
        return channelID;
    }

    public void setChannelID(Integer channelID) {
        this.channelID = channelID;
    }

    public String getLastUpdateBy() {
        return lastUpdateBy;
    }

    public void setLastUpdateBy(String lastUpdateBy) {
        this.lastUpdateBy = lastUpdateBy;
    }

    public String getLastUpdateTime() {
        return lastUpdateTime;
    }

    public void setLastUpdateTime(String lastUpdateTime) {
        this.lastUpdateTime = lastUpdateTime;
    }

    public String getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(String timestamp) {
        this.timestamp = timestamp;
    }

    public String getCampaignIDCRM() {
        return campaignIdCRM;
    }

    public void setCampaignIDCRM(String campaignIDCRM) {
        this.campaignIdCRM = campaignIDCRM;
    }

    public String getClassificationCPC() {
        return classificationCPC;
    }

    public void setClassificationCPC(String classificationCPC) {
        this.classificationCPC = classificationCPC;
    }

    public String getFormCode() {
        return formCode;
    }

    public void setFormCode(String formCode) {
        this.formCode = formCode;
    }

    @Override
    public String toString() {
        return "CampaignId: " + this.campaignID;
    }
}

<强> Driver.java

public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
            String str = "{\"campaignID\":1,\"campaignDescription\":\"A Campaign #1\",\"campaignOption\":\"C\",\"countryID\":\"CI\",\"startDateTime\":\"02/04/2010\",\"endDateTime\":\"02/04/2017\",\"channelID\":3,\"lastUpdateBy\":\"bbblsdcdo\",\"lastUpdateTime\":\"02/04/2017\",\"timestamp\":\"2016-11-10 23:26:10.285\",\"campaignIdCRM\":null,\"classificationCPC\":null,\"formCode\":null}";
            ObjectMapper mapper = new ObjectMapper();
            mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            Campaigns obj = mapper.readValue(str, Campaigns.class);
            System.out.println(obj);
    }

我希望这可以帮助您解决此问题。