Hello i'm new in implementing Spring REST Web Service what's the cause of my error.
Here's my code
#RequestMapping(value = "/message/{regID}/name/{name}", method = RequestMethod.GET, headers = "Accept=application/json")
public String getMessage(#PathVariable String regID, #PathVariable String name) {
return "Hello Alvin!" + regID + " " + name;
}
i want to call it using the web browser but i failed to successfully invoke it but when i call single parameter of may another RequestMapping is Successfully completed.. Here is the RequestMapping that i successfully called
#RequestMapping(value = "/country/{id}", method = RequestMethod.GET, headers = "Accept=application/json")
public Country getCountryById(#PathVariable int id) {
List<Country> listOfCountries = new ArrayList<Country>();
listOfCountries = createCountryList();
for (Country country : listOfCountries) {
if (country.getId() == id)
return country;
}
return null;
}
Or How can i implement multiple parameter for my RequestMapping..?
The chances are that you're using the InternalResourceViewResolver, in which case the methods that return String will interpret the returned valued as a view name that will be searched inside the locations designated in the view resolver. Your no mapping found probably refers to that the framework can't find a view name that matches what you're returning
As your intention seems to be to return the text only you should simply additional map your method with #ResponseBody which will in turn add your text to response body instead of interpreting it as a view name, so
#ResponseBody
public String getMessage(#PathVariable String regID, #PathVariable String name)
Otherwise, your mapping is just fine
I you have more than one path variables, you need to sepecify the identifier in #PathVariable:
#RequestMapping(value = "/message/{regID}/name/{name}", method = RequestMethod.GET, headers = "Accept=application/json")
public String getMessage(#PathVariable("regID") String regID, #PathVariable("name") String name) {
return "Hello Alvin!" + regID + " " + name;
}
Related
I have this rest controller method in springboot
#GetMapping("/cghsHcoSearchText/cityId/{cityId}/hcoName/{hcoName}/treatmentName/{treatmentName}")
public String cghsHcoSearchText(#PathVariable String cityId, #RequestParam(name = "hcoName", required = false) String hcoName,
#RequestParam(name = "treatmentName", required = false) String treatmentName) {
return "Some Text";
}
It has one PathVariable and 2 optional Request parameter.
Now when I hit this url with treatmentName = null i get Whitelabel Error Page
http://localhost:8082/cghs/cghsHcoSearchText/cityId/011?hcoName=Guru?
Any help will be appreciated.
We should not specify request param as a placeholder in URL mapping. Only the path params should be mentioned in placeholder. Sharing a code snippet and corresponding URL which will help out in understanding this
#GetMapping("hello/{id}")
public ResponseEntity<Void> printInfo(#PathVariable("id") String id,
#RequestParam(required = false, name = "name") String name) {
System.out.println(id + " " + name);
return new ResponseEntity<>(HttpStatus.OK);
}
Here id comes as a path param and name as a request param which is not mentioned in mapping annotation.
URL would look like
http://localhost:8080/hello/234?name=pappi
In Java-Jersey, it is possible to receive a dynamic path to a resource, e.g.
localhost:8080/webservice/this/is/my/dynamic/path
#GET
#Path("{dynamicpath : .+}")
#Produces(MediaType.APPLICATION_JSON)
public String get(#PathParam("dynamicpath") String p_dynamicpath) {
return p_dynamicpath;
}
prints out: this/is/my/dynamic/path
Question: how to do this in Spring MVC?
For multiple items inside your path you can access the dynamic path values like this:
#RequestMapping(value="/**", method = RequestMethod.GET)
public String get(HttpServletRequest request) throws Exception {
String dynPath = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
System.out.println("Dynamic Path: " + dynPath );
return dynPath;
}
If you know beforehand hoe many levels of path variables you'll have you can code them explicit like
#RequestMapping(value="/{path1}/{path2}/**", method = RequestMethod.GET)
public String get(#PathVariable("path1") String path1,
#PathVariable("path2") String path2,
HttpServletRequest request) throws Exception {
String dynPath = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
System.out.println("Dynamic Path: " + dynPath );
return dynPath;
}
If you want to see the String returned in your browser, you need to declare the method #ResponseBody as well (so the String you return is the content of your response):
#RequestMapping(value="/**", method = RequestMethod.GET, produces = "text/plain")
#ResponseBody
public String get(HttpServletRequest request) throws Exception {
Given a method like:
#RequestMapping(value = {"/foo"}, method = RequestMethod.GET)
public String getMappingValueInMethod() {
log.debug("requested "+foo); //how can I make this refer to /foo programmatically?
return "bar";
}
The use case is for refactoring some lengthly code. I have several GET methods doing roughly the same thing and only the request mapping value is different.
I've looked at using path variables, but this is not really what I want (unless there's some clever use of it that I don't see). I could also get a value from the HttpServletRequest like in this post, but not sure whether there's a better way.
Solution 1
With HttpServletRequest.
#RequestMapping(value = "/foo", method = RequestMethod.GET)
public String fooMethod(HttpServletRequest request) {
String path = request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE).toString();
System.out.println("path foo: " + path);
return "bar";
}
Solution 2
With reflection.
#RequestMapping(value = "/foo2", method = RequestMethod.GET)
public String fooMethod2() {
try {
Method m = YourClassController.class.getMethod("fooMethod2");
String path = m.getAnnotation(RequestMapping.class).value()[0];
System.out.println("foo2 path: " + path);
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
return "bar";
}
If you want get path from class (instead method) you can use:
String path = YourClassController.class.getAnnotation(RequestMapping.class).value();
Solution 3
With #PathVariable.
#RequestMapping(value = {"/{foo3}"}, method = RequestMethod.GET)
public #ResponseBody String fooMethod3(#PathVariable("foo3") String path) {
path = "/" + path; // if you need "/"
System.out.println("foo3 path: " + path);
return "bar";
}
The simplest way of doing this would be putting the array directly in the request mapping i am assuming this is what you want.
#RequestMapping(value = {"/foo","/foo1","/foo2"}, method = RequestMethod.GET)
public String getMappingValueInMethod(HttpServletRequest request) {
log.debug("requested "+request.getRequestURI());
return request.getRequestURI();
}
Then name the jsp files similar to the uri or other wise you could store the mapping between the request uri and the name of the page in the db .
I am developing a project using Spring Boot. I've a controller which accepts GET requests.
Currently I'm accepting requests to the following kind of URLs:
http://localhost:8888/user/data/002
but I want to accept requests using query parameters:
http://localhost:8888/user?data=002
Here's the code of my controller:
#RequestMapping(value="/data/{itemid}", method = RequestMethod.GET)
public #ResponseBody
item getitem(#PathVariable("itemid") String itemid) {
item i = itemDao.findOne(itemid);
String itemname = i.getItemname();
String price = i.getPrice();
return i;
}
Use #RequestParam
#RequestMapping(value="user", method = RequestMethod.GET)
public #ResponseBody Item getItem(#RequestParam("data") String itemid){
Item i = itemDao.findOne(itemid);
String itemName = i.getItemName();
String price = i.getPrice();
return i;
}
While the accepted answer by afraisse is absolutely correct in terms of using #RequestParam, I would further suggest to use an Optional<> as you cannot always ensure the right parameter is used. Also, if you need an Integer or Long just use that data type to avoid casting types later on in the DAO.
#RequestMapping(value="/data", method = RequestMethod.GET)
public #ResponseBody
Item getItem(#RequestParam("itemid") Optional<Integer> itemid) {
if( itemid.isPresent()){
Item i = itemDao.findOne(itemid.get());
return i;
} else ....
}
To accept both #PathVariable and #RequestParam in the same /user endpoint:
#GetMapping(path = {"/user", "/user/{data}"})
public void user(#PathVariable(required=false,name="data") String data,
#RequestParam(required=false) Map<String,String> qparams) {
qparams.forEach((a,b) -> {
System.out.println(String.format("%s -> %s",a,b));
}
if (data != null) {
System.out.println(data);
}
}
Testing with curl:
curl 'http://localhost:8080/user/books'
curl 'http://localhost:8080/user?book=ofdreams&name=nietzsche'
In Spring boot: 2.1.6, you can use like below:
#GetMapping("/orders")
#ApiOperation(value = "retrieve orders", response = OrderResponse.class, responseContainer = "List")
public List<OrderResponse> getOrders(
#RequestParam(value = "creationDateTimeFrom", required = true) String creationDateTimeFrom,
#RequestParam(value = "creationDateTimeTo", required = true) String creationDateTimeTo,
#RequestParam(value = "location_id", required = true) String location_id) {
// TODO...
return response;
#ApiOperation is an annotation that comes from Swagger api, It is used for documenting the apis.
To accept both Path Variable and query Param in the same endpoint:
#RequestMapping(value = "/hello/{name}", method = RequestMethod.POST)
public String sayHi(
#PathVariable("name") String name,
#RequestBody Topic topic,
//#RequestParam(required = false, name = "s") String s,
#RequestParam Map<String, String> req) {
return "Hi "+name +" Topic : "+ topic+" RequestParams : "+req;
}
URL looks like : http://localhost:8080/hello/testUser?city=Pune&Pin=411058&state=Maha
I was interested in this as well and came across some examples on the Spring Boot site.
// get with query string parameters e.g. /system/resource?id="rtze1cd2"&person="sam smith"
// so below the first query parameter id is the variable and name is the variable
// id is shown below as a RequestParam
#GetMapping("/system/resource")
// this is for swagger docs
#ApiOperation(value = "Get the resource identified by id and person")
ResponseEntity<?> getSomeResourceWithParameters(#RequestParam String id, #RequestParam("person") String name) {
InterestingResource resource = getMyInterestingResourc(id, name);
logger.info("Request to get an id of "+id+" with a name of person: "+name);
return new ResponseEntity<Object>(resource, HttpStatus.OK);
}
See here also
I am trying to pass variables using post in angular to java spring. The java post function is executed, but all of the parameters are null.
var CreditCard = $resource("http://:host::port/" + context + "/agent/creditCard",
{host: "localhost", port: 8080 },
{getTestPost2: {method:'POST', params:{charge:true, jsonPost:"1234"}}
});
var newCard = new CreditCard({number:'0123'});
newCard.name = "Mike Smith";
newCard.$save();
I checked the my network status shows 200 OK.
Request Payloadview source
{number:0123, name:Mike Smith}
name: "Mike Smith"
number: "0123"
Java
#RequestMapping(
value="/agent/creditCard",
method = RequestMethod.POST,
produces = MediaType.APPLICATION_JSON_VALUE)
public #ResponseBody void getTestPost2(
#PathVariable String clientId,
#RequestParam(value="jsonPost", required=false) String jsonPost,
#RequestParam(value="charge", required=false) boolean charge,
#RequestParam(value="name", required=false) String name,
#RequestParam(value="number", required=false) String number
){
logger.debug("jsonPost " + jsonPost);
logger.debug("name " + name);
logger.debug("charge " + charge);
logger.debug("number " + number);
}
Output:
jsonPost null
name null
charge false
number null
Java
#RequestMapping(
value="/agent/creditCard",
method = RequestMethod.POST,
params = { "jsonPost", "charge", "name", "number"}
produces = MediaType.APPLICATION_JSON_VALUE)
#ResponseBody
public #ResponseBody void getTestPost2(
#PathVariable String clientId,
#RequestParam(value="jsonPost", required=false) String jsonPost,
#RequestParam(value="charge", required=false) boolean charge,
#RequestParam(value="name", required=false) String name,
#RequestParam(value="number", required=false) String number
){
logger.debug("jsonPost " + jsonPost);
logger.debug("name " + name);
logger.debug("charge " + charge);
logger.debug("number " + number);
}
I add params and ResponseBody, you can try it.
You should write a CreditCard class to use as the parameter type. Annotate it with #RequestBody to let a HttpMessageConverter convert the JSON body to the required object.
#ResponseBody
#RequestMapping(
value="/agent/creditCard",
method = RequestMethod.POST,
consumes = MediaType.APPLICATION_JSON_VALUE)
public void postCreditCard(#RequestBody CreditCard creditCard) {
// process data...
}