标签: spring-mvc

如何使用Curl从终端/命令行发布JSON数据到测试Spring REST?

我使用Ubuntu并在其上安装了Curl.我想用Curl测试我的Spring REST应用程序.我在Java端编写了我的POST代码.但是,我想用Curl测试它.我正在尝试发布JSON数据.示例数据如下:

{"value":"30","type":"Tip 3","targetModule":"Target 3","configurationGroup":null,"name":"Configuration Deneme 3","description":null,"identity":"Configuration Deneme 3","version":0,"systemId":3,"active":true}
Run Code Online (Sandbox Code Playgroud)

我用这个命令:

curl -i \
    -H "Accept: application/json" \
    -H "X-HTTP-Method-Override: PUT" \
    -X POST -d "value":"30","type":"Tip 3","targetModule":"Target 3","configurationGroup":null,"name":"Configuration Deneme 3","description":null,"identity":"Configuration Deneme 3","version":0,"systemId":3,"active":true \
    http://localhost:8080/xx/xxx/xxxx
Run Code Online (Sandbox Code Playgroud)

它返回此错误:

HTTP/1.1 415 Unsupported Media Type
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=utf-8
Content-Length: 1051
Date: Wed, 24 Aug 2011 08:50:17 GMT
Run Code Online (Sandbox Code Playgroud)

错误描述如下:

服务器拒绝此请求,因为请求实体的格式不受所请求方法()的请求资源支持.

Tomcat日志:"POST/ui/webapp/conf/clear HTTP/1.1"415 1051

关于Curl命令的正确格式的任何想法?

编辑:

这是我的Java端PUT代码(我测试过GET和DELETE,它们有效)

