I'm used to developing in C# Web API where as well as routing attributes for individual endpoints I can also add a prefix for the controller, e,g,
[RoutePrefix("/MyController")]
However I'm developing in Java Spring Boot and although I can map individual endpoints, I can't find a way to add a prefix for all.
Is this possible?
On spring-boot you can use the #RequestMapping("/MyController") annotation at the class level.
#RequestMapping("/MyController")
public class MyController {
#GetMapping(value = "/helloWord") // this will become /MyController/helloWord
public String helloWorld() {
return "Hello World";
}
}
Related
I have a Java project with various #RequestMapping annotations.I want to make a new project which can use this #RequestMapping,is that possible
Of course you can.
If I understand your question correctly, do you want to use the data provided by a Spring application in another application?
It's not that hard, you just have to keep a few things in mind.
The applications have to run on different ports, of course both applications have to be started.
For example, App1 has a #RequestMapping #GetMapping for personal data.
The path is e.g. http://localhost:8080/persondata
In the second application, you only need to address the API endpoint if you need this data.
This can be done with RestTemplate, for example.
#RestController
#RequestMapping("/persondata")
class PersonDataRestController {
private final Service personService;
public PersonDataRestController(Service personService) {
this.personService = personService;
}
#GetMapping
public ResponseEntity<Collection<?>> getAllPersonData() {
return ResponseEntity.ok(personService.allPersonData());
}
}
You just have to replace the Service with your PersonService or whatelse.
in the second application you can call the REST endpoint with RestTemplate for example
RestTemplate template = new RestTemplate();
try{
ResponseEntity<ArrayList<?>> response = template.exchange("http://localhost:8080/persondata", HttpMethod.GET, null, new ParameterizedTypeReference<ArrayList<?>>() {});
In terms of the specific application, you may need a DTO object.
For this topic i can suggest you this website
I hope I could answer your question
I am Planning to build a web application using Spring Boot as restful service.
my spring boot web restful application should be accessible by other application as well. In case if any one accessing the rest service from other application then my application should work as expected.
#Controller
public class GreetingController {
#RequestMapping("/greeting")
public String greeting(#RequestParam(value="name", required=false, defaultValue="World") String name, Model model) {
model.addAttribute("name", name);
return "greeting";
}
}
In above example if call is made from outside of application then the rest service should return JSON output.
One way we can have some variable to distinguish as request variable. But I do not want like that. Please share some standard way.
Appreciate your help.
Idiomatic way is to rely on Accept request header.
If requester presents Accept: application/json then return him JSON data (REST API).
If requester provides you with Accept: application/xhtml+xml return him HTML (web frontend).
Implementation-wise you should is to be done use #RequestMapping with consumes argument. You need two methods. If business logic for both paths is the same then in could be reused. Business logic should reside in another method or in separate #Service. Business logic on its own should not know, care or rely on transport protocol (HTTP), serialization of request response or presentation. Business logic should just work with POJOs and leave serialization to #Controller.
#Controller
#RequestMapping("/greeting")
public class GreetingController {
#RequestMapping(consumes="application/json")
#ResponseBody //required if you want to return POJO (spring will serialize it to response body)
public void rest() {
//return POJO, it will be serialized to JSON. or serialize pojo
directly and return response with manually set body and headers.
}
#RequestMapping(consumes="application/xhtml+xml")
public void html() {
//populate model, return string pointing to HTML to View
}
}
I suggest creating two controller classes, the second one using #RestController. Then, have two paths; the second could be "rs/greeting". This approach, which separates the Web and RESTful concerns, is much more extensible and doesn't require any weird headers that most clients don't want to deal with.
I'm currently developing a Spring Boot application that exposes endpoints using #RestController and #RequestMapping annotations.
I recently discovered the concept of the projections as defined in Spring Data Rest (#Projection-annotated interfaces and #RepositoryRestResource-annotated JPA repositories) and I'd like to apply this concept to my existing services.
As I understand this post Spring Boot Projection with RestController, (please correct me if I'm wrong), #RestController and #RepositoryRestResource classes both define endpoints. So these annotations seem quite incompatible.
Is there a Spring component which can simply apply the projections concept to the #RestController endpoints ?
Is there a way to manually reroute requests from a endpoint to another ? (for example, using #RestController endpoints as some sort of proxy which performs controls or other operations before rerouting the request to the #RepositoryRestResource endpoints)
EDIT: I add a glimpse of the final code I'd like to have in the end.
#RestController
public class MyController {
#RequestMapping(value = "/elements/{id}", method = RequestMethod.GET)
public ResponseEntity<Element> getElements(
#PathVariable("id") Integer elementId,
#RequestParam("projection") String projection,
#RequestHeader(value = "someHeader") String header{
// [manual controls on the header then call to a service which returns the result]
}
}
#Entity
public class Element {
private Integer id;
private String shortField;
private String longField;
private List<SubElement> subElements;
// [Getters & setters]
}
#Projection(name = "light", types = {Element.class})
interface ElementLight {
public Integer getId();
public String getShortField();
}
If I call /elements/4, I'd get the complete Element having id = 4.
If I call /elements/4?projection=light, I'd get only the id and the short field of the Element having id = 4.
This answer gives some detail about how to create projection instances of your entities - https://stackoverflow.com/a/29386907/5371736
So depending on your projection parameter you could generate the given projections.
Hope this is what you are looking for.
in my exercise i have to develop a spring application which should be accessible through a WebGUI AND a REST service.
Now i browed through the examples of Spring MVC, there is this hello world tutorial on Spring MVC.
The controller looks like as follows:
#Controller
#RequestMapping("/welcome")
public class HelloController {
#RequestMapping(method = RequestMethod.GET)
public String printWelcome(ModelMap model) {
model.addAttribute("message", "Spring 3 MVC Hello World");
return "hello";
}
}
Then i looked through the Spring REST example which looks like this:
#Controller
#RequestMapping("/movie")
public class MovieController {
#RequestMapping(value = "/{name}", method = RequestMethod.GET)
public String getMovie(#PathVariable String name, ModelMap model) {
model.addAttribute("movie", name);
return "list";
}
#RequestMapping(value = "/", method = RequestMethod.GET)
public String getDefaultMovie(ModelMap model) {
model.addAttribute("movie", "this is default movie");
return "list";
}
}
Now I am wondering, how do these two examples (Spring-mvc and Spring-rest) differ?
They both use the same annotations and work similar. Aren't that both just REST examples?
How can I provide a Rest-Interface to a Spring-MVC application?
regards
In order to provide rest interface to Spring MVC application, you can apply #RequestMapping annotation with a path name to each of the methods in controller, this creates a unique URL path for each of the rest services you would like to provide.
Meaning, the rest services are nothing but the methods in Spring MVC controller with #RequestMapping annotation.
If you would like to learn how Spring MVC supports Rest Based services, the below link might help:
http://blog.springsource.org/2009/03/08/rest-in-spring-3-mvc/#features
Both samples are about Spring Web MVC.
You should pay more attention to definitions, like what is REST
https://en.wikipedia.org/wiki/Representational_state_transfer
Representational State Transfer is intended to evoke an image of how a
well-designed Web application behaves: presented with a network of Web
pages (a virtual state-machine), the user progresses through an
application by selecting links (state transitions), resulting in the
next page (representing the next state of the application) being
transferred to the user and rendered for their use.
Spring Web MVC greatly facilitates developing REST web APIs and that's it.
Rememeber #ResponseBody as return type on method is going to be REST.
ofcourse returned object can be negotiated with either JSON or XML.
I want the client and server application to talk to each other using REST services. I have been trying to design this using Spring MVC. I am looking for something like this:
Client does a POST rest service call saveEmployee(employeeDTO, companyDTO)
Server has a similar POST method in its controller saveEmployee(employeeDTO, companyDTO)
Can this be done using Spring MVC?
Yes, this can be done. Here's a simple example (with Spring annotations) of a RESTful Controller:
#Controller
#RequestMapping("/someresource")
public class SomeController
{
#Autowired SomeService someService;
#RequestMapping(value="/{id}", method=RequestMethod.GET)
public String getResource(Model model, #PathVariable Integer id)
{
//get resource via someService and return to view
}
#RequestMapping(method=RequestMethod.POST)
public String saveResource(Model model, SomeResource someREsource)
{
//store resource via someService and return to view
}
#RequestMapping(value="/{id}", method=RequestMethod.PUT)
public String modifyResource(Model model, #PathVariable Integer id, SomeResource someResource)
{
//update resource with given identifier and given data via someService and return to view
}
#RequestMapping(value="/{id}", method=RequestMethod.DELETE)
public String deleteResource(Model model, #PathVariable Integer id)
{
//delete resource with given identifier via someService and return to view
}
}
Note that there are multiple ways of handling the incoming data from http-request (#RequestParam, #RequestBody, automatic mapping of post-parameters to a bean etc.). For longer and probably better explanations and tutorials, try googling for something like 'rest spring mvc' (without quotes).
Usually the clientside (browser) -stuff is done with JavaScript and AJAX, I'm a server-backend programmer and don't know lots about JavaScript, but there are lots of libraries available to help with it, for example see jQuery
See also:
REST in Spring 3 MVC
Yes, Rest is very easy to implement using spring MVC.
#RequestMapping(value="/saveEmploee.do",method = RequestMethod.POST)
#ResponseBody
public void saveEmployee(#RequestBody Class myclass){
//saving class.
//your class should be sent as JSON and will be deserialized by jackson
//bean which should be present in your Spring xml.
}