Problem with Rest controller in Spring

Asked

Viewed 179 times

0

I set Spring 4 to work without xml.

I created a @RestController simple, like this:

@RestController
public class JogadorRest {

    @RequestMapping("/ola/{jogador}")
    public Jogador message(@PathVariable String jogador) {

        Jogador j = new Jogador();
        j.setNome(jogador);

        return j;
    }

}

But when testing on Postman (http://localhost:8080/Resttestplayer/ola/Lior), I get an HTML instead of json:

<html>
    <head>
        <title>Error</title>
    </head>
    <body>/RestTestJogador/WEB-INF/view/ola/Liorr.jsp</body>
</html>

Why does that happen?

Is it the way I set up spring? My configuration looks like this:

@Configuration
@ComponentScan("br.com.testejogador.default")
@PropertySource(value = { "classpath:application.properties" })
public class AppConfig {

    @Autowired
    private Environment env;

    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/view/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }

}

1 answer

0

You need to say, in Requestmapping, that your method wants to return for example a JSON or an XML. Since you didn’t set anything up, he’s using the search convention for a JSP based on the address you typed in.

Alberto

Browser other questions tagged

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