0

I have this class DemoController. In this class I want to send a message along with REST request.

How can I send it? Suppose M sending http://localhost:8080/sendmessage...

How can I send a message along with this request?

@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;
    }
}
cassiomolin
  • 124,154
  • 35
  • 280
  • 359
Priyanka Taneja
  • 157
  • 2
  • 3
  • 13
  • you can pass data to a rest controller with either QueryParam, PathVariable or RequestBody. Do you want the message to be a part of the url or not? – R. Gulbrandsen Feb 11 '16 at 12:51
  • As I am using Spring boot application so this is my main class from here how can i send message to controller along with rest request. public static void main(String[] args) { SpringApplication.run(MessagingApplication.class, args); } – Priyanka Taneja Feb 11 '16 at 12:53

1 Answers1

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){
}

When you are posting data to the server, you can also send data in the body parameter too. I recommend you use this for when you have a form or other data you want to send to the server.


RequestBody

url: /sendMessage

body (RAW in postman or other rest client, accept need to be application/xml):

{
    "my message"
}

controller

@RequestMapping(value="/sendmessage",method= {RequestMethod.POST})
@ResponseBody
public String messageSenderController(@RequestBody String message,Model model){
}
R. Gulbrandsen
  • 3,648
  • 1
  • 22
  • 35
  • see this message is going to Postman now if I am making changes to Postman console i want to edit that message manually but again it is showing the previous message but i want the updated one. How can we do that – Priyanka Taneja Feb 11 '16 at 13:09
  • make sure postman dont cache the last url. bit hard to help without more information – R. Gulbrandsen Feb 11 '16 at 13:11
  • I am sending message in URL localhost//8080/sendmessage/welcome this welcome is going to postman now in postman we have an option RAW where i can edit it manually so i want this new message to store into activeMQ. but it is taking the one which we are sending in the URL – Priyanka Taneja Feb 11 '16 at 13:14
  • 1. confirm url localhost:8080/sendMessage/welcome ? 2. So you are sending in the message in the body then? { "my message" } under RAW? – R. Gulbrandsen Feb 11 '16 at 13:18
  • yes m sending in the body it is coming in RAW and saving it to Active MQ now i want to edit this one from RAW in POSTMAN and want the new one to save into my activeMQ – Priyanka Taneja Feb 11 '16 at 13:31
  • well. thats a @RequestBody http://stackoverflow.com/questions/11291933/requestbody-and-responsebody-spring – R. Gulbrandsen Feb 11 '16 at 13:34
  • Is there any way too send string message from spring boot application main class public static void main(String[] args) { SpringApplication.run(MessagingApplication.class, args); } – Priyanka Taneja Feb 11 '16 at 13:40
  • you are mixing up a lot of different names and conscepts.. why do you want to pas a string value from the application starter to a rest controller? for integration tests? – R. Gulbrandsen Feb 11 '16 at 13:52
  • see my updated answer for how to pas objects to the controller, i guess this is what you actually need – R. Gulbrandsen Feb 11 '16 at 14:00
  • the requirement is if we make changes to raw in postman it should get updated in activeMQ queue – Priyanka Taneja Feb 11 '16 at 14:01
  • you cant change data that is already on the queue. you can pas a new message on the queue though – R. Gulbrandsen Feb 11 '16 at 14:05
  • yes how can i, if I am updating in RAW Postman console and want this new one to add into queue – Priyanka Taneja Feb 11 '16 at 14:06