Spring GET类型的RESt服务的DTO映射
在Spring BOOT应用程序中开发REST服务 这是输入jsonSpring GET类型的RESt服务的DTO映射,spring,spring-boot,Spring,Spring Boot,在Spring BOOT应用程序中开发REST服务 这是输入json { "rcode": 122, "ecode": [11, 12] } 控制器代码 @RequestMapping("/getPersonDTOList") public List < PersonDTO > getPersonDTOList( @RequestParam(value = "personDTO") String personDTO){ //how
{
"rcode": 122,
"ecode": [11, 12]
}
控制器代码
@RequestMapping("/getPersonDTOList")
public List < PersonDTO > getPersonDTOList(
@RequestParam(value = "personDTO") String personDTO){
//how can I map this to DTO
}
//DTO
如何在spring BOOT中将此字符串映射到DTO,因为这是GET,我们需要手动执行。GET方法不携带任何正文数据
你可以:
将API更改为@RequestMapping默认支持的POST方法
我不是很有经验,但如果您使用@RequestMapping,我认为您还必须设置一个类型:
@RequestMapping("/getPersonDTOList", method = RequestMethod.POST ) // or GET...
您还可以使用@PostMapping、@GetMapping等
@PostMapping("/getPersonDTOList")
public PersonDTO getPersonDTOList(@RequestBody PersonDTO person){
return person;
}
我认为这只适用于@RestController为什么不是POST?@SudhirOjha可能是因为请求没有改变服务器上的任何东西,即数据库。您如何将json传递给rest服务?@Abhijeet我也可以将其作为请求参数传递,不必作为请求主体发送。
@RequestMapping("/getPersonDTOList")
public List < PersonDTO > getPersonDTOList(@RequestParam("rcode") int rcode, @RequestParam("ecode") List<Integer> ecode )
@RequestMapping("/getPersonDTOList", method = RequestMethod.POST ) // or GET...
@PostMapping("/getPersonDTOList")
public PersonDTO getPersonDTOList(@RequestBody PersonDTO person){
return person;
}