In a URI, https://www.google.com/items/2 how to get 2 alone (item id) to process further [duplicate] - servlets

This question already has answers here:
Servlet and path parameters like /xyz/{value}/test, how to map in web.xml?
(7 answers)
Closed 5 months ago.
In a URI, https://www.google.com/items/2 how to get 2 alone (item id) to process further

PATH VARIABLE
Its called PATH VARIABLE, this is just different type of HTTP REQUEST to send parameters within API.
In SpringBoot we can use PATH VARIABLE like this :
#RestController
#RequestMapping("car")
public class CarController {
#Autowired
CarService carservice;
#GetMapping("{id}")
public Car getCarById(#PathVariable long id) {
return carService.findById(id)
}
}
Then you can call your api like this and send car id in last url
localhost:8080/car/1

Related

How to send integer array as a parameter from endpoint without querystring from url in asp.net core webapi

I have an endpoint that has a parameter that type of integer array.I want send some value from body as a json string.When I tried it , I was getting null value from parameter so I tried to send integer array from url but happen problem about of url length so I want to know that is possible to send it from request body or how to fix url length problem for 5.000 item
request body that I tried
{
"Ids": [349]
}
endpoint function
[HttpGet]
public void GetModels([FromBody]List<int> Ids)
{
}
First, in general including a body in a GET request is often not considered very RESTful. It is no longer specifically "banned" by RFC, but it is not typical. That said, you can make this work in ASP.Net Core using the [FromBody] attribute.
The issue has to do with how you are formatting your JSON body. Using the signature for GetModels that you have listed above, the JSON body doesn't match the parameters. Your JSON represents a top-level object with a property Ids that is an array of int, not just an array of it (or List).
If you want to use public void GetModels([FromBody]List<int> Ids) then your JSON body should simply be an array (e.g. [349,350,351]) and nothing else (no brackets, no "Ids" property name).
If you want to use the JSON body you list above, then you need another class to use for model binding, a DTO. That DTO would look something like:
public class IdDto
{
public List<int> Ids { get; set; }
}
and then your GetModels method would look like:
[HttpGet]
public void GetModels([FromBody] IdDto idDto)
{
var myIds = idDto.Ids;
}
Lastly, be sure that your GET request has a Content-Type set to application/json or ASP.Net will return a 415 "Unsupported Media Type".

Spring boot REST MVC map request parameter with different field name to object

Assume that I have want to capture a bunch of request parameters as one object like this:
#GetMapping("/")
public List<Item> filterItems(#Valid Filter filter){}
and the Filter class looks like this:
class Filter {
public String status;
public String start;
public String end;
}
Now in the API the request parameter name is state instead of status like this ?state=A&start=1&end=2. How do I make these request parameters to map to my Filter object without having to rename status? I know if I have #RequestParam("state") String status it would work but I want it to be part of the request object.
I tried adding #JsonProperty('state') on the field but it didn't work.

What is the difference between #PathParam and #PathVariable [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 2 years ago.
Improve this question
To my knowledge both serves the same purpose. Except the fact that #PathVariable is from Spring MVC and #PathParam is from JAX-RS. Any insights on this?
#PathVariable and #PathParam both are used for accessing parameters from URI Template
Differences:
As you mention #PathVariable is from spring and #PathParam is from JAX-RS.
#PathParam can use with REST only, where #PathVariable used in Spring so it works in MVC and REST.
QueryParam:
To assign URI parameter values to method arguments. In Spring, it is #RequestParam.
Eg.,
http://localhost:8080/books?isbn=1234
#GetMapping("/books/")
public Book getBookDetails(#RequestParam("isbn") String isbn) {
PathParam:
To assign URI placeholder values to method arguments. In Spring, it is #PathVariable.
Eg.,
http://localhost:8080/books/1234
#GetMapping("/books/{isbn}")
public Book getBook(#PathVariable("isbn") String isbn) {
#PathParam is a parameter annotation which allows you to map variable URI path fragments into your method call.
#Path("/library")
public class Library {
#GET
#Path("/book/{isbn}")
public String getBook(#PathParam("isbn") String id) {
// search my database and get a string representation and return it
}
}
for more details : JBoss DOCS
In Spring MVC you can use the #PathVariable annotation on a method argument to bind it to the value of a URI template variable
for more details : SPRING DOCS
#PathParam is a parameter annotation which allows you to map variable URI path fragments into your method call.
#PathVariable is to obtain some placeholder from the URI (Spring call it an URI Template)
Some can use #PathParam in Spring as well but value will be null when URL request is being made
Same time if We use #PathVarriable then if value is not being passed then application will throw error
#PathVariable
#PathVariable it is the annotation, that is used in the URI for the incoming request.
http://localhost:8080/restcalls/101?id=10&name=xyz
#RequestParam
#RequestParam annotation used for accessing the query parameter values from the request.
public String getRestCalls(
#RequestParam(value="id", required=true) int id,
#RequestParam(value="name", required=true) String name){...}
Note
whatever we are requesting with rest call i.e, #PathVariable
whatever we are accessing for writing queries i.e, #RequestParam
#PathParam: it is used to inject the value of named URI path parameters that were defined in #Path expression.
Ex:
#GET
#Path("/{make}/{model}/{year}")
#Produces("image/jpeg")
public Jpeg getPicture(#PathParam("make") String make, #PathParam("model") PathSegment car, #PathParam("year") String year) {
String carColor = car.getMatrixParameters().getFirst("color");
}
#Pathvariable: This annotation is used to handle template variables in the request URI mapping ,and used them as method parameters.
Ex:
#GetMapping("/{id}")
public ResponseEntity<Patient> getByIdPatient(#PathVariable Integer id) {
Patient obj = service.getById(id);
return new ResponseEntity<Patient>(obj,HttpStatus.OK);
}

provide query string request mapping variable at class level

I am trying to make spring boot application & swagger. Application is for REST service provide. I have made application running each page.
I have made a simple controller that have RequestMapping("/group/user/contact").
Which is working fine.
I am trying to do something like RequestMapping("/group/{type}/contact") at class level.
So my question is that is it possible ?
If yes then just want some basic guidance. and if no then fine.
My all request working fine. All request came from CORS filter class.
You can do this, the handler method should look something like
#Controller
#RequestMapping("/group/{type}/contact")
public class ClassLevelPathVariableController {
#ResponseBody
#RequestMapping(method = RequestMethod.GET)
public String classLevelMapping(#PathVariable String type) {
return type;
}
}
In this setup a GET request like e.g. /group/test/contact would be handled by the classLevelMapping method and the type variable will be populated with the value "test"

Pagination In MVC [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
How do I do pagination in ASP.NET MVC?
Hii,
I want to add pagination to my view .
i am getting data from model as a list of users in the controller .
and in view i am displaying that from Model by inheriting IEnumarable..now
How can i inherit IPagelist overhere?
can anyone help me??
What about passing the page number as an id to your Index action?
For example:
public ActionResult Index(int id)
{
var query = from x in y
select x;
return View(query.Skip(id * 20).Take(20));
}

Resources