简体   繁体   English

为什么我要让404弹簧靴休息一下

[英]Why do I get 404 for rest with spring-boot

I am implementing rest services with Spring Boot. 我正在使用Spring Boot实现rest服务。 The entity classes are defined in a separate package. 实体类在单独的程序包中定义。 So I added that with Component annotation in Application.java. 因此,我在Application.java中添加了组件注释。

@Configuration
@EnableAutoConfiguration
@ComponentScan("org.mdacc.rists.cghub.model")
@EnableJpaRepositories(basePackages = "org.mdacc.rists.cghub.model") 
public class Application 
{
    public static void main( String[] args )
    {
        SpringApplication.run(Application.class, args);
    }
}

Here is my controller class: 这是我的控制器类:

// SeqController.java
@RestController
public class SeqController {

    @Autowired
    private SeqService seqService;
    @RequestMapping(
            value = "/api/seqs", 
            method = RequestMethod.GET, 
            produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<List<SeqTb>> getSeqs() {
        List<SeqTb> seqs = seqService.findAll();

        return new ResponseEntity<List<SeqTb>>(seqs, HttpStatus.OK);
    }
}

I also created a JPA data repository that extends JPARepository in which I added custom query code. 我还创建了一个JPA数据存储库,该存储库扩展了JPARepository,并在其中添加了自定义查询代码。

// SeqRepository.java
@Repository
public interface SeqRepository extends JpaRepository<SeqTb, Integer> {

    @Override
    public List<SeqTb> findAll();

    @Query("SELECT s FROM SeqTb s where s.analysisId = :analysisId")
    public SeqTb findByAnalysisId(String analysisId);
}

Below is the servicebean class that implements a service interface 下面是实现服务接口的servicebean类

// SeqServiceBean.java
@Service
public class SeqServiceBean implements SeqService {
    @Autowired
    private SeqRepository seqRepository;

    @Override
    public List<SeqTb> findAll() {
        List<SeqTb> seqs = seqRepository.findAll();
        return seqs;
    }

    public SeqTb findByAnalysisId(String analysisId) {
        SeqTb seq = seqRepository.findByAnalysisId(analysisId);
        return seq;
    }
}

When I started the application and type the following url in the browser " http://localhost:8080/api/seqs " , I got 404 error. 当我启动应用程序并在浏览器中输入以下URL“ http:// localhost:8080 / api / seqs ”时,出现404错误。 What did I miss? 我错过了什么?

Edit #1: I decided to take out the JPA repository stuff and change the controller class to the following: 编辑#1:我决定取出JPA存储库中的东西,并将控制器类更改为以下内容:

@RestController
//@RequestMapping("/")
public class SeqController {
    private static BigInteger nextId;
    private static Map<BigInteger, Greeting> greetingMap;

    private static Greeting save(Greeting greeting) {
        if(greetingMap == null) {
            greetingMap = new HashMap<BigInteger, Greeting>();
            nextId = BigInteger.ONE;
        }
        greeting.setId(nextId);
        nextId = nextId.add(BigInteger.ONE);
        greetingMap.put(greeting.getId(), greeting);
        return greeting;
    }

    static {
        Greeting g1 = new Greeting();
        g1.setText("Hello World!");
        save(g1);

        Greeting g2 = new Greeting();
        g1.setText("Hola Mundo!");
        save(g2);

    }
    @RequestMapping(
            value = "/api/greetings", 
            method = RequestMethod.GET, 
            produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Collection<Greeting>> getGreetings() {
        Collection<Greeting> greetings = greetingMap.values();
        return new ResponseEntity<Collection<Greeting>>(greetings, HttpStatus.OK);

    }
}

When I started the application and put "localhost:8080/api/greetings" in my browser I still got 404. 当我启动应用程序并将“ localhost:8080 / api / greetings”放入浏览器时,我仍然得到404。

If spring boot starter web is not there in your pom.xml then add the same as the reason could be the code not being able to map the endpoints. 如果您的pom.xml中没有spring boot starter web,则添加相同的内容,原因可能是代码无法映射端点。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

==>Did you make sure that your Spring Boot application class and your Rest Controller are in the same base package? ==>您确定您的Spring Boot应用程序类和Rest Controller是否在同一基本包中? For Example if your package for Spring Boot application class is com.example.demo, then your Rest Controller should be in same base package as com.example.demo.controller. 例如,如果您的Spring Boot应用程序类的软件包是com.example.demo,则您的Rest Controller应该与com.example.demo.controller位于同一基本软件包中。

==>I think that is the reason boot is unable to map to the uri of your rest controller. ==>我认为这是引导无法映射到Rest Controller的uri的原因。 Because @SpringBootApplication has @ComponentScan and @Configuration embedded in it already. 因为@SpringBootApplication已经嵌入了@ComponentScan和@Configuration。 Try doing this. 尝试这样做。 I hope it works. 我希望它能起作用。

The first thing I would try is to put @RequestMapping("/") on the class definition of the controller. 我要尝试的第一件事是将@RequestMapping("/")放在控制器的类定义上。 Keep the same value on the method. 在方法上保持相同的值。

Another thing, unrelated to your problem, is that you do not need to define that custom query. 与您的问题无关的另一件事是,您无需定义该自定义查询。 JPA is actually smart enough to do the query you defined just by using that method name. JPA实际上很聪明,仅使用该方法名即可执行您定义的查询。 Check out the findByLastName example here: https://spring.io/guides/gs/accessing-data-jpa/ . 在此处查看findByLastName示例: https : findByLastName

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM