Zach looks like a controller:

@PreAuthorize("hasAuthority('CONFIGURATION_SET_BASE_URL')") @RequestMapping(value = "/baseUrl/{tBaseUrl:.*}", method = RequestMethod.PUT) public ResponseEntity setBaseUrl(@PathVariable String tBaseUrl) throws IOException, AuthenticationException, JAXBException {} 

Here is the test:

 String baseUrl = "http://localhost:" + 40080 + "/newurl"; String url = getContextBaseUrl() + "/configuration/strongbox/baseUrl/" + baseUrl; RestAssuredMockMvc.given() .contentType(MediaType.TEXT_PLAIN_VALUE) .body(baseUrl) .when() .put(url) .peek() // Use peek() to print the ouput .then() .statusCode(200) .extract(); 

The test fails. Returns status 404 Here is the spectra:

 java.lang.AssertionError: 1 expectation failed. Expected status code <200> doesn't match actual status code <404>. at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:80) at org.codehaus.groovy.reflection.CachedConstructor.doConstructorInvoke(CachedConstructor.java:74) at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrap.callConstructor(ConstructorSite.java:84) at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:60) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:235) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:247) at com.jayway.restassured.internal.ResponseSpecificationImpl$HamcrestAssertionClosure.validate(ResponseSpecificationImpl.groovy:584) at com.jayway.restassured.internal.ResponseSpecificationImpl$HamcrestAssertionClosure$validate.call(Unknown Source) at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125) at com.jayway.restassured.internal.ResponseSpecificationImpl.validateResponseIfRequired(ResponseSpecificationImpl.groovy:738) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite$PogoCachedMethodSiteNoUnwrapNoCoerce.invoke(PogoMetaMethodSite.java:210) at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.callCurrent(PogoMetaMethodSite.java:59) at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:52) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:154) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:166) at com.jayway.restassured.internal.ResponseSpecificationImpl.statusCode(ResponseSpecificationImpl.groovy:107) at com.jayway.restassured.specification.ResponseSpecification$statusCode$0.callCurrent(Unknown Source) at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:52) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:154) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:166) at com.jayway.restassured.internal.ResponseSpecificationImpl.statusCode(ResponseSpecificationImpl.groovy:115) at com.jayway.restassured.internal.ValidatableResponseOptionsImpl.statusCode(ValidatableResponseOptionsImpl.java:115) at org.carlspring.strongbox.rest.ConfigurationManagementControllerTest.testSetAndGetBaseUrl(ConfigurationManagementControllerTest.java:128) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27) at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86) at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:254) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) at org.junit.runners.ParentRunner.run(ParentRunner.java:309) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:193) at org.junit.runner.JUnitCore.run(JUnitCore.java:160) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:253) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) 

  • Add a simple route like @RequestMapping(value = "/foo}", method = RequestMethod.PUT) and test. Then add @RequestMapping(value = "/foo/{bar}", method = RequestMethod.PUT) and set the @PathVariable String bar in the parameters of the method. Test it. And then see what happens if you declare @PathVariable String tBaseUrl:.* in your method instead of @PathVariable String tBaseUrl . - DimXenon
  • In the latter case, there will most likely be complaints. I think you need to pull out the quantifiers for braces in value = "/baseUrl/{tBaseUrl:.*}" - like this: value = "/baseUrl/{tBaseUrl}:.*" - DimXenon

2 answers 2

I think you need to pull out the quantifiers with curly braces in value = "/baseUrl/{tBaseUrl:.*}" - like this: value = "/baseUrl/{tBaseUrl}:.*"

To catch the port where the route was accessed, use @Value("${local.ser‌​ver.port}") .

But before that you need to allow access to the server on any port through the destination server.port=0 in /src/main/resources/‌​application.properti‌​es . (Read more here ) (Alternative: Run Configuration -> VM Option -Dserver.port=0 )

Allowed a call on any port. OK. Now, please define a field in your class @Value("${local.ser‌​‌​ver.port}") private String myPort; type such a route in the annotation: @RequestMapping(val‌​ue = "/foo", method = RequestMethod.PUT) and in the method body for the route write the output of myPort . Next, contact your server at http://localhost:40‌​080/foo . See what the output will be.

  • No, it did not help. Even different combinations made like: {tBaseUrl}:. * {TBaseUrl:}. *, Etc. And here it is stated that I am doing syntactically correct docs.spring.io/spring/docs/current/spring-framework-reference/ ... But why it does not work - Yury Grishechkin
  • And if you remove the colon from the template? Just the port is caught separately, sort of like. And to do this: value = "/baseUrl/{tBaseUrl}.*" - DimXenon
  • Does not matter. Yes, and it should be there. no, everything goes in one line. So did. Differently - syntax error. - Yury Grishechkin
  • You catch the port. Then it's not about the way, but at the port where the server is deployed. See which ports your application server listens to. Perhaps you first need to set the range of ports in the server settings on which you want to listen to customer requests. And then - see how to organize work on the ports. - DimXenon
  • The port is caught by the string. And then the service works with it. It has nothing to do with the request request. See what the full address looks like: localhost: 48080 / configuration / strongbox / baseUrl / http: // ... - Yury Grishechkin

And what answer will be if you open such an address in the browser? I suspect that the fact is that you specified the content type as text/plain , but the controller expects text/html . Try adding the consumes attribute to @RequestMapping , for example.

  • Added consumes = MediaType.TEXT_PLAIN_VALUE and did not help - Yury Grishechkin