【问题标题】:Getting response 404 on non-root routes在非根路由上获得响应 404
【发布时间】:2021-07-11 17:27:36
【问题描述】:

1.当我尝试向“/families”路由发送 GET 请求时,我的应用程序以 404 响应我。

控制器:

package com.example.controller;

import com.example.domain.*;
import com.example.repository.*;
import com.example.view.*;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping(consumes = "application/json", produces = "application/json")
public class FamilyController {
    private FamilyRepository familyRepository;

    public FamilyController(FamilyRepository familyRepo) {
        this.familyRepository = familyRepo;
    }

    @GetMapping("/")
    public String getFamily(){
        List<Family> families = familyRepository.findAll();

        ObjectMapper mapper = new ObjectMapper();
        mapper.disable(MapperFeature.DEFAULT_VIEW_INCLUSION);

        String result = "";
        try {
            result = mapper
                    .writerWithView(FamiliesView.class)
                    .writeValueAsString(families);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        return result;
    }
}

主类:

package com.example;

import com.example.controller.*;
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.context.annotation.*;

@SpringBootApplication
public class ConstantaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConstantaServerApplication.class, args);
    }
}

2.但是当我尝试在控制器注释和 GET 请求中将路由更改为“/”时,我得到了

"message": "Content type '' not supported"

映射器工作正常。

【问题讨论】:

    标签: java spring spring-boot spring-mvc


    【解决方案1】:

    您的映射不正确。

    使用控制器上的@RequestMapping 注释为整个控制器设置/families 前缀:

    @RestController
    @RequestMapping(value = "/families", consumes = "application/json", produces = "application/json")
    public class FamilyController {
        @GetMapping("/")
        public String getFamily(){...}
    }
    

    或者在你的getFamily方法上调整@GetMapping注解

    @RestController
    @RequestMapping(consumes = "application/json", produces = "application/json")
    public class FamilyController {
        @GetMapping("/families")
        public String getFamily(){...}
    }
    

    或通过将配置属性server.servlet.context-path 设置为值/families 来注册整个应用程序上下文路径前缀/families

    【讨论】:

      【解决方案2】:

      在上面的代码中,您可以从控制器级别删除 @RequestMapping。

      @RequestMapping(consumes = "application/json", produces = "application/json")
      

      控制器应该如下所示。

      @RestController
      public class FamilyController {
      

      要了解有关该行为的更多信息,您可以遵循一些教程,例如 baeldung

      【讨论】:

        猜你喜欢
        • 2021-07-27
        • 2014-01-10
        • 1970-01-01
        • 2016-08-27
        • 2013-08-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-14
        • 2019-06-10
        相关资源
        最近更新 更多