简体   繁体   中英

Getting response 404 on non-root routes

1.When I try to send GET request to "/families" route my application responses me with 404.

Controller:

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

Main class:

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.But when I try to change route to "/" both in Controller annotation and GET request I'm getting

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

Mapper work properly.

You have incorrect mapping.

Either set /families prefix for whole controller using @RequestMapping annotation on controller:

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

or adjust @GetMapping annotation on your getFamily method

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

or register for whole application context path prefix /families by setting config property server.servlet.context-path to value /families

In your code above you can remove @RequestMapping from the controller level.

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

The controller should look like this.

@RestController
public class FamilyController {

To learn more about the behavior you can follow some tutorials like baeldung

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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