関連クラスとJSONリクエストを見つけてください。私のリクエストに何が間違っているか分からない場合は、400のBADリクエストを取得します。おそらく要求はサーバーに届いていないかもしれません。私は失われたものが何かを持っていない。問題がへのペイロードJSONのマッピングであると思われるこのHTTP 400 Rest WebService POSTメソッドを呼び出すときのリクエストが不正です
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;
}
}
スタックオーバーフローへようこそ。あなたのコードを掲載していますが、このコードはどのように展開されていますか?どのようにこのコードを実際にリクエストしていますか?このコードに対するクライアント(呼び出しを行う)をデバッグモードで実行できるので、できるだけ多くの情報を表示できますか?あなたの答えと一緒にそれらのエラーを貼り付けてください。あなたの説明がより詳細になればなるほど、私たちはあなたに尋ねなければならない質問が少なくなります。 –
コードはTomat 8にデプロイされ、例外やエラーは発生しません。私は、SOAPUIクライアントに似たPostman Restful Webサービスクライアントを使用しています。私はデータを追加するPOST要求をしています。そして何とか私は上記のJSONペイロードを使って400 Bad Requestを取得します。 –
クールで良い情報。 Tomcatからのログがありますか? JSONはhttp://jsonlint.com/によると有効なjsonですので、そうではありません。 'curl'などのような別のRESTクライアントで同じデータを投稿しようとしましたか?あなたはどんなURLに投稿していますか? –