如何传递消息以及RESTful请求?

时间:2016-02-11 12:43:16

标签: java spring rest

我有这个班级DemoController。在这个类中,我想发送一条消息以及REST请求。

我该怎么发送?假设M发送http://localhost:8080/sendmessage...

如何与此请求一起发送消息?

@RestController
@EnableAutoConfiguration
public class DemoController {

    @Autowired
    DemoPublisher demopublisher;

    @Autowired
    DemoConsumer democonsumer;

    @RequestMapping(value="/sendmessage", method= {RequestMethod.POST})
    @ResponseBody
    public String messageSenderController(@RequestParam String message, Model model){
        try {
            demopublisher.demoPublishMessage(message);
        } catch (JMSException e) {
            e.printStackTrace();
        }
        return message;
    }
}

1 个答案:

答案 0 :(得分:0)

QueryParam

url:/ sendMessage?msg = HelloWorld!

@RequestMapping(value="/sendmessage",method= {RequestMethod.POST})
@ResponseBody
public String messageSenderController(@QueryParam("msg") String message,Model model){
}

UrlParam

url:/ sendMessage / HelloWorld!

@RequestMapping(value="/sendmessage/{message}",method= {RequestMethod.POST})
@ResponseBody
public String messageSenderController(@PathVariable String message,Model model){
}

当您将数据发布到服务器时,您也可以在body参数中发送数据。如果您有要发送到服务器的表单或其他数据,我建议您使用此功能。

RequestBody

url:/ sendMessage

正文(邮递员或其他其他客户端的RAW,接受需要申请/ xml):

{
    "my message"
}

控制器

@RequestMapping(value="/sendmessage",method= {RequestMethod.POST})
@ResponseBody
public String messageSenderController(@RequestBody String message,Model model){
}