404 when trying to access post url with springboot

Asked

Viewed 213 times

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.

inserir a descrição da imagem aqui

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.

1 answer

0

According to the spring-boot documentation, for you to create a basic web application, you only need the spring-boot Parent and the spring-boot-Starter-web dependency. The most common mistake that happened to me at the beginning of my learning with spring-boot, was in relation to the wrong header that was sent in the request, but this caused me a 400 or 405. I don’t have much experience with android but as far as I could notice, this code snippet (@POST("executing/writingLote")) indicates that you are sending a post to the execution/writingLote address and may be causing the 404 error, because your address is /test123. I hope I’ve helped!

  • You made the correct observation, I edited the code a few times and did not maintain integrity between these classes here in the post. I couldn’t test a filter to allow CORS yet, but the problem is when I add spring-security to the move. I’ll correct up there to leave no doubt.

  • About CORS, you can do this in a very simple way with what spring-boot already offers. For examplesee this section of the documentation (http://docs.spring.io/spring-boot/current/reference/htmlsingle/#boot-Features-Cors), which explains how to do. That is if you are using a recent version of spring-boot, of course. About spring-security, what is your question with it really? I just realized that when you add it to your project, you’re getting 404. How are you setting it up in your project? would be nice more details of how you do it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.