I have setup a service with spring boot but I can't call my rest endpoint.
CODE:
#RestController
#RequestMapping("/route")
#SessionAttributes("session_userId")
public class RoutingController extends BaseController implements WebMvcConfigurer{
Endpoint:
#RequestMapping("/users/web")
public ModelAndView routeToUserInterfaceServiceWeb(HttpSession session,Device device) {
The service runs on port 8000. So when I do in my browser:
"localhost:8000/route/users/web" I expect a any answer of the server. But all I get is a 404.
Try removing the controller annotation:
#RequestMapping("/route")
Keep the original annotation on the method:
#RequestMapping("/route/users/web")
ModelAndView is a Spring MVC construction. I would expect a REST service to return a ResponseEntity.
Related
I have a health check controller like the below one:
#RestController
#RequestMapping("/api/${app-name}/management")
public class HealthCheckController {
#GetMapping(path = "health", produces = MediaType.TEXT_PLAIN_VALUE)
public Mono<String> healthCheck() {
return Mono.just("Ok");
}
}
I have this controller in a common library and this library is included in all our services. In each service properties, I have given the value for app-name. So the URLs like http://host:port/api/service1/management/health will return Ok. This is working fine on my local machine but on the server, getting a 404 error. Our services are deployed in Kubernetes.
Am I missing anything here? Was the value binding in #RequestMapping (${app-name}) is correct usage?
I am currently trying to shift my application from spring boot 1.5.x to 2.x.x on reactive stack. I am facing a kinda weird problem that I can't figure out. Hope someone knows the solution to this.
I implemented an api to receive a user jwt token as "Authorization" field on the header. The api is a POST method that receives a certain json data from the user in the body, goes to the backend and processes it.
The unfortunate thing is i keep getting a http 404 error when i add in the header, a normal 200 when i remove it in postman.
Here is my controller.
#RestController
#RequestMapping("/user")
#Slf4j
public class UserHandler {
#Autowired
private UserService service;
#Autowired
private Utility utility;
#PostMapping("/updateLink")
public Mono<ServerResponse> addNewAccountLinkAPI(#RequestHeader(name="Authorization") String id, #RequestBody UpdateAccountLink request){
return Mono.just(request)
.flatMap(s -> service.addNewAccountLink(s))
.flatMap(s -> ok().body(BodyInserters.fromObject(new RespWrap("Success", new Date(), null, s))))
.switchIfEmpty(badRequest().body(BodyInserters.fromObject(new RespWrap("Failed", new Date(), "Failed to create new link", null))));
}
}
Here is my simple security config
#Configuration
#EnableWebFluxSecurity
#EnableWebFlux
public class ResourceServerConfig implements WebFluxConfigurer {
#Bean
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http, FirebaseAuthenticationManager manager) {
http
.authorizeExchange().anyExchange().permitAll()
.and().csrf().disable();
return http.build();
}
}
Can anyone please point me out on the problem. This sure seems like a lack of config problem.
I can see two issues with your code snippets.
First, you shouldn't add #EnableWebFlux as it completely disables the auto-configuration done by Spring Boot. Same goes for #EnableWebMvc in a Spring MVC application.
Second, you're mixing WebFlux annotations and WebFlux functional. The annotations you're using are fine, but the ServerResponse type should only be used when writing functional handlers. You should try instead here to use ResponseEntity.
I created a REST web service using Spring Boot.
The project is configured to use an external Tomcat.
When using Tomcat 8 it connects successfully and get good responses when i call my GET methods.
However, the POST methods return 404 errors.
#RestController
#RequestMapping("/hashkey")
#EnableAutoConfiguration
public class StringHashingController {
#RequestMapping(method=RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public #ResponseBody HashedStringsList hashMe(#RequestParam(value="caller", required=true) String callerId, #RequestBody StringHashingRequestBodyParams hashParam) {
HashedStringsList pList = new HashedStringsList();
//...
return pList;
}
}
I have no problem calling the same post method locally in the IDE (Intellij) through running 'Spring Boot Configuration'.
Is there anything I need to configure in Tomcat or in my code?
Thanks!
I am creating a spring RESTful service, and it is working. My client send an object into a put request and my service receive this object perfectly, but after this, my client receive this exception: "org.springframework.web.client.HttpClientErrorException: 404 Not Found"
This is my client code:
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
Greeting greeting = new Greeting(21l, FileUtils.toByteArray("src/main/resources/test.png"));
String url = "http://localhost:8080/DolphinRestServer/bulletin/put";
restTemplate.put(url,greeting);
And this is my server code:
#Service
#RequestMapping("/bulletin")
public class BulletinService {
#RequestMapping(method = RequestMethod.PUT, value = "/put")
public void put(#RequestBody Greeting greeting){
System.out.println("it's work fine and my greeting is here --------->"+greeting);
}
}
When tested, i get those messages:
-server side:
-Client side:
Your put method has a void return type. Additionally, it does not take the HttpServletResponse as a parameter. According to the Spring MVC documentation, when such a situation arises, Spring MVC uses the URL as the view name and attempts to render it. In your case, Spring MVC is attempting to find and load a view called /bulletin/put, which it can't find and hence the 404 message.
As you can see it is making another request at 20:40:34:085 DolphinRestServer/bulletin/bulletin/put in the server side giving an error page not found.
And it is because you are calling BulletinService twice.
You defined as a bean and it has an annotation. Make sure you are only loading/scanning this package once..
Remove #service annotation on BulletinService
So I am developing a REST API in an application that uses both Spring and Wicket at the same time.
If I annotate #RequestMapping(value="/exchange") at my Spring #Controller annotated class (the one that is acting as a webserver), how do I have to configure Wicket to "recognize" http://myserver.com/myapp/exchange or http://myserver.com/myapp/exchange/onemethod as a valid URL so I don't get a 404 ERROR when I try to call the webservice from a client?
Use the JAX-RS #Path annotation:
#Path("exchange")
#Component
public class ExchangeService {
#POST
#Path("onemethod")
public void oneMethod(...) {
...
}
}