Getting 404 response on spring 4 rest service - spring

I am getting a 404 response from a #RestController returning an object.
Everything seems fine as I get a correct response from another url in the same #RestController, but it is a different class.
I see no exception or any other error in the logs though. Only 404.
Problematic class is defines as follows:
public class Menu implements Serializable {
private static final long serialVersionUID=1L;
private String url;
private List<Menu> submenu;
... getters and setters ...
}
I have no problems with a similar class. Only difference is there is a List of String instead of a List of Menu.
What may be causing the problem?
EDIT:
Didn't want to add too much code to the question.
This is the Controller:
package org.web.ui.controller;
import java.io.IOException;
import java.security.Principal;
import java.util.ArrayList;
import java.util.Collection;
import javax.annotation.Resource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpResponse;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.databind.ObjectMapper;
import mx.org.ife.rfe.siirfe.comun.web.bean.LoginStatus;
import mx.org.ife.rfe.siirfe.comun.web.controller.UserLoginController;
import mx.org.ife.rfe.siirfe.comun.web.ui.model.Menu;
#RestController
#RequestMapping("/menu")
public class menuController {
private LoginStatus loginStatus ;
public menuController() {
ArrayList<String> _roles = new ArrayList<>();
_roles.add("TESTCASE");
loginStatus = new LoginStatus();
loginStatus.setRoles(_roles);
loginStatus.setAnonymous(true);
loginStatus.setError(false);
}
#RequestMapping(value = "/app.do")
public Menu app(HttpResponse response) {
return new Menu();
}
#RequestMapping(value="/test1.do")
public LoginStatus test1(Principal user) {
return loginStatus;
}
}
This is LoginStatus class
public class LoginStatus implements Serializable {
private static final long serialVersionUID = 1L;
private boolean anonymous = true;
private Boolean error;
private String errorMessage;
private List<String> roles;
... Getters and Setters ...
}
This is in the web.xml
<servlet>
<servlet-name>spring-dispatcher-servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:/webApplicationContext.xml,
classpath*:/environmentContext.xml,
classpath*:/daoContext.xml,
classpath*:/menuContext.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-dispatcher-servlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
Now, a get request to hostname/menu/app.do returns a 404 error code while a get request to hostname/menu/test1.do returns a correctly formatted json object.
I ommited initialization of the menu for simplicity.
Example outputs from test with wget:
$ wget wlcap1:9102/siilnere-web/menu/app.do -O -
--2016-12-08 18:16:51-- http://wlcap1:9102/siilnere-web/menu/app.do
Resolving wlcap1... 172.19.94.15
Connecting to wlcap1|172.19.94.15|:9102... connected.
HTTP request sent, awaiting response... 404 Not Found
2016-12-08 18:16:51 ERROR 404: Not Found.
06:16 PM
$ wget wlcap1:9102/siilnere-web/menu/test1.do -O -
--2016-12-08 18:16:55-- http://wlcap1:9102/siilnere-web/menu/test1.do
Resolving wlcap1... 172.19.94.15
Connecting to wlcap1|172.19.94.15|:9102... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [application/json]
Saving to: 'STDOUT'
2016-12-08 18:16:55 (7.62 MB/s) - written to stdout [101]
06:16 PM
$ wget wlcap1:9102/siilnere-web/menu/test1.do -O - 2>/dev/null
{"roles":["EJEMPLO"],"token":null,"anonymous":true,"error":false,"errorMessage":null,"username":null}

In your app(...) controller method, I think that HttpResponse is not a valid param. It should be ServletResponse or HttpServletResponse. You can refer to the list of accepted param types of a controller method here: http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestMapping.html

Related

How to use Spring Security with load balancer?

