我有一个Spring RestController,
@RestController
public class TestController {
@RequestMapping("/checkTestObject")
public MyPOJOObject3 checkTestObject(MyPOJOObject1 obj1, MyPOJOObject2 obj2) {
//Using obj1 and obj2 - business logic
return new MyPOJOObject3();
}
}
MyPOJOObject1,MyPOJOObject2和MyPOJOObject3是我用setter和getter字段定义的3个自定义对象。我尝试过的客户,
public class TestClient(
public static void main(String args[]){
MyPOJOObject1 obj1 = new MyPOJOObject1();
obj1.setInfoChgDate(new Date());
obj1.setInfoRegUserName("NEW_USER");
MyPOJOObject2 obj2 = new MyPOJOObject2();
obj2.setId(121l);
obj2.setDescription("Hello TestDemo");
String url = "http://localhost:8080/TestApp/checkTestObject";
MultiValueMap<String, Object> formData = new LinkedMultiValueMap<String, Object>();
formData.set("obj1", obj1);
formData.set("obj2", obj2);
// Prepare acceptable media type
List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
acceptableMediaTypes.add(MediaType.ALL);
HttpHeaders headers = new HttpHeaders();
headers.setAccept(acceptableMediaTypes);
HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(formData, headers);
ResponseEntity<MyPOJOObject3> response = null;
try {
response = restTemplate.exchange(url, HttpMethod.POST, httpEntity, MyPOJOObject3.class);
logger.debug(url + " :: response :: " + response.toString());
} catch (Exception exception) {
logger.error(url + " :: Exception thrown :: ", exception);
}
//response.getBody();
}
)
当我在tomcat上部署RestController并运行客户端时,我得到错误,
2015-07-27 14:54:47,230 [main] ERROR TestController - http://localhost:8080/TestApp/checkTestObject :: Exception thrown ::
org.springframework.web.client.HttpClientErrorException: 405 Method Not Allowed
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91)
at org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.java:607)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:565)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:521)
我的问题是,这是使用Spring通过REST发送和接收自定义对象的正确方法还是有其他方法吗?我尝试使用其他RestTemplate方法,如getForObject,postForObject,postForLocation等,但没有运气。如果有通过JSON通过REST发送接收复杂对象的标准方法,请告诉我,或者我必须采取类似的方式 - 将对象转换为JSON字符串 - 发送 - 将JSON字符串转换回Object,反之亦然。请让我知道这些选项,因为我对我的工作感到震惊
由于
答案 0 :(得分:1)
405 Method Not Allowed
这意味着您正在尝试POST
到非POST
的方法。 @RequestMapping
默认方法为GET
。将您的Controller代码更改为以下内容。
<强>更新强>
@RestController
public class TestController {
@RequestMapping(value="/checkTestObject", method=RequestMethod.POST)
public MyPOJOObject3 checkTestObject(@RequestParam MyPOJOObject1 obj1, @RequestParam MyPOJOObject2 obj2) {
//Using obj1 and obj2 - business logic
return new MyPOJOObject3();
}
}
要修复400 Bad Request错误,您需要在obj1和obj2之前添加@RequestParam。因为您将这些作为formdata的一部分发送。请参阅上面的代码。
答案 1 :(得分:0)
使用@RequestBody
:
@RestController 公共类TestController {
@RequestMapping(value="/checkTestObject", method=RequestMethod.POST)
public MyPOJOObject3 checkTestObject(@RequestBody MyPOJOObject1 obj1, @RequestBody MyPOJOObject2 obj2) {
//Using obj1 and obj2 - business logic
return new MyPOJOObject3();
}
}