2

Please find the relevant classes and JSON request, I fail to understand what is wrong with my request, I get 400 BAD Request. The request perhaps is not even reaching the server. I dont have a clue whats missing. Appreciate your help on this

Json request payload

{
  "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
}

Campaigns model class

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 class

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

}
Azim
  • 1,043
  • 13
  • 27
Sri Sris
  • 2,001
  • 2
  • 11
  • 10
  • welcome to stack overflow. you'd done a great job listing your code, but how is this code deployed? how are you making your actual request to this code? can your client (making the call) against this code be run in debug mode so we can see as much info as possible? paste those errors in along with your answer. the more detailed your explanations, the less questions we have to ask you. – matias elgart Nov 12 '16 at 18:51
  • Code deployed in tomat 8 and no exceptions or errors thrown at. I am using Postman Restful webservice client similar to SOAPUI client. I am making a POST request to add data. And somehow with the JSON payload I added above I get 400 Bad Request. – Sri Sris Nov 12 '16 at 18:53
  • cool, good info. do you have logs from Tomcat to show? your JSON is valid json, according to http://jsonlint.com/, so not that. have you tried to post the same data with a different REST client, like 'curl' or similar? exactly what URL are you POSTing to? – matias elgart Nov 12 '16 at 19:02
  • Before & After the POST request https://paste.ee/p/UvFqI nothing changes – Sri Sris Nov 12 '16 at 19:07
  • What Content-Type header are you passing while making the call? It depends upon what Content-Type you defined as default. Just try adding one more header Content-Type as application/json. It would be helpful if you can share the controller code as well. – Azim Nov 12 '16 at 19:09
  • and what URL are you hitting? show us how you configured Postman to hit your service. the more info, the better. – matias elgart Nov 12 '16 at 19:09
  • And yeah I validated my JSON request in jsonlint.com and its valid – Sri Sris Nov 12 '16 at 19:09
  • URL - http://localhost:8080/listload/campaign/ content-type is application/json – Sri Sris Nov 12 '16 at 19:10
  • CampaignController is already added there at top in the main question – Sri Sris Nov 12 '16 at 19:13
  • Did you try putting debug point inside post method of the controller? – Azim Nov 12 '16 at 19:17
  • Yeah I did, its not even reaching to the Controller. Thats why I am unable to fix the issue :) HTTP 400 BAD request means it is bad syntax. But not sure why it is bad. – Sri Sris Nov 12 '16 at 19:19
  • Then, it seems issue comes while mapping json to Campaigns POJO during request mapping. – Azim Nov 12 '16 at 19:23
  • I added My Campaigns POJO and JSON request on top, do you see anything missing. I am going crazy over this issue :( – Sri Sris Nov 12 '16 at 19:25
  • Got closer now, something to do with the values I am passing there "endDateTime": "02/04/2017" – Sri Sris Nov 12 '16 at 19:51

1 Answers1

0

It seems issue is with the mapping of the payload json to the POJO Campaigns in the controller. I converted all the Date and LocalDate with String and then tried to map to POJO as below.

I slightly modified your Campaigns 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);
    }

I hope this will help you to resolve this problem.

Azim
  • 1,043
  • 13
  • 27
  • As of now, I am unsure, why its not working, but my answer gave a clue of what's going wrong. You can google further to resolve this issue. – Azim Nov 12 '16 at 20:49