Skip to end of metadata
Go to start of metadata

Servlet with @Controller annotation
@Controller
@RequestMapping( "/custom" )
public class CustomController
{
	@Autowired
	private PositionService ps;
 
	@RequestMapping( "positions" )
    public @ResponseBody List<User> getPositions( @RequestParam String positionName )
    {
		return ps.getByName(positionName);
	}
}

First, add the @Controller so the system sees the class as a servlet. The path to a servlet annotated with the @Controller starts with api/. The @RequestMapping annotation allows us to define the path to the servlet. As you can see in the example you have two such annotations, which together with the prefix api/ form the path to the servlet.

For example, the path would be: api/custom/positions. With the @ResponseBody annotation, the servlet's response is automatically converted to JSON format. The last @RequestParam annotation allows you to read the parameter passed by the servlet. The parameter can also be a JSON-encoded object, in which case the system will automatically decode it.

Request methods

Defining a request method is done with the @RequestMapping( method = "method" ) annotation.

The basic methods are as follows:

  • RequestMethod.GET
  • RequestMethod.POST
  • RequestMethod.DELETE

For the above example, retrieving the item using the GET method will look like this:

@RequestMapping( value = "positions", method = RequestMethod.GET )
public @ResponseBody List<User> getPositions( @RequestParam String positionName )
{
    return ps.getByName(positionName);
}
@RequestMapping( value = "positions/{positionName}", method = RequestMethod.GET )
public @ResponseBody List<User> getPositions( @PathVariable String positionName )
{
    return ps.getByName(positionName);
}

It is possible to send basic parameters in the REST link of the request. Such a variable should be placed in {} brackets, and then transferred to the method by reading the parameter

@PathVariable String positionName. Now if we call url: api/custom/positions/test, the value test will be passed to the matoda as the positionName parameter.

 

Useful resources:

File download

The com.suncode.pwfl.web.support.io.DownloadResource class simplifies the creation of a servlet to download a file by taking care of setting the appropriate HTTP response headers:

  • Content-Disposition - a header containing the file name (the file name can also contain UTF-8 characters)
  • Content-Type - a header containing the file name (the file name can also contain UTF-8 characters)
  • Content-Length - the size of the file

To do this, return the appropriate DownloadResource class object from any Spring MVC controller method (@Controller). This resource will be automatically passed to the browser with the appropriate headers. Note the @ResponseBody annotation which must be present on the method.

Below is an example of such a controller along with a sample server response.

@Controller
@RequestMapping("/file")
public class DownloadFileServlet()
{
	@RequestMapping( value = "download/{filePath}", method = RequestMethod.GET )
    public @ResponseBody DownloadResource download( @PathVariable String filePath )
        throws Exception
    {
        File file = new File(filePath);
        return new DownloadResource( file );
    }
}
HeaderValue
Content-Disposition
attachment; filename="Faktury_ca%C5%82o%C5%9B%C4%87.pdf"; filename*=UTF-8''Faktury_ca%C5%82o%C5%9B%C4%87.pdf
Content-Length
15913
Content-Type
application/pdf;charset=UTF-8

  • No labels