我收到400 Bad Request但我不明白为什么。对我来说似乎一切都很好。我会很感激一些想法......
@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;
}
}
答案 0 :(得分:0)
服务器端似乎没问题。
我写了几行代码: 我写了一个简单的课堂测试
@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;
}
}