I am new at loadBalancing so please I need help and thats what i did :
i built 2 services as 2 apps (A,B) I used spring security on both of them
(both of them are restfull api , they have theymleaf and full frontEnd pages ),
then i had made another app as spring cloud loadbalancer .
when i send a request , it go from loadbalancer app to one of the 2 services but the problem is when iam not authenticated the response will be empty , it wont take me to the default login page as usual as when i use the normal A app directly , and when i go to pages that does not need to be authenticated to get to it , it is returned without my css/js styles
this is my A app controller ( it is returning view not json )
package com.hariri_stocks.controllers;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.hariri_stocks.models.Estates;
import com.hariri_stocks.models.SoldEstates;
import com.hariri_stocks.models.Users;
import com.hariri_stocks.services.estatesService;
#Controller
public class LoginController {
#Autowired
estatesService ES;
#GetMapping(value = "/")
public String login() {
return "/signIn-up.html";
}
#GetMapping(value = "/dashboard")
public String dashboard(Model model ,#RequestParam(required = false) String add_result
,#RequestParam(required = false) String alert_err) {
List<Estates> estates = ES.findAll();
model.addAttribute("estates",estates);
return "/dashboard";
}
#GetMapping(value = "/dashboard/unSold")
public String unselled_stocks(Model model) {
List<Estates> estates = ES.findUnsold();
if(estates.size() > 0)
model.addAttribute("estates",estates);
else
model.addAttribute("error","there is no sold estates yet !!");
return "/dashboard";
}
#Value(value = "${server.port}")
String port_num;
#GetMapping("/port")
public String hello() {
return port_num;
}
}
and this is my loadbalancer controller iam using #restcontroller
package com.hariri_loadbalancer;
import reactor.core.publisher.Mono;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.reactive.ReactorLoadBalancerExchangeFilterFunction;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.function.client.WebClient;
#SpringBootApplication
#RestController
public class UserApplication {
private final WebClient.Builder loadBalancedWebClientBuilder;
private final ReactorLoadBalancerExchangeFilterFunction lbFunction;
public UserApplication(WebClient.Builder webClientBuilder,
ReactorLoadBalancerExchangeFilterFunction lbFunction) {
this.loadBalancedWebClientBuilder = webClientBuilder;
this.lbFunction = lbFunction;
}
public static void main(String[] args) {
SpringApplication.run(UserApplication.class, args);
}
#RequestMapping("/port")
public Mono<String> showMePort() {
return loadBalancedWebClientBuilder.build().get().uri("http://hariri/port")
.retrieve().bodyToMono(String.class);
}
#RequestMapping("/")
public Mono<String> showMainPage() {
return loadBalancedWebClientBuilder.build().get().uri("http://hariri/")
.retrieve().bodyToMono(String.class);
}
}
So what should I do? I feel that what I am doing is stupid,
should I move all my Thymleaf pages to the loadbalancer maybe , so that the a app return what it want to return with #restController then the loadbalancer use #controller to get to the styling front pages or there is a way , and for the security , should i implement the spring security with the loadbalancer instead of the A,B apps
.........................
8080 is loadBalancer port
9091 is A app port
so it seams that when A is returning the html page , the html is searching for the css at the loadbalancer machin at 8080 , while they are existing at A app on 9091
bodyToMono decodes the body but you are not handling headers.
On spring security there is very likely a redirection to the login page ... so it wont work if you only attend to the body. This might be also affecting styles somehow.
Check something like this:
How to extract response header & status code from Spring 5 WebClient ClientResponse

I am trying to get Header info from Request Controller and read into IntegrationFlow

