0
Good evening, I have the code below with the implementation of a service Rest /teste123, and when calling this method from the angular-js code, already authenticated with the basic authentication of spring-boot I have access to this service. When I try to access it from the retrofit2 of my android or the Postman extension of Chrome, the two give error 404. Somebody explain to me what I’m doing wrong. Just to be clear that I am pointing to the correct ip of the pc which is 192.168.... and not to localhost at the time of the calls. Thank you.
@RestController
public class TesteController {
@RequestMapping(value = "/teste123", method = RequestMethod.POST)
public Programa gravaLote2(@RequestBody Programa programa) {
System.out.println(programa);
return new Programa();
}
}
below the example of the angular-js code that works.
$http.post("/teste123", {id: 'asd', txtPrograma: 'adsadasd'})
.then(function (response) {
console.log(response);
}, function (response) {
console.log(response);
})
Here the interface I used on android.
public interface ExecucaoService {
@POST("teste123")
public Call<Programa> gravaLote(@Body Programa programa);}
and to add credentials to the retrofit header I did as follows.
private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
private static Retrofit.Builder builder =
new Retrofit.Builder()
.baseUrl(baseUrlWS)
.addConverterFactory(GsonConverterFactory.create());
String credentials = username + ":" + password;
final String basic = "Basic " + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
httpClient.addInterceptor(new Interceptor() {
@Override
public okhttp3.Response intercept(Interceptor.Chain chain) throws IOException {
Request original = chain.request();
Request.Builder requestBuilder = original.newBuilder()
.header("Authorization", basic)
.header("Accept", "application/json")
.method(original.method(), original.body());
Request request = requestBuilder.build();
return chain.proceed(request);
}
});
OkHttpClient client = httpClient.build();
Retrofit retrofit = builder.client(client).build();
// retrofit.create(serviceClass); aqui pego a instancia do serviço criada.
here the image showing the success of the angle on the left and the problem on the right.
I still have hope that it is not retrofit or Postman problem, but a configuration that I did not do in sptring-boot.
Guys, I didn’t solve the problem with spring-boot-security, I removed it from the project and implemented a basic security filter, just to certify that you are logged in or not logged in and I gave up on finding the real problem. It’s definitely a situation where spring-security couldn’t get the Authentication header and log in with it. Since the project is a TCC and it won’t be on the Internet anytime soon, I’ll let you worry about that later. But if anyone can tell me the problem so I can use spring again I’d appreciate it.
– Jandrei Masiero