I want to send clickable URL from java code to UI where it return type initially was String
#POST
#Path("/crd")
#Produces(MediaType.TEXT_PLAIN)
#Consumes(MediaType.APPLICATION_JSON)
public String isValid(SomeDTO SomeDTO)
throws Exception {
// business logic
catch(Exceptioin e){
return "notvalid"
}
}
Now i want send to ui url along with text(like notvalid.Click below link to
user guide)
#POST
#Path("/crd")
#Produces(MediaType.TEXT_PLAIN)
#Consumes(MediaType.APPLICATION_JSON)
public String isValid(SomeDTO SomeDTO)
throws Exception {
// business logic
catch(Exceptioin e){
return "notvalid"+url
}
}
before i was notvalid
---expected is notvalid.Cilck below link to user guide.
return "notvalid"+"Click here for more info";
Related
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;
}
}
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){
}
How do I access the parameters sent in a jQuery $.get request?
$(document).ready(function () {
var input1 = "name has been sent";
$("#btn1").click(function(){
$.get("http://localhost:8080/RestHelloWorld/rest/message/hello", {
name: input1
});
});
Java:
#GET
#Path("/hello")
public String printMessage(#FormParam("name") String n){
System.out.println(n);
return "helloWorld";
}
The connection is fine as I am printing null to the console. How do I access the data sent within the HTTP request? I'm thinking the #FormParam is not the correct way to reference the data.
Found the correct command - (#QueryParam instead of (#FormParam
#GET
#Path("/hello")
public String printMessage(#QueryParam("name") String n){
System.out.println(n);
return "helloWorld number 2";
}
You can use #RequestParam for parameters;
#GET
#Path("/hello")
public String printMessage(#RequestParam("name") String n){
System.out.println(n);
return "helloWorld";
}
Or HttpServletRequest;
#GET
#Path("/hello")
public String printMessage(HttpServletRequest request){
System.out.println(request.getParameter("name"));
return "helloWorld";
}
I am new to web service in jersey. I have created a web service the code is below
#Path("/remotedb")
public class RemoteDb {
#GET
#Path("/save/{xmlData}")
#Produces(MediaType.TEXT_XML)
public String saveData(#PathParam("xmlData") String xml) {
return xml;
}
}
I have this code at client side
public class WebServiceClient {
public static void callWebService() {
String xml = "<data>" +
"<table><test_id>t4</test_id><dateprix>2013-06-06 22:50:40.252</dateprix><nomtest>NOMTEST</nomtest><prixtest>12.70</prixtest><webposted>N</webposted><posteddate>2013-06-06 21:51:42.252</posteddate></table>" +
"</data>";
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource service = client.resource(getBaseURI());
System.out.println(service.path("restful").path("remotedb").path("save").path(xml).accept(MediaType.TEXT_XML).get(String.class));
}
private static URI getBaseURI() {
return UriBuilder.fromUri("http://localhost:8080/WebServiceModule").build();
}
}
Now when i called the web service i got the following exception
Exception in thread "main" com.sun.jersey.api.client.UniformInterfaceException: GET http://localhost:8080/WebServiceModule/restful/remotedb/save/%3Cdata%3E%3Ctable%3E%3Ctest_id%3Et4%3C/test_id%3E%3Cdateprix%3E2013-06-06%2022:50:40.252%3C/dateprix%3E%3Cnomtest%3ENOMTEST%3C/nomtest%3E%3Cprixtest%3E12.70%3C/prixtest%3E%3Cwebposted%3EN%3C/webposted%3E%3Cposteddate%3E2013-06-06%2021:51:42.252%3C/posteddate%3E%3C/table%3E%3C/data%3E returned a response status of 404 Not Found
at com.sun.jersey.api.client.WebResource.handle(WebResource.java:686)
at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74)
at com.sun.jersey.api.client.WebResource$Builder.get(WebResource.java:507)
at com.main.WebServiceClient.callWebService(WebServiceClient.java:25)
at com.main.Test.main(Test.java:7)
Passing XML data in a path segment is very unorthodox and likely to raise all kind of issues. You should pass it as a query parameter, e.g. /WebServiceModule/restful/remotedb/save?xmlData=
%3Cdata...
#GET
#Path("/save")
#Produces(MediaType.TEXT_XML)
public String saveData(#QueryParam("xmlData") String xml) {
return xml;
}
}
or even better if it is a write operation as the name suggests then it should be a POST /WebServiceModule/restful/remotedb/save with the xmlData passed in the request body.
#POST
#Path("/save")
#Produces(MediaType.TEXT_XML)
public String saveData(String xml) {
return xml;
}
}
or even better yet, if you can map your xmlData to a POJO with JAXB's #XmlRootElement annotation, then you can get jersey to parse it for you:
#POST
#Path("/save")
#Consumes(MediaType.APPLICATION_XML)
public String saveData(YourXmlDataObject obj) {
return obj.getField();
}
}
I want to create a REST Jersey Web-Service accepting JSON string as input parameter.
Also I will use post requestand from webmethod I will return one JSON string.
How can I consume this in a HTML page using Ajax post request.
I want to know what all changes I need to make it on web method to accept JSON String.
public class Hello {
#POST
public String sayPlainTextHello() {
return "Hello Jersey";
}
}
Need to break down your requests. First, you want to accept a JSON string. So on your method you need
#Consumes(MediaType.APPLICATION_JSON)
Next, you need to decide what you want your method to obtain. You can obtain a JSON string, as you suggest, in which case your method would look like this:
#Consumes(MediaType.APPLICATION_JSON)
public String sayPlainTextHello(final String input) {
Or alternatively if your JSON string maps to a Java object you could take the object directly:
#Consumes(MediaType.APPLICATION_JSON)
public String sayPlainTextHello(final MyObject input) {
You state that you want to return a JSON string. So you need:
#Produces(MediaType.APPLICATION_JSON)
And then you need to actually return a JSON string:
return "{\"result\": \"Hello world\"}";
So your full method looks something like this:
#PATH("/hello")
#POST
#Consumes(MediaType.APPLICATION_JSON)
#Produces(MediaType.APPLICATION_JSON)
public String sayPlainTextHello(final String input) {
return "{\"result\": \"Hello world\"}";
}
Regarding using AJAX to send and receive, it would look something like this:
var myData="{\"name\": \"John\"}";
var request = $.ajax({
url: "/hello",
type: "post",
data: myData
});
request.done(function (response, textStatus, jqXHR){
console.log("Response from server: " + response);
});
This will work. "path" is the relative URL path to be used in AJAX call.
public class Hello {
#POST
#Path("/path")
#Produces({ "text/html" })
public String sayPlainTextHello() {
return "Hello Jersey";
}
}
I am trying to pass a JSON request body to a REST Webservice which is made using CXFRS in my Apache Camel application.
I want to access the request JSON passed in my Processor.
REST URL:
http://localhost:8181/mywebservice/Hello/name/{request_param}
Though i am posting a JSON in request body, still in my processor exchange.getIn().getBody() always return the {request_param} not the Request JSON.
My REST webservice is as follows:
#Path("/name/")
#Consumes({"application/json" ,"application/xml"})
public class HelloRest {
#POST
#Path("/{name}")
public TestPojo sayHi(#PathParam("name") String name) {
return new TestPojo(name);
}
}
Server part:
#POST
#Path("/")
#Produces(MediaType.TEXT_PLAIN)
#Consumes(MediaType.APPLICATION_JSON)
public String add(MappingUser newUser){
UserEntity userEntity = new UserEntity(newUser.getNickname(), newUser.getPassword());
boolean ret = myDB.addUser(userEntity);
//sends the return value (primitive type) as plain text over network
return String.valueOf(ret);
}
Client part:
public boolean addUser(User user){
WebResource resource = client.resource(url).path("/");
String response = resource
//type of response
.accept(MediaType.TEXT_PLAIN_TYPE)
//type of request
.type(MediaType.APPLICATION_JSON_TYPE)
//method
.post(String.class, user);
return Boolean.valueOf(response);
}