相关疑难解决方法(0)

Spring中的@RequestBody和@ResponseBody注释

有人可以解释Spring 3中的注释@RequestBody@ResponseBody注释吗?它们适用于什么?任何例子都会很棒.

java spring annotations spring-mvc

134
推荐指数
3
解决办法
28万
查看次数

Spring @ResponseBody注释如何在这个RESTful应用程序示例中工作?

我有一个以下列方式注释的方法:

/**
* Provide a list of all accounts.
*/
//  TODO 02: Complete this method.  Add annotations to respond
//  to GET /accounts and return a List<Account> to be converted.
//  Save your work and restart the server.  You should get JSON results when accessing 
//  http://localhost:8080/rest-ws/app/accounts
@RequestMapping(value="/orders", method=RequestMethod.GET)
public @ResponseBody List<Account> accountSummary() {
    return accountManager.getAllAccounts();
}
Run Code Online (Sandbox Code Playgroud)

所以我知道这个注释:

@RequestMapping(value="/orders", method=RequestMethod.GET)
Run Code Online (Sandbox Code Playgroud)

此方法处理对URL / orders表示的资源进行的GET HTTP请求.

此方法调用返回List的DAO对象.

其中Account表示系统上的用户,并且有一些代表此用户的字段,如:

public class Account {

    @Id
    @Column(name = …
Run Code Online (Sandbox Code Playgroud)

java rest spring json spring-mvc

81
推荐指数
2
解决办法
15万
查看次数

没有@ResponseBody的@RestController方法工作不正确

我有以下控制器:

@RestController
@RequestMapping(value = "/base/url")
public class MyController {

    @RequestMapping(
            value = "/child/url",
            method = RequestMethod.POST
    )
    @ResponseBody
    public String mmm() {      
        return "Ok";
    }
}
Run Code Online (Sandbox Code Playgroud)

现在它的工作(服务器响应Ok),但我认为这是@ResponseBody多余的,因为我们使用@RestController和删除@ResponseBody注释

我看到以下服务器响应:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
        <title>Error 404 Not Found</title>
    </head>
    <body>
        <h2>HTTP ERROR 404</h2>
        <p>Problem accessing /base/url/child/url/Ok. Reason:

            <pre>    Not Found</pre>
        </p>
        <hr />
        <i>
            <small>Powered by Jetty://</small>
        </i>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/> …
Run Code Online (Sandbox Code Playgroud)

java rest spring controller spring-mvc

6
推荐指数
1
解决办法
3143
查看次数

在 Spring 应用程序中拥有 RestController 和 Controller 的最佳实践

我不是在问是否有可能,我知道有可能,但我想知道在我的应用程序中有前端的同时提供休息服务的最佳方式是什么。

我正在开发一个 Spring Boot 应用程序,我目前有一个调用 jsp 页面的控制器和一个单独的RestController。我希望能够使用 Android 应用程序使用它。

那么在我的应用程序中同时拥有一个 Controller 和一个单独的Restcontroller是否正确?例如,将从 /api/* 调用 Rest 控制器方法。


编辑:我知道两者之间的区别,但是由于我希望能够返回视图(并且我不应该使用 RestController 来这样做)并且我想要一个休息服务我想知道我是否可以同时拥有他们(当然是分开的)。

非常感谢你。

java rest spring

6
推荐指数
1
解决办法
3569
查看次数

Spring不提供html文件

所以我试图按照本指南关于如何使用Spring提供html文件:http: //spring.io/guides/gs/serving-web-content/

我有完全相同的文件夹结构和完全相同的文件,但是当我运行spring boot服务器时,我localhost:8080/greeting只会显示greetingGreetingController它返回的字符串,就是它,如果我查看页面源代码,其中没有html .

我找不到任何相关的答案,因为所有类似的答案仍然使用.xml文件驱动的Spring,您在.xml文件中声明视图.但是本指南明确指出不需要使用.xml.它应该像那样工作.

制图:

@RestController
public class GreetingController {

    @RequestMapping("/greeting")
    public String greeting(@RequestParam(value="name", required = false, defaultValue="World") String name, Model model) {
        model.addAttribute("name", name);
        return "greeting";
    }
}
Run Code Online (Sandbox Code Playgroud)

使用@Controller抛出错误:

2015-02-25 14:50:14.830 ERROR 2378 --- [nio-8080-exec-7] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Circular view path [greeting]: would dispatch back to the current handler URL [/greeting] again. Check your ViewResolver setup! (Hint: This may …
Run Code Online (Sandbox Code Playgroud)

html java spring spring-mvc

3
推荐指数
1
解决办法
5554
查看次数

Spring boot 中无法显示 index.html 页面

我试图创建一个简单的弹簧启动应用程序

我创建了 spring boot 应用程序类、配置类、控制器类和 index.html。

我添加了 Thymeleaf 依赖项并将 html 页面放在资源文件夹下 (\src\main\resources\templates\index.html)

但是当我运行应用程序时它会出错

org.thymeleaf.exceptions.TemplateInputException:
Error resolving template "index.html", template might not exist or might not be accessible by any of the configured Template Resolvers
Run Code Online (Sandbox Code Playgroud)

请给我一个解决方案。

@SpringBootApplication
@ComponentScan(basePackages = {"com.mail"})
public class SpringBootWebApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootWebApplication.class, args);
    }
}
Run Code Online (Sandbox Code Playgroud)

控制器类

@RestController
public class IndexController {

    @RequestMapping(value="/", method = RequestMethod.GET)
    String index(){
        System.out.println("..............hit");
        return "index";
    }
Run Code Online (Sandbox Code Playgroud)

Thymeleaf 的 Web 配置

@Configuration
public class WebConfiguration extends WebMvcConfigurerAdapter{
    @Override …
Run Code Online (Sandbox Code Playgroud)

java thymeleaf spring-boot

2
推荐指数
1
解决办法
5907
查看次数