Fork me on GitHub

Spring REST Servlet Tutorial

This tutorial demonstrates direct injection of jakarta.servlet.http.HttpServletRequest and jakarta.servlet.http.HttpServletResponse into OfficeFloor REST service methods.

Both types are available as plain method parameters — no annotation is required. OfficeFloor recognises them by type and supplies the current request/response objects from the live servlet context.

This is useful when low-level HTTP access is needed: reading raw headers, working with binary streams, or writing directly to the response output stream.

Tutorial Source

Maven dependency

		<dependency>
			<groupId>net.officefloor.springboot</groupId>
			<artifactId>officefloor-rest-spring-boot-starter</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

Application class

@SpringBootApplication
public class SpringRestServletApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringRestServletApplication.class, args);
	}
}

Reading from HttpServletRequest

Declaring HttpServletRequest as a method parameter gives access to query parameters, headers, cookies, and the raw input stream. OfficeFloor injects the live request object:

public class RequestInfoService {

	public void service(HttpServletRequest request, ObjectResponse<String> response) {
		String name = request.getParameter("name");
		String userAgent = request.getHeader("User-Agent");
		response.send("name=" + name + ", agent=" + userAgent);
	}
}
service:
  class: net.officefloor.tutorial.springrestservlet.RequestInfoService

Writing directly to HttpServletResponse

Declaring HttpServletResponse as a method parameter gives direct control over status codes, response headers, and the output stream. When writing directly to the response, do not also declare an ObjectResponse parameter — the framework assumes one or the other:

public class DirectWriteService {

	public void service(HttpServletResponse response) throws IOException {
		response.setContentType("text/plain");
		response.getWriter().write("Written directly to servlet response");
	}
}
service:
  class: net.officefloor.tutorial.springrestservlet.DirectWriteService

Testing

@SpringBootTest
@AutoConfigureMockMvc
public class SpringRestServletTest {

	@Autowired
	private MockMvc mvc;

	@Test
	public void requestInfo() throws Exception {
		mvc.perform(get("/request/info?name=OfficeFloor")
				.header("User-Agent", "TestAgent/1.0")
				.accept(MediaType.APPLICATION_JSON))
				.andExpect(status().isOk())
				.andExpect(content().string(containsString("name=OfficeFloor")))
				.andExpect(content().string(containsString("agent=TestAgent/1.0")));
	}

	@Test
	public void directWrite() throws Exception {
		mvc.perform(get("/response/direct"))
				.andExpect(status().isOk())
				.andExpect(content().string("Written directly to servlet response"));
	}
}

Next

The Spring REST Qualifier tutorial demonstrates @Qualifier injection to disambiguate multiple beans of the same type.