繁体   English   中英

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

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

我正在使用Spring Boot实现rest服务。 实体类在单独的程序包中定义。 因此,我在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);
    }
}

这是我的控制器类:

// 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);
    }
}

我还创建了一个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);
}

下面是实现服务接口的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;
    }
}

当我启动应用程序并在浏览器中输入以下URL“ http:// localhost:8080 / api / seqs ”时,出现404错误。 我错过了什么?

编辑#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);

    }
}

当我启动应用程序并将“ localhost:8080 / api / greetings”放入浏览器时,我仍然得到404。

如果您的pom.xml中没有spring boot starter web,则添加相同的内容,原因可能是代码无法映射端点。

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

==>您确定您的Spring Boot应用程序类和Rest Controller是否在同一基本包中? 例如,如果您的Spring Boot应用程序类的软件包是com.example.demo,则您的Rest Controller应该与com.example.demo.controller位于同一基本软件包中。

==>我认为这是引导无法映射到Rest Controller的uri的原因。 因为@SpringBootApplication已经嵌入了@ComponentScan和@Configuration。 尝试这样做。 我希望它能起作用。

我要尝试的第一件事是将@RequestMapping("/")放在控制器的类定义上。 在方法上保持相同的值。

与您的问题无关的另一件事是,您无需定义该自定义查询。 JPA实际上很聪明,仅使用该方法名即可执行您定义的查询。 在此处查看findByLastName示例: https : findByLastName

暂无
暂无

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

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