@RequestMapping(method = RequestMethod.PUT)
public Configuration updateConfiguration(HttpServletResponse response, @RequestBody Configuration configuration) { //consider @Valid tag
    configuration.setName("PUT worked");
    //todo If error occurs …
Run Code Online (Sandbox Code Playgroud)

rest json curl spring-mvc http-headers

2606
推荐指数
24
解决办法
263万
查看次数

Spring中的@ Component,@ Repository和@Service注释有什么区别?

@Component,@Repository@Service注释互换在Spring中,还是他们提供任何特殊的功能,除了作为一个符号设备?

换句话说,如果我有一个Service类并且我将注释更改@Service@Component,它仍然会以相同的方式运行吗?

或者注释是否也会影响类的行为和功能?

java spring annotations spring-mvc

1969
推荐指数
26
解决办法
86万
查看次数

自动装配如何在Spring工作?

关于控制(IoC)的反转是如何工作的,我有点困惑Spring.

假设我有一个名为的UserServiceImpl实现UserService接口的服务类.

怎么会这样@Autowired

而在我Controllers,我怎么会instantiateinstance这个服务的?

我会做以下吗?

UserService userService = new UserServiceImpl();
Run Code Online (Sandbox Code Playgroud)

java spring spring-mvc ioc-container autowired

481
推荐指数
5
解决办法
33万
查看次数

Jackson JSON和Hibernate JPA问题的无限递归

在尝试将具有双向关联的JPA对象转换为JSON时,我不断获取

org.codehaus.jackson.map.JsonMappingException: Infinite recursion (StackOverflowError)
Run Code Online (Sandbox Code Playgroud)

我找到的只是这个线程,基本上建议避免双向关联.有没有人对这个春天的bug有一个解决方法?

------编辑2010-07-24 16:26:22 -------

Codesnippets:

业务对象1:

@Entity
@Table(name = "ta_trainee", uniqueConstraints = {@UniqueConstraint(columnNames = {"id"})})
public class Trainee extends BusinessObject {

    @Id
    @GeneratedValue(strategy = GenerationType.TABLE)
    @Column(name = "id", nullable = false)
    private Integer id;

    @Column(name = "name", nullable = true)
    private String name;

    @Column(name = "surname", nullable = true)
    private String surname;

    @OneToMany(mappedBy = "trainee", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @Column(nullable = true)
    private Set<BodyStat> bodyStats;

    @OneToMany(mappedBy = "trainee", fetch = FetchType.EAGER, …
Run Code Online (Sandbox Code Playgroud)

java orm json spring-mvc jackson

375
推荐指数
15
解决办法
26万
查看次数

如何在返回String的Spring MVC @ResponseBody方法中响应HTTP 400错误?

我使用Spring MVC作为一个简单的JSON API,@ResponseBody基于以下方法.(我已经有一个直接生成JSON的服务层.)

@RequestMapping(value = "/matches/{matchId}", produces = "application/json")
@ResponseBody
public String match(@PathVariable String matchId) {
    String json = matchService.getMatchJson(matchId);
    if (json == null) {
        // TODO: how to respond with e.g. 400 "bad request"?
    }
    return json;
}
Run Code Online (Sandbox Code Playgroud)

问题是,在给定的场景中,用HTTP 400错误响应的最简单,最干净的方法是什么?

我确实遇到过这样的方法:

return new ResponseEntity(HttpStatus.BAD_REQUEST);
Run Code Online (Sandbox Code Playgroud)

...但我不能在这里使用它,因为我的方法的返回类型是String,而不是ResponseEntity.

java spring spring-mvc http-error

366
推荐指数
8
解决办法
34万
查看次数

354
推荐指数
6
解决办法
36万
查看次数

带有点(.)的Spring MVC @PathVariable被截断了

这是Spring MVC @PathVariable被截断的问题的延续

Spring论坛声明它已修复(3.2版本)作为ContentNegotiationManager的一部分.见下面的链接.
https://jira.springsource.org/browse/SPR-6164
https://jira.springsource.org/browse/SPR-7632

在我的应用程序中,带有.com的requestParameter被截断.

谁能解释我如何使用这个新功能?它是如何在xml中配置的?

注意:spring论坛 - #1带有点(.)的Spring MVC @PathVariable正在被截断

rest spring spring-mvc spring-annotations

345
推荐指数
14
解决办法
14万
查看次数

如何解决"未能懒散地初始化角色集合"的Hibernate异常

我有这个问题:

org.hibernate.LazyInitializationException:懒得初始化角色集合:mvc3.model.Topic.comments,没有会话或会话被关闭

这是模型:

@Entity
@Table(name = "T_TOPIC")
public class Topic {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int id;

    @ManyToOne
    @JoinColumn(name="USER_ID")
    private User author;

    @Enumerated(EnumType.STRING)    
    private Tag topicTag;

    private String name;
    private String text;

    @OneToMany(mappedBy = "topic", cascade = CascadeType.ALL)
    private Collection<Comment> comments = new LinkedHashSet<Comment>();

    ...

    public Collection<Comment> getComments() {
           return comments;
    }

}
Run Code Online (Sandbox Code Playgroud)

调用模型的控制器如下所示:

@Controller
@RequestMapping(value = "/topic")
public class TopicController {

    @Autowired
    private TopicService service;

    private static final Logger logger = LoggerFactory.getLogger(TopicController.class);


    @RequestMapping(value = "/details/{topicId}", method = RequestMethod.GET)
    public ModelAndView …
Run Code Online (Sandbox Code Playgroud)

java spring jsp hibernate spring-mvc

340
推荐指数
21
解决办法
46万
查看次数

spring @Controller和@RestController注释之间的区别

弹簧@Controller@RestController注释之间的区别.

可以@Controller注解同时用于Web MVC框架和REST的应用程序?
如果是,我们如何区分它是Web MVC还是REST应用程序.

java spring spring-mvc

326
推荐指数
9
解决办法
16万
查看次数

@RequestParam vs @PathVariable

是什么区别@RequestParam@PathVariable同时处理的特殊字符?

+@RequestParam空间接受了.

在这种情况下@PathVariable,+被接受为+.

java spring spring-mvc

324
推荐指数
4
解决办法
39万
查看次数