Good evening, I have the code below with the implementation of a service rest / test123, and when calling this method from the angular-js code, already authenticated with the basic spring-boot authentication I have access to this service. When I try to access it from the retrofit2 of my android or the chrome postman extension, the two give 404 error. Someone tells me what I'm doing wrong. Just to make it clear that I'm 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 is the example of angled-js code that works.
$http.post("/teste123", {id: 'asd', txtPrograma: 'adsadasd'})
.then(function (response) {
console.log(response);
}, function (response) {
console.log(response);
})
Here is the interface I used on android.
public interface ExecucaoService {
@POST("teste123")
public Call<Programa> gravaLote(@Body Programa programa);}
And to add the credentials in 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 is the image showing the success of the angular on the left and the problem on the right.
I still have hope that it is not a retrofit or a postman problem, but a configuration I did not do in sptring-boot.