Skip to content

Commit cbc8231

Browse files
committed
Addressing issue reported in #117 and added a unit test for it.
1 parent 7dbb3f2 commit cbc8231

File tree

5 files changed

+66
-1
lines changed

5 files changed

+66
-1
lines changed

aws-serverless-java-container-spring/src/main/java/com/amazonaws/serverless/proxy/spring/LambdaSpringApplicationInitializer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public void dispatch(HttpServletRequest request, HttpServletResponse response)
115115
* Gets the initialized Spring dispatcher servlet instance.
116116
* @return The Spring dispatcher servlet
117117
*/
118-
public Servlet getDispatcherServlet() {
118+
public DispatcherServlet getDispatcherServlet() {
119119
return dispatcherServlet;
120120
}
121121

aws-serverless-java-container-spring/src/main/java/com/amazonaws/serverless/proxy/spring/SpringLambdaContainerHandler.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,36 @@ public SpringLambdaContainerHandler(Class<RequestType> requestTypeClass,
108108
Timer.stop("SPRING_CONTAINER_HANDLER_CONSTRUCTOR");
109109
}
110110

111+
112+
/**
113+
* Asks the custom web application initializer to refresh the Spring context.
114+
* @param refreshContext true if the context should be refreshed
115+
*/
111116
public void setRefreshContext(boolean refreshContext) {
112117
this.initializer.setRefreshContext(refreshContext);
113118
}
114119

120+
121+
/**
122+
* Returns the custom web application initializer used by the object. The custom application initializer gives you access
123+
* to the dispatcher servlet.
124+
* @return The instance of the custom {@link org.springframework.web.WebApplicationInitializer}
125+
*/
126+
public LambdaSpringApplicationInitializer getApplicationInitializer() {
127+
return initializer;
128+
}
129+
115130
@Override
116131
protected AwsHttpServletResponse getContainerResponse(AwsProxyHttpServletRequest request, CountDownLatch latch) {
117132
return new AwsHttpServletResponse(request, latch);
118133
}
119134

135+
136+
/**
137+
* Activates the given Spring profiles in the application.
138+
* @param profiles A number of spring profiles
139+
* @throws ContainerInitializationException if the initializer is not set yet.
140+
*/
120141
public void activateSpringProfiles(String... profiles) throws ContainerInitializationException {
121142
if (initializer == null) {
122143
throw new ContainerInitializationException(LambdaSpringApplicationInitializer.ERROR_NO_CONTEXT, null);

aws-serverless-java-container-spring/src/test/java/com/amazonaws/serverless/proxy/spring/SpringAwsProxyTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.amazonaws.serverless.proxy.internal.testutils.MockLambdaContext;
88
import com.amazonaws.serverless.proxy.spring.echoapp.EchoResource;
99
import com.amazonaws.serverless.proxy.spring.echoapp.EchoSpringAppConfig;
10+
import com.amazonaws.serverless.proxy.spring.echoapp.RestControllerAdvice;
1011
import com.amazonaws.serverless.proxy.spring.echoapp.UnauthenticatedFilter;
1112
import com.amazonaws.serverless.proxy.spring.echoapp.model.MapResponseModel;
1213
import com.amazonaws.serverless.proxy.spring.echoapp.model.SingleValueModel;
@@ -55,6 +56,20 @@ public void clearServletContextCache() {
5556
AwsServletContext.clearServletContextCache();
5657
}
5758

59+
@Test
60+
public void controllerAdvice_invalidPath_returnAdvice() {
61+
AwsProxyRequest request = new AwsProxyRequestBuilder("/echo2", "GET")
62+
.json()
63+
.header(CUSTOM_HEADER_KEY, CUSTOM_HEADER_VALUE)
64+
.build();
65+
66+
AwsProxyResponse output = handler.proxy(request, lambdaContext);
67+
assertNotNull(output);
68+
assertEquals(404, output.getStatusCode());
69+
validateSingleValueModel(output, RestControllerAdvice.ERROR_MESSAGE);
70+
71+
}
72+
5873
@Test
5974
public void headers_getHeaders_echo() {
6075
AwsProxyRequest request = new AwsProxyRequestBuilder("/echo/headers", "GET")

aws-serverless-java-container-spring/src/test/java/com/amazonaws/serverless/proxy/spring/echoapp/EchoSpringAppConfig.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public SpringLambdaContainerHandler springLambdaContainerHandler() throws Contai
3636
// update the registration to map to a path
3737
registration.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, "/echo/*");
3838
// servlet name mappings are disabled and will throw an exception
39+
40+
handler.getApplicationInitializer().getDispatcherServlet().setThrowExceptionIfNoHandlerFound(true);
3941
});
4042

4143
return handler;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.amazonaws.serverless.proxy.spring.echoapp;
2+
3+
4+
import com.amazonaws.serverless.proxy.spring.echoapp.model.SingleValueModel;
5+
6+
import org.springframework.http.HttpStatus;
7+
import org.springframework.http.ResponseEntity;
8+
import org.springframework.web.bind.annotation.ControllerAdvice;
9+
import org.springframework.web.bind.annotation.ExceptionHandler;
10+
import org.springframework.web.bind.annotation.RequestMapping;
11+
import org.springframework.web.bind.annotation.ResponseBody;
12+
import org.springframework.web.servlet.NoHandlerFoundException;
13+
14+
15+
@ControllerAdvice
16+
@RequestMapping(produces = "application/json")
17+
@ResponseBody
18+
public class RestControllerAdvice {
19+
public static final String ERROR_MESSAGE = "UnhadledPath";
20+
21+
@ExceptionHandler(NoHandlerFoundException.class)
22+
public ResponseEntity<SingleValueModel> unhandledPath(final NoHandlerFoundException e) {
23+
SingleValueModel model = new SingleValueModel();
24+
model.setValue(ERROR_MESSAGE);
25+
return new ResponseEntity<SingleValueModel>(model, HttpStatus.NOT_FOUND);
26+
}
27+
}

0 commit comments

Comments
 (0)