I wanted to understand where is best location to read headers and use them inside my IntegrationFlow layer.
ServiceController.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
#RestController
#RequestMapping("/api/v1/integration")
public class ServiceController {
#Autowired
private ServiceGateway gateway;
#GetMapping(value = "info")
public String info() {
return gateway.info();
}
}
ServiceGateway.java
import org.springframework.integration.annotation.Gateway;
import org.springframework.integration.annotation.MessagingGateway;
#MessagingGateway
public interface ServiceGateway {
#Gateway(requestChannel = "integration.info.gateway.channel")
public String info();
}
ServiceConfig.java
import java.net.URISyntaxException;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.integration.annotation.IntegrationComponentScan;
import org.springframework.integration.config.EnableIntegration;
import org.springframework.integration.dsl.IntegrationFlow;
import org.springframework.integration.dsl.IntegrationFlows;
import org.springframework.integration.http.dsl.Http;
import org.springframework.messaging.MessageHeaders;
#Configuration
#EnableIntegration
#IntegrationComponentScan
public class ServiceConfig {
#Bean
public IntegrationFlow info() throws URISyntaxException {
String uri = "http://localhost:8081/hellos/simpler";
return IntegrationFlows.from("integration.info.gateway.channel")
.handle(Http.outboundGateway(uri).httpMethod(HttpMethod.POST).expectedResponseType(String.class)).get();
}
}
From Consumer I am receiving some Header meta data. I want to know in above flow whether it is good idea from following approaches:
Read headers in Controller and then pass through into my IntegrationFlow: For this I am not aware how to pass through.
Is there best or any way exist to read request headers into IntegrationFlow layer?
For this second approach I have tried below code but runtime I am getting error as channel is one way and hence stopping the flow.
return IntegrationFlows.from("integration.info.gateway.channel").handle((request) -> {
MessageHeaders headers = request.getHeaders();
System.out.println("-----------" + headers);
}).handle(Http.outboundGateway(uri).httpMethod(HttpMethod.POST).expectedResponseType(String.class)).get();
My problem is how to send request parameters from incoming call to carry those internally invoking another rest call. Here I wanted to transform the data from request headers and construct into new json body and then send this to http://localhost:8081/hellos/simpler URL.
The flow:
I am trying to construct this RequestBody before sending to internal REST POST call:
A gateway method with no paylaod is for receiving data, not requesting it.
https://docs.spring.io/spring-integration/docs/current/reference/html/messaging-endpoints.html#gateway-calling-no-argument-methods
Add a #Header annotated parameter to the gateway.
https://docs.spring.io/spring-integration/docs/current/reference/html/messaging-endpoints.html#gateway-configuration-annotations
#MessagingGateway
public interface ServiceGateway {
#Gateway(requestChannel = "integration.info.gateway.channel")
public String info("", #Header("x-api") String xApi);
}
This will send a message with an empty string as the payload with the header set.

How to make Spring boot CSV message converter display CSV inline and not download when using a browser

I created a spring starter project in eclipse . Most of the code was from this link https://www.logicbig.com/tutorials/spring-framework/spring-web-mvc/csv-msg-converter.html.
I added content negotiation configuration to accept headers, path extension and parameters. It works great from postman.
But when I try in a browser http://localhost:8080/employeelist.csv. In all the cases CSV is getting downloaded in a file. I want it displayed inline on the browser. I tried to set content disposition as inline in Request mapping, http output message header but still CSV is always getting downloaded.
What should I be doing to get csv displayed inline? I had previously successfully displayed CSV inline in a browser by having separate request mapping method for CSV and make the method return void and accept httpservletresponse as parameter. But I want to use content negotiation and a single method for all formats - XML, CSV, json. Whatever format selected should be displayed inline in the browser.
Is that possible ?
Thanks a lot for your time.
Update : added portions of code which were edited
package ti.projects;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import java.util.List;
#SuppressWarnings("deprecation")
#EnableWebMvc
#Configuration
#ComponentScan("ti.projects")
public class AppConfig extends WebMvcConfigurerAdapter {
#Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(new CsvHttpMessageConverter<>());
}
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.favorPathExtension(true).favorParameter(true).parameterName("mediaType").ignoreAcceptHeader(false)
.useJaf(false).mediaType("json", MediaType.APPLICATION_JSON)
.mediaType("csv", new MediaType("text", "csv"));
}
}
package ti.projects;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import java.util.Arrays;
import java.util.List;
#Controller
public class ExampleController {
#RequestMapping(
value = "/newEmployee",
consumes = "text/csv",
produces = MediaType.TEXT_PLAIN_VALUE,
method = RequestMethod.POST)
#ResponseBody
#ResponseStatus(HttpStatus.OK)
public String handleRequest (#RequestBody EmployeeList employeeList) {
System.out.printf("In handleRequest method, employeeList: %s%n", employeeList.getList());
String s = String.format("size: " + employeeList.getList().size());
System.out.println(s);
return s;
}
#RequestMapping(
value = "/employeeList",
produces = {"text/csv", "application/json"},
method = RequestMethod.GET
)
#ResponseBody
#ResponseStatus(HttpStatus.OK)
public EmployeeList handleRequest2 () {
List<Employee> list = Arrays.asList(
new Employee("1", "Tina", "111-111-1111"),
new Employee("2", "John", "222-222-2222")
);
EmployeeList employeeList = new EmployeeList();
employeeList.setList(list);
return employeeList;
}
}
package ti.projects;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class ContentNegotiationApplication {
public static void main(String[] args) {
SpringApplication.run(ContentNegotiationApplication.class, args);
}
}
The browser (should) use the provided mime type to decide how to display or process the response. What should work is using a MIME of text/plain to let the browser render the received content as text.
You can set the MIME type of your response in your spring Controller like this:
#GetMapping(produces = MediaType.TEXT_PLAIN_VALUE)
public String renderCsv() {...}
If you want to offer different MIME types with one method you have three options:
Use query parameter (e.g. ...?contentType=json)
Use path parameter (e.g..../{contentType})
Use accept header of client (preferably?)
You can register different MessageConverter for each contentType and configure a ContentNegotiationConfigurer to automatically choose the correct converter depending on given MIME type and your preferences.
I'll try to attach an example tonight.

