Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@PathVariable issue #71

Closed
Sithira opened this issue May 17, 2020 · 7 comments · Fixed by #75
Closed

@PathVariable issue #71

Sithira opened this issue May 17, 2020 · 7 comments · Fixed by #75

Comments

@Sithira
Copy link

Sithira commented May 17, 2020

Hi, I'm trying to use the path variable feature like this, but it throws

net.kaczmarzyk.spring.data.jpa.web.InvalidPathVariableRequestedException: Requested path variable {parentId} is not present in Controller request mapping annotations

What I'm trying
@RequestMapping(method = RequestMethod.GET, path = "/{parentId}/translations/{translationId}/meta/search") public ResponseEntity<List<MetaDetailResponseDto>> search( @Conjunction(value = { @Or(value = @Spec(path = "metaType", params = {"meta_type"}, spec = Equal.class)) }, and = @Spec(path = "entity_id", pathVars = "parentId", spec = Equal.class)) Specification<MetaDetail> specification) { return ResponseEntity.status(HttpStatus.OK).body(this.adminDestinationTranslationMetaDetailService.search(specification)); }

it is an issue or am I doing anything wrong?

Kind Regards,
Sithira

@Sithira
Copy link
Author

Sithira commented May 17, 2020

Screenshot 2020-05-17 at 12 56 16

A bit readable way

@jradlica
Copy link
Collaborator

jradlica commented Jun 3, 2020

Hi @Sithira, could you provide more details or a more detailed stacktrace?

I could not reproduce your error. Code which I used to reproduction (test shows that everything should work):

package net.kaczmarzyk;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import net.kaczmarzyk.spring.data.jpa.Application;
import net.kaczmarzyk.spring.data.jpa.web.annotation.*;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import net.kaczmarzyk.spring.data.jpa.Customer;
import net.kaczmarzyk.spring.data.jpa.CustomerRepository;
import net.kaczmarzyk.spring.data.jpa.domain.Equal;
import org.springframework.web.context.WebApplicationContext;

import javax.persistence.*;

@Transactional
@RunWith(SpringRunner.class)
@EnableAutoConfiguration
public class Issue71 extends TestBase {

	@Controller
	public static class TestController {

		@Autowired
		TestEntityRepository repository;

		@RequestMapping(method = RequestMethod.GET, value = "/{parentId}/translations/{translationId}/meta/search")
		@ResponseBody
		public Object search(
				@Conjunction (value = {
						@Or(value = @Spec(path = "metaType", params = {"meta_type"}, spec = Equal.class))},
						and = @Spec(path = "entity_id", pathVars = "parentId", spec = Equal.class)) Specification<TestEntity> spec) {
			return repository.findAll(spec);
		}
	}

	@Autowired
	TestEntityRepository repository;

	@Before
	public void setupDb() {
		repository.save(new TestEntity("metaType1", "parentId1Value"));
		repository.save(new TestEntity("metaType1", "parentId2"));
	}

	@Test
	public void test() throws Exception {
		mockMvc.perform(get("/parentId1Value/translations/translationIdIgnoredValue/meta/search")
				.accept(MediaType.APPLICATION_JSON))
			.andExpect(status().isOk())
			.andExpect(jsonPath("$").isArray())
			.andExpect(jsonPath("$[0].entityId").value("parentId1Value"))
			.andExpect(jsonPath("$[1]").doesNotExist());
	}

}

@Configuration
@ComponentScan
@EnableAutoConfiguration
@WebAppConfiguration
@EnableJpaRepositories
class TestBase {
	@PersistenceContext
	protected EntityManager em;

	@Autowired
	WebApplicationContext wac;

	MockMvc mockMvc;

	@Before
	public void setup() {
		mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
	}

	@Entity
	public class TestEntity {
		@Id @GeneratedValue
		private Long id;
		private String metaType;
		private String entity_id;

		public TestEntity(String metaType, String entity_id) {
			this.metaType = metaType;
			this.entity_id = entity_id;
		}

		public String getMetaType() {
			return metaType;
		}

		public String getEntityId() {
			return entity_id;
		}
	}

}

@Repository
interface TestEntityRepository extends JpaRepository<TestBase.TestEntity, Long>, JpaSpecificationExecutor<TestBase.TestEntity> {
}

@Sithira
Copy link
Author

Sithira commented Jun 4, 2020

Hi @jradlica,

This is the stack trace you asked for :-)

net.kaczmarzyk.spring.data.jpa.web.InvalidPathVariableRequestedException: Requested path variable {parentId} is not present in Controller request mapping annotations at net.kaczmarzyk.spring.data.jpa.web.WebRequestProcessingContext.getPathVariableValue(WebRequestProcessingContext.java:68) at net.kaczmarzyk.spring.data.jpa.web.SimpleSpecificationResolver.resolveSpecArgumentsFromPathVariables(SimpleSpecificationResolver.java:148) at net.kaczmarzyk.spring.data.jpa.web.SimpleSpecificationResolver.resolveSpecArguments(SimpleSpecificationResolver.java:135) at net.kaczmarzyk.spring.data.jpa.web.SimpleSpecificationResolver.buildSpecification(SimpleSpecificationResolver.java:61) at net.kaczmarzyk.spring.data.jpa.web.ConjunctionSpecificationResolver.buildSpecification(ConjunctionSpecificationResolver.java:66) at net.kaczmarzyk.spring.data.jpa.web.ConjunctionSpecificationResolver.resolveArgument(ConjunctionSpecificationResolver.java:54) at net.kaczmarzyk.spring.data.jpa.web.ConjunctionSpecificationResolver.resolveArgument(ConjunctionSpecificationResolver.java:36) at net.kaczmarzyk.spring.data.jpa.web.SpecificationArgumentResolver.resolveArgument(SpecificationArgumentResolver.java:51) at net.kaczmarzyk.spring.data.jpa.web.SpecificationArgumentResolver.resolveArgument(SpecificationArgumentResolver.java:63) at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121) at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:167) at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:134) at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:879) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793) at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) at javax.servlet.http.HttpServlet.service(HttpServlet.java:645) at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) at javax.servlet.http.HttpServlet.service(HttpServlet.java:750) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:748)

and I have posted a photo of what my controller method looks like. Kindly tell me what I'm i doing wrong here?

@tkaczmarzyk
Copy link
Owner

@Sithira please retry with v2.3.0 (just released)

@tkaczmarzyk tkaczmarzyk reopened this Jun 5, 2020
@tkaczmarzyk
Copy link
Owner

(assuming that fixes in 2.3.0 solved this -- feel free to reopen if your issue persists, though)

@YoliNikolova
Copy link

Hello, i still have this problem with v2.6.2, did you find a solution? (problem with PathVariable)

@alphana-dev
Copy link

+++ Hello, i still have this problem with v2.6.2, did you find a solution? (problem with PathVariable)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants