2

I created interface:

@RequestMapping("/data")
public interface PersonControllerInterface {

    @RequestMapping(value="/person/{id}", method=RequestMethod.GET)
    public Person getPersonDetail(@PathVariable final Integer id);

}

and class which implements that interface:

@RestController
public class PersonController implements PersonControllerInterface {

    @Override
    public Person getPersonDetail(final Integer id) {
        final Person p = new Person(id);
        return p;
    }

}

But after I call GET localhost:8080/data/person/4. I got:

DEBUG o.s.web.servlet.DispatcherServlet - Could not complete request
java.lang.NullPointerException: null
at com.concretepage.Person.<init>(Person.java:12)
at com.concretepage.PersonController.getPersonDetail(PersonController.java:16)

And the Person class:

public Person(final Integer id) { this.id = id; }

In jax-rs I can do similar stuff, but in Spring it can't fetch id in path. Are there any solutions on my problem? I really don't want put @PathVariable annotation in my implementation class.

EDIT: It is not duplicate of fixing NPE. It is a problem with propagating annotations from interface class into implementation.

ByeBye
  • 6,650
  • 5
  • 30
  • 63
  • 1
    Can you include the full stack trace? – spa Jan 04 '16 at 12:08
  • Share PersonService (line 12?) – Jan Jan 04 '16 at 12:16
  • Why would you annotate an interface? – Stefan Jan 04 '16 at 12:19
  • http://pastebin.com/rRDkKYDD – ByeBye Jan 04 '16 at 12:20
  • 1
    public Person(final Integer id) { this.id = id; } It isn't a problem with constructor, it's problem with that id is null – ByeBye Jan 04 '16 at 12:33
  • 3
    Why is this question is duplicate of another: This question already has an answer here: What is a NullPointerException, and how do I fix it? 12 answers. The last is common java question, but current relates to spring-mvc – Timur Milovanov Jan 09 '17 at 12:54
  • 1
    No, because it is not a problem with NPE, it is a problem that annotations from interface are not populated into concrete class – ByeBye Dec 05 '17 at 13:18
  • @ByeBye you should demonstrate how you've eliminated a simple error in your `Person` class. Perhaps an example of the same set of annotations placed on a single class that work as expected. If it's a problem with annotations, I'd expect the parameter itself to be null. If that's the case, highlight it as soon as it's visible. The stack trace with the NPE might just hide what's relevant. – toniedzwiedz Jan 16 '18 at 16:28

1 Answers1

-3

You are binding Rest interface with your application interface ... I can't imagine situation in which this solution would be helpful. Just make request mapping in simple class (so it's simple to find out where the request ends) and if you need to change the implementation just choose one in there (or use some DI if you want).

EDIT: Ok, there are tools that can generate interface, anyway this looks like duplicate https://stackoverflow.com/a/8005644/2037762

Community
  • 1
  • 1
Flowy
  • 420
  • 1
  • 5
  • 15
  • I want to generate interface for each endpoint from swagger codegen. After that I want to implements interfaces to my controller class and override methods from there. In this point I dont want to rewrite any annotations – ByeBye Jan 04 '16 at 12:25
  • 2
    When developing a webservice, it's useful to have a JAR containing the API (i.e. the interfaces and model objects) and another JAR containing the implementation. And you don't want to duplicate the annotations, so I totally agree with the need of OP. – Gaël J Jan 04 '16 at 12:27
  • my example which works in jax-rs http://pastebin.com/M1HZLbti – ByeBye Jan 04 '16 at 12:30
  • So the answer is "no, you can't"? – ByeBye Jan 04 '16 at 12:45
  • yeah, for me its same case as in the link – Flowy Jan 04 '16 at 14:32