In spring boot project, When I click submit button, how can I solve Whitelabel Error Page Error,

When I click on submit button I got this error message
Whitelabel Error Page This application has no explicit mapping for
/error, so you are seeing this as a fallback. Tue Jun 30 17:24:02 CST
2015 There was an unexpected error (type=Not Found, status=404). No
message available
Here is my code.
package com.tourpackage.controllers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.tourpackage.model.TourPackage;
import com.tourpackage.repository.TourPackageMongoRepository;
import com.tourpackage.repository.VehicleTypeMongoRepository;
#Controller
public class TourPackageController {
#Autowired
TourPackageMongoRepository packageRepository;
VehicleTypeMongoRepository vehicleTypeRepository;
#RequestMapping("/tourpackage")
public String tourpackage(Model model){
model.addAttribute("packagelist", packageRepository.findAll());
return "index";
}
#RequestMapping("/addNewTour")
public String addNewTour(Model model){
model.addAttribute("packagelist", packageRepository.findAll());
return "tourpack";
}
#RequestMapping(value="/addPackage", method = RequestMethod.POST)
public String addPackage(#ModelAttribute TourPackage tourpack) {
packageRepository.save(tourpack);
return "redirect:tourpackage";
}
}
Spring Boot automatically registers the Basic ErrorController as a Spring Bean when you haven't specified an implementation for ErrorController.
so,
If you want to return customised content for path /error, refer following code:
#RestController
public class MyController implements ErrorController{
private static final String PATH = "/error";
#RequestMapping(value = PATH)
public String error() {
return "Error handling";
}
#Override
public String getErrorPath() {
return PATH;
}
}
Else,
If you want to disable it, you can refer this post:
http://www.logicbig.com/tutorials/spring-framework/spring-boot/disable-default-error-page/

RestyGWT- custom url mapping for spring rest service

I have problem that my Spring Rest Controllers is mapped other way than RestyGWT would like.
My application is on: http://localhost:8080/restgwt/
According to web.xml:
<servlet>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/action-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
My Spring service/controller listen on:
http://localhost:8080/restgwt/service/test
But my RestyGWT service calls this url:
http://localhost:8080/restgwt/restgwt/test
And I don't know how to tell to RestyGWT to change url. Please help.
I know that the simplest solution would be changing in web.xml file servlet url-pattern parameter
from: <url-pattern>/service/*</url-pattern>
to: <url-pattern>/restgwt/*</url-pattern>
but I would like to make RestyGWT to change it's behaviour.
Here paste some additional code:
TestService on GWT side
package pl.korbeldaniel.restgwt.client;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import org.fusesource.restygwt.client.MethodCallback;
import org.fusesource.restygwt.client.RestService;
public interface TestService extends RestService {
#GET
#Path("test")
public void getInfo(MethodCallback<TestPojo> test);
}
TestService on Spring side
package pl.korbeldaniel.restgwt.server;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
#RestController()
public class TestService {
#RequestMapping(value = "test", method = RequestMethod.GET)
public #ResponseBody TestEntity getInfo() {
TestEntity test = new TestEntity();
System.out.println("Hit server for getting _1");
return new TestEntity();
}
}
Reffering to the official documentation:
Configuring service root URLs
There are two ways to configure service root URLs which are appended with the #Path annotation property when building the final service URL. For single service root URL the Defaults.setServiceRoot(String) method can be used. When several services with different service roots are used the #Options annotation is equipped with the serviceRootKey property which can be set to read service root entries provided with the static ServiceRoots.add(String, String) method.
Defaults.setServiceRoot(new Resource( GWT.getModuleBaseURL()).resolve("../rest").getUri());
So my REST path for RestyGWT becomes http://domain-name/myGwtAppModuleName/rest/furtherPath
where furtherPath is javax.ws.rs #Path(..) value
Putting the line directly in GIN ClientModule failed with java.lang.UnsatisfiedLinkError: com.google.gwt.core.client.impl.Impl.getModuleBaseURL()Ljava/lang/String
To avoid error this I've wrapped it up
public class ClientModule extends AbstractPresenterModule {
#Override
protected void configure(){
//your installs and binds here
bind(RestyGwtConfig.class).asEagerSingleton();
}
}
public class RestyGwtConfig {
static {
Defaults
.setServiceRoot(new Resource( GWT.getModuleBaseURL()).resolve("../rest").getUri());
}
}

Resources