[英]Spring Rest 400 Bad Request
我收到400錯誤請求,但我不知道為什么。 對我來說似乎一切都很好。 我將不勝感激。
@Component
public class TicketIdentifierRestClient implements TicketIdentifierService{
@Autowired
private RestClient restClient;
@Override
public List<TicketIdentifierDto> createTicketIdentifier(List<TicketIdentifierDto> list)
throws ServiceException {
RestTemplate restTemplate = this.restClient.getRestTemplate();
String url = this.restClient.createServiceUrl("/ticketIdentifier/");
HttpHeaders headers = this.restClient.getHttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON))
;
HttpEntity<String> entity = new HttpEntity<String>(headers);
List<TicketIdentifierDto> tickets = null;
// System.out.println("necati"+list.get(0).toString());
try {
ParameterizedTypeReference<List<TicketIdentifierDto>> ref = new ParameterizedTypeReference<List<TicketIdentifierDto>>() {};
ResponseEntity<List<TicketIdentifierDto>> response = restTemplate.exchange(URI.create(url), HttpMethod.POST, entity,ref);
tickets = response.getBody();
return tickets;
} catch (RestClientException e) {
e.printStackTrace();
throw new ServiceException("Could not retrieve tickets: " + e.getMessage(), e);
}
}
和服務器端即時消息只是試圖獲取列表並在此處打印出來
@RestController
@RequestMapping(value = "/ticketIdentifier")
public class TicketIdentifierController {
private final Logger LOG = Logger.getLogger(TicketController.class);
@Autowired
private TicketIdentifierService tiService;
@RequestMapping(method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
public @ResponseBody List<TicketIdentifierDto> getTickets(@RequestBody List<TicketIdentifierDto> list) throws ServiceException {
System.out.println("did i get it ?"+list.get(0).toString());
//return EntityToDto.convertTicketIdentifiers(tiService.create(list));
return list;
}
}
服務器端似乎還可以。
我編寫了以下幾行代碼:我編寫了一個簡單的類測試
@JsonIgnoreProperties(ignoreUnknown = true)
public class Test {
String text;
int number;
public int getNumber() {
return number;
}
public String getText() {
return text;
}
public Test(String text, int number) {
super();
this.text = text;
this.number = number;
}
public Test() {
super();
}
}
和一個簡單的客戶
public class Application {
public static void main(String args[]) {
RestTemplate restTemplate = new RestTemplate();
List<Test> list = new ArrayList<Test>();
Test request = new Test();
request.number = 1;
request.text = "txt1";
Test request2 = new Test();
request2.number = 2;
request2.text = "txt2";
list.add(request);
list.add(request2);
@SuppressWarnings("rawtypes")
ResponseEntity<ArrayList> test = restTemplate.postForEntity("http://localhost:8080/postevent", list, ArrayList.class);
System.out.println(test.getBody().toString());//show json
for(int i =0; i<list.size();i++){
System.out.println("number "+list.get(i).number +" text "+ list.get(i).text);
}
}
}
服務器端:
@RestController
@RequestMapping(value = "/postevent")
public class PostEvent {
@RequestMapping(method = RequestMethod.POST, consumes= "application/json", produces = "application/json")
public @ResponseBody List<Test> getTickets(@RequestBody List<Test> list) {
for(int i =0; i<list.size();i++){
System.out.println("number "+list.get(i).number +" text "+ list.get(i).text);
}
return list;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.